因此,我有一個應用程序,我將數據輸入到sqlite數據庫,它從一個文本文件中獲取數據拖入文檔目錄。 的內容是這樣的:sqlite連續插入語句停止運行在大約200
file1.pdf, A1, A3, folderName1,
file2.pdf, A4, A75, folderName2,
我曾與文本的10串進行了測試。它的工作和插入很好,所以我決定創建一個更多的測試。 (357)這不會是現在使用的最大值,而是足夠大的樣本。
我跑了這個,並在大約240 ish插入查詢更改爲錯誤準備。然後下面的錯誤開始:無法打開數據庫。 - 我爲sqlite3_open!= SQLITE_OK創建的錯誤消息。
2016-07-12 09:49:10.289 AppName[897:491157]/ /
2016-07-12 09:49:10.290 AppName[897:491157] INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file1.pdf', 'Statement', 'S460', 'S460' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file1.pdf')
2016-07-12 09:49:10.317 AppName[897:491157]/ /
2016-07-12 09:49:10.317 AppName[897:491157] INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file2.pdf', 'Important', 'S461', 'S461' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file2.pdf')
2016-07-12 09:49:10.341 AppName[897:491157]/ /
2016-07-12 09:49:10.342 AppName[897:491157] INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file3.pdf', 'Important', 'S462', 'S463' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file3.pdf')
2016-07-12 09:49:10.343 AppName[897:491157] Error running insert: INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file3.pdf', 'Important', 'S462', 'S463' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file3.pdf')
2016-07-12 09:49:10.343 AppName[897:491157]/ /
2016-07-12 09:49:10.344 AppName[897:491157] Error: cannot open database
2016-07-12 09:49:10.344 AppName[897:491157] INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file4.pdf', 'Important', 'S464', 'S464' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file4.pdf')
2016-07-12 09:49:10.344 AppName[897:491157] Error preparing INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT 'file4.pdf', 'Important', 'S464', 'S464' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = 'file4.pdf')
以上是我的輸出的一個片段,顯示了我的INSERT語句和出現的錯誤消息。正如你所看到的,前兩次照常進行。然後第三個文件在運行INSERT語句時遇到了問題。然後從其他文件不能打開數據庫,我認爲這是爲什麼它不能準備聲明。
我已經試過
我認爲它是這樣做的原因是因爲某行 被扔它關閉。 - 我剪下文本文件的下半部分並將其粘貼到頂部。第一批進展順利,所以這不是原因。
我注意到有一個命令叫做sqlite for sqlite,用於多重插入查詢。我是否需要做類似iOS的操作?
-(void) addFileName:(NSString *)fileName
fileType:(NSString *)fileType
refNumber:(NSString *)bates
fileLocation:(NSString *)location
endrefNumber:(NSString *)endBates
{
long long fileID;
NSLog(@"/ /");
[self openDB];
// Names Table
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO Files (fileName, folder, refNum, endrefNum) SELECT '%@', '%@', '%@', '%@' WHERE NOT EXISTS(SELECT fileName FROM Files WHERE fileName = '%@')", fileName, fileType, bates, endBates, fileName];
NSLog(@"%@",insertSQL);
const char* query1 = [insertSQL UTF8String];
sqlite3_stmt * query1_stmt = NULL;
if (sqlite3_prepare_v2(database, query1, -1, &query1_stmt, NULL) != SQLITE_OK){
NSLog(@"Error preparing %@", insertSQL);
[self closeDB];
return;
}
if (sqlite3_step(query1_stmt) != SQLITE_DONE) {
NSLog(@"Error running insert: %s", query1);
[self closeDB];
return;
}
// row id
fileID = sqlite3_last_insert_rowid(database);
// phones table
insertSQL = [NSString stringWithFormat:@"INSERT INTO Location (FileID, Location) VALUES (%lld, '%@')", fileID, location];
const char* query2 = [insertSQL UTF8String];
sqlite3_stmt * query2_stmt = NULL;
if (sqlite3_prepare_v2(database, query2, -1, &query2_stmt, NULL) != SQLITE_OK){
NSLog(@"Error preparing %@", insertSQL);
[self closeDB];
return;
}
if (sqlite3_step(query2_stmt) != SQLITE_DONE) {
NSLog(@"Error running insert: %s", query2);
[self closeDB];
return;
}
[self closeDB];
}
開放式數據庫
-(void) openDB
{
if ((sqlite3_open([[self dbDocPath] UTF8String], &database)) != SQLITE_OK)
NSLog(@"Error: cannot open database");
}
關閉數據庫
-(void) closeDB
{
sqlite3_close(database);
}
UPDATE
所以我做了它,以便在數據庫打開一次,插入的所有數據,然後在完成後關閉。這似乎是數據庫的打開和關閉是造成問題的原因。對我來說,似乎有點奇怪,如果有人能解釋爲什麼這會有所幫助。
所有插入一個單一的交易。但是你的問題是它根本不起作用,因爲你的代碼有一個bug(你沒有顯示)。 –
添加的代碼,我現在要做的是打開連接,讀取並插入,然後關閉連接,而不是打開和關閉每個插入語句。 – kni9ht
爲什麼你打開/關閉每個查詢的數據庫?打開它並保持打開狀態,並在應用程序進入後臺時關閉它。只有在關閉時才能打開它,只有打開時才關閉(這需要防護碼)。另外打開數據庫可能會失敗,所以你需要一個返回值,你需要檢查它。 – Droppy