在 iPhone 上将 NSData 插入 SQLite
到目前为止,我已经成功创建了这种方法来插入 iPhone 上的 SQLite 数据库:
- (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);
}
现在我正在考虑在数据库中存储图像。到目前为止,我已经找到了这个:
UIImage *cachedImage = [UIImage imageNamed:@"Icon.png"];
NSData *dataForImage = UIImagePNGRepresentation(cachedImage);
但是我在尝试将此 NSData 插入到生成 sqlStatement 的 char 数组中时遇到问题。有人知道如何做到这一点吗?
(我在数据库中有一个 blob 类型的字段用于此)。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会使用 sqlite3_stmt 而不是字符串。然后,您可以使用 sqlite3_bind_blob 将 blob 绑定到准备好的语句。
但是,实际上,将图像存储在磁盘上并将路径存储在数据库中对于性能来说是最好的。
另一种方法是对数据进行 Base 64 编码,我用这种方法以 XML 形式发送图像数据。我把它作为 NSString 的一个类别:
I would use
sqlite3_stmt
instead of a string. Then, you could usesqlite3_bind_blob
to 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: