我一直在使用以下代码更新我的sqlite表中的数据,但一直认为这不是最佳方式。基本上,因为我不知道用户在我的二维NSMutableArray中更新了什么以及在哪里,我正在做的是删除表,再次使用所有列创建它,然后在其中插入数据。
通过表和我的数组之间的某种数据比较,有更好的方法吗?请指出下面出现的任何其他不当行为:
-(void)updateCurrentItemsData:(NSArray *) dataArray
{
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
gameDBPath=[documentsDirectory stringByAppendingPathComponent:@"gameData.sqlite"];
const char *dbpath = [gameDBPath UTF8String];
int test;
char *errMsg;
if (sqlite3_open(dbpath, &gamedatabase)==SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM currentitems"];
const char *query_stmt = [querySQL UTF8String];
test=sqlite3_exec(gamedatabase, query_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 1 %i",test);
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS currentitems(itemid, name, quantity, damaged, price)";
test=sqlite3_exec(gamedatabase, sql_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 2 %i",test);
for (int i=0; i<dataArray.count; i++)
{
NSString *querySQL= [NSString stringWithFormat:@"INSERT INTO currentitems(itemid, name, quantity, damaged, price) VALUES('%i','%@','%i','%i','%i')",[[[dataArray objectAtIndex:i] objectAtIndex:0] intValue],[[dataArray objectAtIndex:i] objectAtIndex:1],[[[dataArray objectAtIndex:i] objectAtIndex:2] intValue],[[[dataArray objectAtIndex:i] objectAtIndex:3] intValue],[[[dataArray objectAtIndex:i] objectAtIndex:4] intValue]];
//NSLog(@"querySQL is %@",querySQL);
const char *insert_stmt= [querySQL UTF8String];
test=sqlite3_exec(gamedatabase, insert_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 3 %i",test);
}
sqlite3_finalize(statementgame);
sqlite3_close(gamedatabase);
}
}
感谢您寻找:)
答案 0 :(得分:0)
您可以使用交易来提高效果 - 只需查看下面稍作修改的代码:
-(void)updateCurrentItemsData:(NSArray *) dataArray
{
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
gameDBPath=[documentsDirectory stringByAppendingPathComponent:@"gameData.sqlite"];
const char *dbpath = [gameDBPath UTF8String];
int test;
char *errMsg;
if (sqlite3_open(dbpath, &gamedatabase)==SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM currentitems"];
const char *query_stmt = [querySQL UTF8String];
test=sqlite3_exec(gamedatabase, query_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 1 %i",test);
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS currentitems(itemid, name, quantity, damaged, price)";
test=sqlite3_exec(gamedatabase, sql_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 2 %i",test);
sqlite3_exec(gamedatabase, "BEGIN TRANSACTION", NULL, NULL, NULL);
for (int i=0; i<dataArray.count; i++)
{
NSString *querySQL= [NSString stringWithFormat:@"INSERT INTO currentitems(itemid, name, quantity, damaged, price) VALUES('%i','%@','%i','%i','%i')",[[[dataArray objectAtIndex:i] objectAtIndex:0] intValue],[[dataArray objectAtIndex:i] objectAtIndex:1],[[[dataArray objectAtIndex:i] objectAtIndex:2] intValue],[[[dataArray objectAtIndex:i] objectAtIndex:3] intValue],[[[dataArray objectAtIndex:i] objectAtIndex:4] intValue]];
//NSLog(@"querySQL is %@",querySQL);
const char *insert_stmt= [querySQL UTF8String];
test=sqlite3_exec(gamedatabase, insert_stmt, NULL, NULL, &errMsg);
if (test!=0) NSLog(@"exec is 5 3 %i",test);
}
sqlite3_exec(gamedatabase, "COMMIT TRANSACTION", NULL, NULL, NULL);
sqlite3_finalize(statementgame);
sqlite3_close(gamedatabase);
}
}