So far I’ve managed to create this method for inserting into a SQLite database on the iPhone:
- (void) insertToDB :(NSString *)Identifier :(NSString *)Name
{
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
char *sql1 = "INSERT INTO table VALUES ('";
const char *sql2 = [Identifier cStringUsingEncoding:[NSString defaultCStringEncoding]];
char *sql3 = "', '";
const char *sql4 = [Name cStringUsingEncoding:[NSString defaultCStringEncoding]];
char *sql5 = "')";
char *sqlStatement[255];
strcpy(sqlStatement, sql1);
strcat(sqlStatement, sql2);
strcat(sqlStatement, sql3);
strcat(sqlStatement, sql4);
strcat(sqlStatement, sql5);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_last_insert_rowid(database);
sqlite3_reset(compiledStatement);
}
sqlite3_finalize(compiledStatment);
}
sqlite3_close(database);
}
Now I’m looking at storing an image in the database. So far I’ve found this:
UIImage *cachedImage = [UIImage imageNamed:@"Icon.png"];
NSData *dataForImage = UIImagePNGRepresentation(cachedImage);
But i’m having trouble trying to insert this NSData into the char array which makes the sqlStatement. Anyone got an idea how to do this?
(I have a field in the database of type blob for this).
Thanks
I would use
sqlite3_stmtinstead of a string. Then, you could usesqlite3_bind_blobto bind the blob to the prepared statement.But, really, it would be best for performance to store the image on disk and the path in the database.
Another way to do it, one that I use to send image data in XML, is to base 64 encode the data. I have it here as a category on NSString: