PHP function to convert date from MM/DD/YYYY to YYYY-MM-DD

//Convert MM/DD/YYYY to YYYY-MM-DD. This format is useful if you want to insert into a MySQL date field.

function date_to_mysql_date($date){
$date_year=substr($date,6,4);
$date_month=substr($date,0,2);
$date_day=substr($date,3,2);


$date=date("Y-m-d", mktime(0,0,0,$date_month,$date_day,$date_year));
return $date;
}

Preparing a sqlite3 statement to execute in xcode

sqlite3_prepare_v2(database, cString, -1, &statement, nil);

This is the code used to prepare the sqlite3 statement to execute. Here the arguments are as follows

database -> sqlite3 object, declared as       sqlite3 *database;

cString -> const char, obtained by converting the query string to char format.

statement -> sqlite3_stmt object, declared as      sqlite3_stmt *statement;

Getting the string result from a query in sqlite3 xcode

NSString *message = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, colIndex)];

This is the code used to get the string result from a query in sqlite3. Here the obtained result is a char value and we have to convert it to NSString. Here, the first argument is statement, which is the sqlite3_stmt object, declared as follows
sqlite3_stmt *statement;
The second argument is the integer value, which represent the column index from the query.

Getting the integer result from a query in sqlite3 xcode

sqlite3_column_int(statement, 0);

This is the code used to get the integer result from a query in sqlite3. Here the first argument is statement, which is the sqlite3_stmt object, declared as follows

sqlite3_stmt *statement;

The second argument is the integer value, which represent the column index from the query.

 

Displaying sqlite3 Error Message in xcode

sqlite3 *database;     // the sqlite3 database object
.
.
.
NSLog( @”Failed from sqlite3_step(statement). Error is:  %s”, sqlite3_errmsg(database) );    // the parameter for the method, sqlite3_errmsg(sqlite3*) is the sqlite3 object, which represents the database.

Making a Database Backup using GZip compression.

Backing up your database is very important in a database driven application. Ideally database should be backed up often. There are a lot of ways to accomplish this. I’ll show how to back up database using command-line commands.

The command to run the backup is:

mysqldump -u mysqluser -p mysqldatabase

“mysqldump” program is a tool for creating database backups.

The parameters used are:

“-u” switch means you’re going to specify a username to connect with, which must follow, like “-u mysqluser” above

“-p” switch means you’re either going to immediately specify the password to use (with no space), or it’ll prompt you for one

The final parameter used in the example above is the name of the database to backup

If you run the command above, you would see the contents of your database go scrolling on the screen.

To place the contents of the output into a file, execute the following command

mysqldump -u mysqluser -p mysqldatabase > db_backup.sql

Now you should be able to see a file named “db_backup.sql”, and if you open it you can see a SQL script with the structure and content of your database ready for restoration or migration.

Now compress this SQL script using GZip compression. Instead of gzip, you can use bzip, tar etc..

To add compression into this command, just execute the following command.

mysqldump -u mysqluser -p mysqldatabase | gzip > db_backup.sql.gz

Now your database backup is ready to import/export for future use.