我的 Iphone 应用程序有问题。有时我的应用程序成功运行,但在某些情况下它给出“数据库被锁定异常”,这就是为什么我无法从 sqlite 数据库读取或插入数据。如果有人有解决方案,请建议我。这是我将数据插入数据库的代码谢谢。
-(void)insertDataIntoDatabase
{
NSLog(@"insertDataIntoDatabase-----1");
@try{
tUserName=userNameTf.text;
tLevel=[NSString stringWithFormat:@"%d",level];
tMoves=[NSString stringWithFormat:@"%d",moves];
NSLog(@"tLevel;;;%@",tLevel);
// NSString *tdatabaseName = @"FlipScoreBord.sqlite";
sqlite3_stmt *addStatement;
// NSArray *tdocumentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *tdocumentsDir = [tdocumentPaths objectAtIndex:0];
// NSString *tdatabasePath = [tdocumentsDir stringByAppendingPathComponent:tdatabaseName];
NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO Moves (User_Name,User_Label,User_Moves) VALUES('%@','%@','%@')",tUserName,tLevel,tMoves];
const char *sql = [insertQuery cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(tdatabase, sql, -1, &addStatement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(tdatabase));
}
sqlite3_bind_text(addStatement, 0, [tUserName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 1, [tLevel UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 2, [tUserName UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStatement))
{
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(tdatabase));
sqlite3_reset(addStatement);
}
sqlite3_close(tdatabase);
}
@catch (NSException *r)
{
NSLog(@"Exception---- %@",r);
}
NSLog(@"insertDataIntoDatabase-----2");
}