NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *pathDocument = [document stringByAppendingPathComponent:@"/arr.sqlite"];
NSLog(@"%@", pathDocument);
FMDatabase *db = [FMDatabase databaseWithPath:pathDocument];
[db open];
// 建立了一个名為PersonList的table,裡面有姓名、年龄、性别、电话、地址和照片
if ([db open]) {
NSString *sqlcreateTable = [NSString stringWithFormat:@"create table if not exists personList(name text, sex text)"];
BOOL res = [db executeUpdate:sqlcreateTable];
if (!res) {
NSLog(@"创建失败");
}
else{
NSLog(@"创建成功");
}
}
// 插入数据
if ([db open]) {
NSString *insertString = [NSString stringWithFormat:@"insert into personList(name, sex) values ('%@', '%@')", @"ZZP", @"male"];
BOOL res = [db executeUpdate:insertString];
if (res) {
NSLog(@"插入成功");
} else {
NSLog(@"插入失败");
}
}
// 查询操作
if ([db open]) {
NSString *sqlString = @"select * from personList";
BOOL res = [db executeQuery:sqlString];
if (res) {
FMResultSet *set = [db executeQuery:sqlString];
if ([set next]) {
NSLog(@"%@", [set objectForColumnName:@"name"]);
}
}else {
NSLog(@"查询失败");
}
}
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:@"delete from personList where name = '%@'", @"ZZP"];
BOOL res = [db executeUpdate:deleteSql];
if (res) {
NSLog(@"删除成功");
}
}
[db close];