iOS sqlite数据库使用
关于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); }