IOS 事務在資料庫中的用處

2021-07-11 01:12:38 字數 1313 閱讀 5754

sqlite 是支援事務處理的。如果你知道你要同步刪除很多資料,不仿把它們做成乙個統一的事務。

通常一次 sqlite3_exec 就是一次事務,如果你要刪除1萬條資料,sqlite就做了1萬次:開始新事務->刪除一條資料->提交事務->開始新事務->… 的過程。這個操作是很慢的。因為時間都花在了開始事務、提交事務上。

你可以把這些同類操作做成乙個事務,這樣如果操作錯誤,還能夠回滾事務。

事務的操作沒有特別的介面函式,它就是乙個普通的 sql 語句而已:

分別如下:

int result; 

result = sqlite3_exec( db, "begin transaction", 0, 0, &zerrormsg ); //開始乙個事務

result = sqlite3_exec( db, "commit transaction", 0, 0, &zerrormsg ); //提交事務

result = sqlite3_exec( db, "rollback transaction", 0, 0, &zerrormsg ); //回滾事務

這裡我用的是fmdatabase;

#pragma mark -

- (void) demofortransaction

[_database executeupdate:@"create table if not exists student (id text,name text,iphone text)"];

[_database close];

[self testdbspeed];

}- (void)insertdata:(int)fromindex usetransaction:(bool)usetransaction

// nsstring *deletesql = @"delete from student where id=?";

// bool b = [_database executeupdate:deletesql,nid];

// if (!b) else}}

@catch (n***ception *exception)

@finally

}}else}}

[_database close];

}}-(void)testdbspeed

一次插入500條資料的時候,使用事務和不使用事務的時間相差 30倍。資料越多的話,差距的倍數就越大。所以如果資料量比較大的情況下,能使用事務還是盡量使用事務處理比較好。

IOS 之sqlite在資料庫中新增資料

首先引入檔案 libsqlite3.fmdb 包含global.m,global.h檔案 關閉arc 用mesasqlite建立乙個資料庫,引入檔案中 其次 首先,在global.h檔案中找到 define kdbname shuju.db 如果你建立的資料庫檔名為 liyongxing.db,那就...

xml在資料庫中的應用

1。將xml文件儲存為乙個文字塊 create a table to hold the manuals for the games statement s conn.createstatement s.executeupdate create table manuals gameid int,man...

樹在資料庫中的應用

1 索引在資料庫中的作用 最基本的查詢演算法是順序查詢,遍歷表然後逐行匹配行值是否等於待查詢的關鍵字,時間複雜度為 o n 不適合資料量大的時候。在資料之外,資料庫還維護著滿足特定查詢演算法的資料結構,這些資料結構以某 種方式引用資料,這樣就可以在這些資料結構上實現高階查詢演算法,這種資料結構就是,...