需要的头文件和类库
import
import “FMDB.h”
libsqlite3.0.tdb(Xcode7.0之后dylib变为tdb)
// 获取数据库路径 ,有数据库就打开,没有就创建
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"Student.sqlite"];
NSLog(@"%@",dbPath);
// 得到数据库路径后初始化FMDB(声明个属性方便使用 @property (nonatomic, strong) FMDatabase * database;)
self.database = [FMDatabase databaseWithPath:dbPath];
// 创建表
[self.database open];
// executeUpdate:@"create table 表名 (列名 类型,..... )" 对数据库操作表名要保持一致
[self.database executeUpdate:@"create table user (name text, age integer, myData blob)"];
[self.database close];
// 用FMDB对数据库操作就是FMDB的对象调用方法再加常用的SQLite语句
// 增加数据 FMDB方法:
- (IBAction)insert:(id)sender {
[self.database open];
// 写一个数组
NSMutableArray * array = [[NSMutableArray alloc] init];
[oneArr addObject:@"111"];
[oneArr addObject:@"222"];
// 把数组转为data
NSData * arrayData = [NSKeyedArchiver archivedDataWithRootObject:array];
// FMDB方法:executeQuery
// SQLite语法:insert into 表名(列名) values(?)
FMResultSet *rs = [self.database executeQuery:@"insert into user(name,age,myData) values(?,?,?)" withArgumentsInArray:@[@"张三",@18,arrayData]];
或者写成FMResultSet *rs = [self.database executeQuery:@"insert into user(name,age,myData) values(?,?,?)", @"张三", @18, arrayData];
// 把数据循环加入到数据库中
[rs next];
[self.database close];
}
// 修改数据
- (IBAction)update:(id)sender {
[self.database open];
NSString * newName = @"李四";
NSString * oldName = @"张三";
// FMDB方法:executeUpdateWithFormat
//SQLite语法:update 表名 set 新的参数 = 类型 where 旧的参数 = 类型
[self.database executeUpdateWithFormat:@"update user set name = %@ where name = %@",newName,oldName];
NSLog(@"修改数据成功");
[self.database close];
}
// 删除数据
- (IBAction)delete:(id)sender {
[self.database open];
// 删除所有
// FMDB方法:executeUpdateWithFormat
// SQLite语法: delete from 表名
[self.database executeUpdateWithFormat:@"delete from user"];
// 按条件删除
// FMDB方法:executeUpdateWithFormat
// SQLite语法: delete from 表名 where 条件
// NSString * name = @"张三";
// [self.database executeUpdateWithFormat:@"delete from user where name = %@",name];
[self.database close];
}
// 查询
- (IBAction)select:(id)sender {
[self.database open];
// 按条件查询
// FMDB方法:intForQuery(根据返回值类型来写)
// SQLite语法: select 要返回的列名 from 数据库名 where 查询的列名 = ?",@"查询条件"
// NSInteger age = [self.database intForQuery:@"select age from user where name = ?",@"张三"];
//查询整个表
// FMDB方法:executeQuery
// SQLite语法: select * from 表名
FMResultSet * resultSet = [self.database executeQuery:@"select * from user"];
//[resultSet next]遍历条件 遍历前为YES 遍历结束为NO
while ([resultSet next] == YES)
{
// 用[resultSet stringForColumn:]或[resultSet intForColumn:]等方法把值转成Object-C的型态
NSString * name = [resultSet stringForColumn:@"name"];
NSInteger age = [resultSet longLongIntForColumn:@"age"];
NSData *data = [resultSet dataForColumn:@"myData"];
//data转为数组
NSMutableArray *testArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
// 打印数据
NSLog(@"name = %@, age = %ld,myData = %@",name, age, testArr);
}
[self.database close];
}