简介
- iOS平台下sqlLite数据库的框架,以OC对sqlLite的C语言API进行了封装。
- 面向对象,灵活,线程安全
使用
- 去github地址地址进行下载源代码
- 注意点
- 在iOS中倒入libsqlite3库。
- 可以使用;也可以不加。
- 在移动开发中一般不建议关闭数据库操作
[self.db close]
,因为每次打开都很耗费性能,而且随着应用的关闭,数据库也自然关闭。
- 创建队列后会对数据库进行加锁
- 书写代码。
//存储数据库的路径
NSString *mainPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [mainPath stringByAppendingPathComponent:@"first.sql"];
self.dataBase = [FMDatabase databaseWithPath:filePath];
//对数据库进行操作
if (![self.dataBase open]) {
NSLog(@"数据库打开失败");
}else{
NSLog(@"数据库打开成功");
BOOL result = [self.dataBase executeUpdate:@"CREATE TABLE IF NOT EXISTS t_students ( idd integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL)"];
if (result) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self insertData];
[self deleteData];
[self query];
}
- (void)insertData{
for (int i = 0; i < 10; i ++) {
NSString *sting = [NSString stringWithFormat:@"zyan%d",i];
[self.dataBase executeUpdate:@"INSERT INTO t_students (id,name,age) VALUES (%ld,%@,%d);", i, sting , arc4random_uniform(10)];
// [self.dataBase executeUpdate:@"INSERT INTO t_students (id,name, age) VALUES (?, ?, ?);", @(i), sting,@(arc4random_uniform(40))];
}
}
- (void)deleteData{
[self.dataBase executeUpdate:@"DELETE * FROM t_students"];
}
- (void)query{
FMResultSet *set = [self.dataBase executeQuery:@"SELECT * FROM t_students"];
while ([set next]) {
int number = [set intForColumn:@"id"];
NSString *string = [set stringForColumn:@"name"];
NSLog(@"%d,%@",number,string);
}
}
@interface ViewController ()
/**
* 数据库操作对象队列
*/
@property (strong, nonatomic) FMDatabaseQueue *dataQueue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//存储数据库的路径
NSString *mainPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [mainPath stringByAppendingPathComponent:@"first.sql"];
//创建数据库操作对象队列
self.dataQueue = [FMDatabaseQueue databaseQueueWithPath:filePath];
[self.dataQueue inDatabase:^(FMDatabase *db) {
BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_students ( idd integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL)"];
if (result) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
// Do any additional setup after loading the view, typically from a nib.
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self insertData];
// [self deleteData];
[self query];
}
- (void)insertData{
for (int i = 0; i < 10; i ++) {
NSString *sting = [NSString stringWithFormat:@"zyan%d",i];
[self.dataQueue inDatabase:^(FMDatabase *db) {
// [db executeUpdate:@"INSERT INTO t_students (id,name,age) VALUES (%ld,%@,%d);", i, sting , arc4random_uniform(10)];
[db executeUpdate:@"INSERT INTO t_students (idd,name, age) VALUES (?, ?, ?);", @(i), sting,@(arc4random_uniform(40))];
}];
}
}
- (void)deleteData{
[self.dataQueue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"DELETE * FROM t_students"];
}];
}
- (void)query{
[self.dataQueue inDatabase:^(FMDatabase *db) {
FMResultSet *set = [db executeQuery:@"SELECT * FROM t_students"];
while ([set next]) {
int number = [set intForColumn:@"idd"];
NSString *string = [set stringForColumn:@"name"];
NSLog(@"%d,%@",number,string);
}
}];
}
网友评论