我在sqlite更新中遇到错误,这里正确给出了查询,但值没有更新。这里我需要更新给定位置的描述值,下面我添加了我的代码,请帮助我
-(void) update:(NSString *)filePath withName:(NSString *)place description:(NSString*)description
{
sqlite3* db = NULL;
int rc=0;
sqlite3_stmt* stmt =NULL;
rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
if (SQLITE_OK != rc)
{
sqlite3_close(db);
NSLog(@"Failed to open db connection");
}
else
{
NSString * query = [NSString stringWithFormat:@"UPDATE places SET description=%@ WHERE place=%@",description,place];
sqlite3_bind_text(stmt, 1, [place UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [description UTF8String], -1, SQLITE_TRANSIENT);
NSLog(@"query %@",query);
char * errMsg;
rc = sqlite3_exec(db, [query UTF8String] ,NULL,&stmt,&errMsg);
if(SQLITE_OK != rc)
{
NSLog(@"Failed to insert record rc:%d, msg=%s",rc,errMsg);
}
else{
NSLog(@"Sucessfully updated");
}
sqlite3_finalize(stmt);
sqlite3_close(db);
}
}
创建表
-(int) createTable:(NSString*) filePath
{
sqlite3* db = NULL;
int rc=0;
NSLog(@"create");
rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (SQLITE_OK != rc)
{
sqlite3_close(db);
NSLog(@"Failed to open db connection");
}
else
{
char * query ="CREATE TABLE IF NOT EXISTS places ( id INTEGER PRIMARY KEY AUTOINCREMENT,place TEXT ,locationname TEXT,time TEXT,description TEXT,notifyTime TEXT,radius TEXT,lat DOUBLE,lon DOUBLE ,notify TEXT,selection INTEGER)";
char * errMsg;
rc = sqlite3_exec(db, query,NULL,NULL,&errMsg);
if(SQLITE_OK != rc)
{
NSLog(@"Failed to create table rc:%d, msg=%s",rc,errMsg);
}
else{
NSLog(@"Sucessfully Created ");
}
sqlite3_close(db);
}
return rc;
最佳答案
首先打开数据库连接。 然后写下面的代码。
NSString * query = [NSString stringWithFormat:@"UPDATE places SET description=%@ WHERE place=%@",description,place];
SQL=[NSString stringWithString:query];
sqlite3_stmt *insert_statement=nil;
if (sqlite3_prepare_v2(database, [SQL UTF8String], -1, &insert_statement, NULL) != SQLITE_OK) {
//NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
int result=sqlite3_step(insert_statement);
sqlite3_finalize(insert_statement);
然后关闭数据库连接
关于ios - 在 ios 中的 sqlite 更新中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687717/