为什么数据没有插入到数据库 sqlite 中,而是显示我采用静态路径并插入的数据?
why the data is not inserting in database sqlite but show data that i take static path and inserted?
////此方法用于显示数据并且有效
NSString *databasePath =[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"images.db"];
if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK){
const char *sql = "SELECT * FROM IMAGEDATA";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(sqlStatement, 1) length: sqlite3_column_bytes(sqlStatement, 1)];
imagesfromdb =[UIImage imageWithData:imageData];
NSLog(@"IMAGE :::%@",imagesfromdb);
[databaseimage addObject:imagesfromdb];
//imageData is saved favicon from website.
}
}
}
/////此方法用于插入数据。
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK)
{
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1);
const char* sqliteQuery = "INSERT INTO IMAGEDATA (NAME, IMAGE) VALUES (?, ?)";
//sqlite3_stmt* statement;
if( sqlite3_prepare_v2(database, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
{
sqlite3_bind_text(statement, 1, [fileName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [imageData bytes], (int)[imageData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
}
else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(database) );
// Finalize and close database.
sqlite3_finalize(statement);
}
我无法理解此方法不起作用,但此方法会显示数据,直到我关闭应用程序。但在那之后数据将消失并且没有保存在数据库文件中
您的应用沙箱 mainBundle
内的所有资产都有 readonly
属性。这就是为什么在您的情况下您可以读取数据但不能修改数据的原因。因此,您应该首先将数据库文件复制到具有 readwrite
访问权限的 DocumentDirectory
,然后您就可以在该数据库中执行 write
操作。检查此问题以了解如何将数据库文件从 mainBundle 复制到 DocumentDirectory
。
How to copy sqlite database when application is launched in iOS?
您也可以按照此 Tutorial 了解所有步骤
////此方法用于显示数据并且有效
NSString *databasePath =[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"images.db"];
if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK){
const char *sql = "SELECT * FROM IMAGEDATA";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(sqlStatement, 1) length: sqlite3_column_bytes(sqlStatement, 1)];
imagesfromdb =[UIImage imageWithData:imageData];
NSLog(@"IMAGE :::%@",imagesfromdb);
[databaseimage addObject:imagesfromdb];
//imageData is saved favicon from website.
}
}
}
/////此方法用于插入数据。
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &database)==SQLITE_OK)
{
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1);
const char* sqliteQuery = "INSERT INTO IMAGEDATA (NAME, IMAGE) VALUES (?, ?)";
//sqlite3_stmt* statement;
if( sqlite3_prepare_v2(database, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
{
sqlite3_bind_text(statement, 1, [fileName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [imageData bytes], (int)[imageData length], SQLITE_TRANSIENT);
sqlite3_step(statement);
}
else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(database) );
// Finalize and close database.
sqlite3_finalize(statement);
}
我无法理解此方法不起作用,但此方法会显示数据,直到我关闭应用程序。但在那之后数据将消失并且没有保存在数据库文件中
您的应用沙箱 mainBundle
内的所有资产都有 readonly
属性。这就是为什么在您的情况下您可以读取数据但不能修改数据的原因。因此,您应该首先将数据库文件复制到具有 readwrite
访问权限的 DocumentDirectory
,然后您就可以在该数据库中执行 write
操作。检查此问题以了解如何将数据库文件从 mainBundle 复制到 DocumentDirectory
。
How to copy sqlite database when application is launched in iOS?
您也可以按照此 Tutorial 了解所有步骤