![](/img/trans.png)
[英]Getting unable to open database file while inserting bulk data in sqlite db
[英]Objective C “unable to open database file” while inserting large amount of data in loop SQLite
您可以使用以下代碼在單個事務中插入多行
char* error;
sqlite3_exec(mDb, "BEGIN TRANSACTION", NULL, NULL, &error);
char buffer[] = "INSERT INTO example VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)";
sqlite3_stmt* stmt;
sqlite3_prepare_v2(mDb, buffer, strlen(buffer), &stmt, NULL);
for (unsigned i = 0; i < mVal; i++) {
std::string id = getID();
sqlite3_bind_text(stmt, 1, id.c_str(), id.size(), SQLITE_STATIC);
sqlite3_bind_double(stmt, 2, getDouble());
sqlite3_bind_double(stmt, 3, getDouble());
sqlite3_bind_double(stmt, 4, getDouble());
sqlite3_bind_int(stmt, 5, getInt());
sqlite3_bind_int(stmt, 6, getInt());
sqlite3_bind_int(stmt, 7, getInt());
if (sqlite3_step(stmt) != SQLITE_DONE) {
printf("Commit Failed!\n");
}
sqlite3_reset(stmt);
}
sqlite3_exec(mDb, "COMMIT TRANSACTION", NULL, NULL, &error);
sqlite3_finalize(stmt);
通過這BEGIN
和COMMIT
事務可以在數據庫中插入多行。
有關更多詳細信息,請參考以下鏈接: 批量行插入
編輯:
要檢查是否已有數據將更新行要求,可以在插入查詢下面使用:
INSERT OR REPLACE INTO <TableName> VALUES ()
注意:僅當表中有PRIMARY KEY時,以上查詢才有效。
希望這有助於解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.