標籤:
關於sqlite是一個輕量的、跨平台的、開源的資料庫引擎。他在讀寫效率,操作便捷程度,記憶體消耗上具有很大的優越性,所以很受移動開發人員的喜愛。當然,sqlite 也因其力求簡單高效,也就限制了它對並發,海量資料的處理。這篇部落客要講的是iOS開發中sqlite和開源庫FMDB的使用。
demo 地址 TP
- 首先開啟資料庫
int result = sqlite3_open_v2(fileName.UTF8String, &db, SQLITE_IOERR_READ|SQLITE_IOERR_WRITE|SQLITE_OPEN_CREATE, NULL); if (result == SQLITE_OK) { NSLog(@"open true"); }
2. 建立表
//建立表 const char *sqlCreate = "CREATE TABLE IF NOT EXISTS t_demo (id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL);"; char *errmsg = NULL; result = sqlite3_exec(db, sqlCreate, NULL, NULL, &errmsg); if (result == SQLITE_OK) { NSLog(@"create true"); }
3. 執行sql語句
//執行sql語句 //1.插入 NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO t_demo (name,age) VALUES(‘%@‘,‘%d‘);",@"one",19]; sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg); sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg); sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"insert false"); } // 2.查詢 NSString *sqlQuery = [NSString stringWithFormat:@"SELECT id,name,age FROM t_demo;"]; sqlite3_stmt *stmt = NULL; //準備執行語句 if (sqlite3_prepare_v2(db, sqlQuery.UTF8String, -1, &stmt, NULL) == SQLITE_OK) { //分步執行 while (sqlite3_step(stmt) == SQLITE_ROW) { int ID = sqlite3_column_int(stmt, 0); const unsigned char *name = sqlite3_column_text(stmt, 1); int age = sqlite3_column_int(stmt, 2); NSLog(@"%d***%s******%d",ID,name,age); } } //釋放 sqlite3_free(stmt);
- 開啟資料庫
sqlite3_open_v2( const char *filename, /* 資料庫名字 */ sqlite3 **ppDb, /* 資料庫執行個體 */ int flags, /* Flags */ const char *zVfs /* Name of VFS module to use */ );
2.執行sql語句
sqlite3_exec( sqlite3*, /* 資料庫執行個體*/ const char *sql, /* sql語句 */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ );
3.檢查sql語句文法
sqlite3_prepare_v2( sqlite3 *db, /* 資料庫執行個體 */ const char *zSql, /* sql語句 */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ );
4.查詢一行資料,如果查詢到一行資料,就會返回SQLITE_ROW
sqlite3_step(sqlite3_stmt*);
5.利用stmt獲得某一列的值
double sqlite3_column_double(sqlite3_stmt*, int iCol); // 浮點數據int sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型資料sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 長整型資料const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二進位文本資料const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); // 字串資料
FMDB 是對sqlite的封裝。源碼地址 https://github.com/ccgus/fmdb,操作是oc方法,這對不熟悉或者不想操作c語言方法的同學來說是一個解脫。
首先匯入sqlite系統庫,引入標頭檔#import "FMDB.h"
NSString *docDic = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; NSString *fileName = [docDic stringByAppendingPathComponent:@"fmdb.db"]; //建立資料庫,路徑為空白的話,在記憶體中建立資料庫 fmdb = [FMDatabase databaseWithPath:fileName]; [fmdb open]; //執行sql語句,增、刪、改都是executeUpdate方法 NSString *sqlCreate =@"CREATE TABLE IF NOT EXISTS t_demo (id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL);"; BOOL res = [fmdb executeUpdate:sqlCreate]; NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO t_demo (name,age) VALUES(‘%@‘,‘%d‘);",@"one",19]; res = [fmdb executeUpdate:sqlInsert]; if (!res) { NSLog(@"error when creating db table"); } else { NSLog(@"success to creating db table"); } //執行查詢 NSString *sqlQuery = [NSString stringWithFormat:@"SELECT id,name,age FROM t_demo;"]; FMResultSet * rs = [fmdb executeQuery:sqlQuery]; while ([rs next]) { int Id = [rs intForColumn:@"id"]; NSString * name = [rs stringForColumn:@"name"]; NSString * age = [rs stringForColumn:@"age"]; NSLog(@"id = %d, name = %@, age = %@ ", Id, name, age); }
iOS sqlite資料庫使用