在使用之前需要手动导入sqlite3系统框架
使用的时候一般就是增删改查
先是基本的数据库的使用
创建数据库
sqlite3 *db;
创建数据库文件的位置
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *dbPath = [documentPath stringByAppendingPathComponent:@"test.sqlite"];
//转化为C的字符串const char *p = [dbPath UTF8String];
// 创建打开数据库
int res = sqlite3_open(p, &db);
// 判断数据库是否打开成功
char *errar;
if (res == SQLITE_OK) {
NSLog(@"数据库成功打开");
// 创建表格
NSString *creatTableSqlite = @"create table if not exists student (stu_id integer primary key autoincrement not null ,stu_name vachar(20) not null)";
// 参数1:sqlite3的对象
// 参数2:sql语句,C的
// 参数3:回调函数C
// 参数4:回调函数的参数
// 参数5:错误信息,可以用char类型的指针接收检查是否有错
int c = sqlite3_exec(db, [creatTableSqlite UTF8String], NULL, NULL, &errar);
if (c == SQLITE_OK) {
NSLog(@"表格成功创建");
}
}
插入数据
NSString *insert = @"insert into student (stu_name) values ('renshizheng')";
if (sqlite3_exec(db, [insert UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"插入成功");
}
删除数据
NSString *remove = @"delete from student where stu_id = 1";
if (sqlite3_exec(db, [remove UTF8String], NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"删除成功");
}
// 参数1:sqlite对象
// 参数2:查询语句C
// 参数3:查询语句长度 通常为—1,自动定义长度 strlength()来计算也行
// 参数4:sqlite3_stme对象
// 参数5:未执行的sql语句
// 准备执行
sqlite3_stmt *stmt1; //存放结果集
// 用问号作占位符来拼接
int selectStuId = 5;
NSString *select1 = @"select * from student where stu_id<?";
if (sqlite3_prepare(db, [select1 UTF8String], -1, &stmt1, NULL)== SQLITE_OK) {
// 绑定参数
// 参数1:结果集的对象
// 参数2:占位符的索引 就是第几个问号,从1开始
// 参数3:真是数据 可以有多个?,然后需要依次绑定
sqlite3_bind_int(stmt1, 1, selectStuId);
// 遍历结果集
// 执行,一行行的查找
while (sqlite3_step(stmt1) == SQLITE_ROW) {
// 按照当前列的数据类型取数据,列号从0开始
int stu_id = sqlite3_column_int(stmt1, 0);
const unsigned char *stu_name = sqlite3_column_text(stmt1, 1);
NSLog(@"%i %@",stu_id,[NSString stringWithUTF8String:(char *)stu_name]);
}
// 释放掉结果集
sqlite3_finalize(stmt1);
}
// 关闭数据库
sqlite3_close(db);
FMDB有三个主要的类
1、FMDatabasE
一个FMDatabase对象就代表一个单独的SQLite数据库
用来执行SQL语句
2、FMResultSet
使用FMDatabase执行查询后的结果集
3、FMDatabaseQueue
用于在多线程中执行多个查询或更新,它是线程安全的
文件路径有三种情况
(1)具体文件路径
如果不存在会自动创建
(2)空字符串@""
会在临时目录创建一个空的数据库
当FMDatabase连接关闭时,数据库文件也被删除
(3)nil
会创建一个内存中临时数据库,当FMDatabase连接关闭时,数据库会被销毁
FMDatabase *db = [FMDatabase databaseWithPath:[self getFilePathWithFileName:@"Test.sqlite"]];
if ([db open]) {
NSLog(@"打开成功");
}
NSString *sql = @"create table if not exists student (stu_name varchar(20) not null ,stu_ID integer primary key autoincrement, stu_age integer)";
if ([db executeUpdate:sql]) {
NSLog(@"创建成功");
}
插入
1 executeUpdate:不确定的可以用?做占位符,但必须是OC对象
NSString *sql1 = @"insert into student (stu_name,stu_age) values (?,?)";
if ([_db executeUpdate:sql1,name,@(age)]) {
NSLog(@"插入成功");
}
2 executeUpdateWithFormat:用原始的数据类型来占位
NSString *sql1 = @"insert into student (stu_name,stu_age) values (%@,%i)";
[_db executeUpdateWithFormat:sql1,name,age];
3 不确定的参数用?,把参数放到数组里
NSString *sql1 = @"insert into student (stu_name,stu_age) values (?,?)";
[_db executeUpdate:sql1 withArgumentsInArray:@[name,@(age
)]];
删除
NSString *sql = @"delete from student where stu_ID=?";
[_db executeUpdate:sql,@(1)];
1、执行查询语句
FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM t_student"];
2、遍历结果 while ([resultSet next]) { int ID = [resultSet intForColumn:@"id"]; NSString *name = [resultSet stringForColumn:@"name"]; int age = [resultSet intForColumn:@"age"]; NSLog(@"%d %@ %d", ID, name, age); }