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
See Question&Answers more detail:os