Sqlite3_key的设计是很慢的,上面的代码是强制一个打开/键/关闭的过程,你插入的每一个记录,这将大大降低你的操作速度。
我使用以下代码插入行超过5000在我的ios应用程序。如果我不使用行sqlite3_close(dbv);语句我得到一个错误无法打开数据库。如果我使用语句sqlite3_close(dbv)数据插入大约需要10-15分钟。我怎样才能更快地插入记录而不出现错误?
-(void)insertrecordIntoTable:(NSString*)SQLStatement
{
NSString *sqlStr=SQLStatement;
const char *sql=[sqlStr UTF8String];
const char* key = [@"StrongPassword" UTF8String];
sqlite3_stmt *statement1;
@try {
int res = sqlite3_open([[self.databaseURL path] UTF8String], &dbv);
if (res != SQLITE_OK)
sqlite3_open([[self.databaseURL path] UTF8String], &dbv);
sqlite3_key(dbv, key, (int)strlen(key));
if(sqlite3_prepare_v2(dbv, sql, -1, &statement1, nil)==SQLITE_OK)
{
NSLog(@"done");
}
else
{
NSLog(@"the error occurred here is %s ",sqlite3_errmsg(dbv));
}
res =sqlite3_step(statement1);
if(res !=SQLITE_DONE)
NSLog(@"Error upadating table");
sqlite3_finalize(statement1);
sqlite3_close(dbv);
}
@catch (NSException *exception) {
}
@finally {
sql=NULL;
sqlStr=NULL;
SQLStatement=NULL;
}
}
除了Begin Transaction和Commit Transaction之外,我尝试打开数据库一次,在整个应用程序期间保持连接打开,并在应用程序终止时关闭连接?
打开数据库后添加以下代码
sqlite3_exec(mDb, "BEGIN TRANSACTION", NULL, NULL, &errorMessage);
并且,在关闭数据库之前添加以下代码
sqlite3_exec(mDb, "COMMIT TRANSACTION", NULL, NULL, &errorMessage);
查看更多详细信息-
如何在iPad中快速插入40000条记录到sqlite数据库