優化SQLite3資料庫插入10000條資料

2021-06-27 18:30:01 字數 611 閱讀 7884

如果用普通的方式向資料庫中插入10000條資料,會不會妨礙使用者體驗呢?答案是肯定會,那麼如何優化我們的資料庫的各種操作呢。

(1)使用事務。

string sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";

sqlitestatement stat = db.compilestatement(sql);

db.begintransaction();

for (station line : buslines)

db.settransactionsuccessful();

db.endtransaction();

db.close();

可以大大減少插入資料庫所需的時間。

那麼為啥呢?你如果是普通的插入資料庫的方法的話,插入10000條資料需要執行10000次的  事務開始+執行sql+事務結束,而上面事務開始和事務結束只執行了一次。大部分的時間實在執行sql。

(2)  這個不知道好使不好使。

(3)開啟乙個執行緒,執行完插入資料庫的操作之後通過handler來發訊息。

Sqlite3 資料庫使用

iphone本身是支援 sqlite3 資料庫的,在專案中匯入libsqlite3.dylib。並建立資料庫,在終端,建立資料庫的方式 mkdir sql 建立sql資料夾 cd sql 進入sql目錄下 sqlite3 student.sql 建立名為 student.sql的資料庫 建立表 插入...

sqlite3資料庫操作

1 開啟資料庫 1 需要制定資料庫的路徑 nsstring filepath nshomedirectory documents data.sqlite 2 建立資料庫的物件 sqlite3 qingyundb null 3 開啟命令 sqlite3 open dbfilepath utf8stri...

SQLite3資料庫操作

簡單的sqlite3語句,通過字串拼接執行資料庫操作。1.建立資料庫格式 db.execsql create table if not exists sharp id integer primary key,name varchar,level integer,high integer 其真正的有效...