第一步:插入libsqlite3.tbd
插入libsqlite3.tbd第二步:viewController.m全部代码
//
// ViewController.m
// FMDataBase数据库
//
// Created by qianfeng on 16/9/25.
// Copyright © 2016年 qianfeng. All rights reserved.
//
#import "ViewController.h"
#import "FMDB.h"
@interface ViewController ()
@end
/*
FMDB的简单使用:
*/
@implementation ViewController
/*
NSSearchPathForDirectoriesInDomains:查找路径,在(cache,user)下载
NSCachesDirectory:library 的caches 快速存储目录路径
NSUserDomainMask:用户安装目录
YES:表示完整路径
*/
-(NSString *)databasePath
{
NSString *cachepath = NSSearchPathForDirectoriesInDomains
(NSCachesDirectory,
NSUserDomainMask, YES)[0];
return [cachepath stringByAppendingPathComponent:@"data.db"];
}
- (void)viewDidLoad {
[super viewDidLoad];
/*
1.创建/打开数据库
*/
FMDatabase *database = [FMDatabase databaseWithPath:
[self databasePath] ];
[database open];
NSLog(@"cachePath == %@",[self databasePath]);
/*
2.创建表
*/
NSString *sql_createTable = @"create table if not exists
book(id integer primary key autoincrement,name text ,price double)";
[database executeUpdate:sql_createTable];
/*
3.插入数据
*/
NSString *sql_insert = @"insert into book (name , price)
values (?, ?)";
for (int i = 0; i < 10;i ++ ){
[database executeUpdate:sql_insert,
[NSString stringWithFormat:@"name%d",i+1], @(20+i)];
}
/*
4.查询
*/
NSString *sql_select = @"select * from book";
FMResultSet *resultSet = [database executeQuery:sql_select];
while (resultSet.next) {
NSString *name = [resultSet stringForColumn:@"name"];
NSLog(@"name == %@",name);
}
}
@end
第三步:运行即可。。。
运行结果|
|
微云网盘:
FMDB简单使用
FMDB二次封装
网友评论