![](/img/trans.png)
[英]Objective C “unable to open database file” while inserting large amount of data in loop SQLite
[英]Getting unable to open database file while inserting bulk data in sqlite db
我正在使用以下代碼在ios應用程序中插入超過5000行。 如果我不使用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;
}
}
除了“開始事務”和“提交事務”之外,我還嘗試一次打開數據庫,在整個應用程序期間都保持連接打開狀態,並在應用程序終止時關閉連接? sqlite3_key在設計上很慢,並且上面的代碼對插入的每條記錄強制執行打開/密鑰/關閉過程,這將大大降低您的操作速度。
打開數據庫后添加此代碼
sqlite3_exec(mDb, "BEGIN TRANSACTION", NULL, NULL, &errorMessage);
並且,在關閉數據庫之前添加此代碼
sqlite3_exec(mDb, "COMMIT TRANSACTION", NULL, NULL, &errorMessage);
有關更多詳細信息,請檢查此鏈接-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.