#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *pathesArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathesArr firstObject];
NSString *dbPath = [path stringByAppendingPathComponent:@"sqlite.db"];
NSLog(@"%@",dbPath);
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
[queue inDatabase:^(FMDatabase *db) {
FMResultSet *set = [db executeQuery:@"select id from myTable where name = 'wangbing';"];
while ([set next]) {
int int_id = [set intForColumn:@"id"];
NSLog(@"%zi",int_id);
}
[set close];
[db close];
}];
[queue close];
}
- (void)dbByDatabase{
NSArray *pathesArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathesArr firstObject];
NSString *dbPath = [path stringByAppendingPathComponent:@"sqlite.db"];
NSLog(@"%@",dbPath);
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
if ([database open]) {
if ([database executeUpdate:@"create table if not exists 'myTable' (id int,name varchar(255),height float, primary key (id)) ;"]) {
NSLog(@"建表成功");
FMResultSet *set = [database executeQuery:@"select id from myTable where name = 'wangshuo';"];
while ([set next]) {
int int_id = [set intForColumn:@"id"];
NSLog(@"%zi",int_id);
}
[set close];
};
}
[database close];
}