参考:http://wenku.baidu.com/link?url=TK90OOhfVLK_2N5ZalNS5-hB_a5_Bxb7WLCx5ryzfOxHkCw8mW7tMAe63s-lpP0gzh7sytZ8cYuWabYbjMLPlxkm9cbZiphxE1sVDHbgMBa
依赖库: libsqlte3.0
//
// ViewController.m
// FMDBDemo
// FMDB有三个主要的类
// FMDatabase一个FMDatabase对象就代表一个单独的SQLite数据库 用来执行SQL语句
// FMResultSet使用FMDatabase执行查询后的结果集
// FMDatabaseQueue用于在多线程中执行多个查询或更新,它是线程安全的
// Created by Jason on 15-2-16.
// Copyright (c) 2015年 eshore. All rights reserved.
//
static const char encodingChar[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#import "ViewController.h"
@interface ViewController (){
NSString *__dataStr;
NSData *__imageData;
NSArray *__dataAry;
NSString *__imageStr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
__dataStr = @"字符串";
__dataAry = @[@"1",@"2"];
__imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://d.hiphotos.baidu.com/zhidao/pic/item/962bd40735fae6cd0009f9410eb30f2442a70f54.jpg"]];
NSLog(@"===%@",__imageData);
//第一种操作
[self firDbUse];
//第二种操作
[self serMyDbUse];
}
/**
* 第一种获取队列读取数据库方法 多线程 可以获取id
* 创表和插入在队列里:FMDatabaseQueue
*
*/
-(void)firDbUse
{
//Documents 路径
NSString *documenPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//db路径
NSString *dbPath = [documenPath stringByAppendingString:@"mydbFile.db"];
//获取数据库实例
FMDatabaseQueue *dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
// [_db executeUpdate:@"create table if not exists USER(id integer primary key autoincrement,name,score,image)"];
//1.创建表 CREATE TABLE
[dbQueue inDatabase:^(FMDatabase *db) {
BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_person (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];
if (result) {
NSLog(@"创表成功");
}else
{
NSLog(@"创表失败");
}
}];
//2.插入数据 INSERT INTO
[dbQueue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO t_person (name, age) VALUES (?, ?);",@"wendingding", @22];
[db commit];
}];
//查询数据
[dbQueue inDatabase:^(FMDatabase *db) {
// 3.执行查询语句
FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM t_person"];
// 4.遍历结果
while ([resultSet next]) {
int ID = [resultSet intForColumn:@"id"];
NSString *name = [resultSet stringForColumn:@"name"];
int age = [resultSet intForColumn:@"age"];
NSLog(@"第一种:%d %@ %d", ID, name, age);
}
}];
[dbQueue close];
}
/**
* 第二种获取队列读取数据库方法
*
*/
-(void)serMyDbUse
{
//Documents 路径
NSString *documenPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//db路径
NSString *dbPath = [documenPath stringByAppendingString:@"serDBFile.db"];
//获取数据库实例
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
//1.创建表 CREATE TABLE
[db open];
// [db executeUpdate:@"CREATE TABLE User1 (Name text,Age text)"];
[db executeUpdate:@"CREATE TABLE MyDBb1 (Name text,Age)"];
[db executeUpdate:@"create table if not exists User1(id integer primary key autoincrement,name,age,image)"];
//2.插入数据 INSERT INTO
[db executeUpdate:@"INSERT INTO User1 (name, age) VALUES (?, ?);",@"字符111", __dataStr];
[db executeUpdate:@"INSERT INTO User1 (name, age) VALUES (?, ?);",@"字符222", @"字符串2222"];
[db executeUpdate:@"INSERT INTO MyDBb1 (name, age) VALUES (?, ?);",@"图片", __imageData];
//3.替换更新数据 将“Jason”更改为“李四”
[db executeUpdate:@"UPDATE MyDBb SET Name = ? WHERE Name = ? ",@"李四",@"Jason"];
//4.删除数据
// [db executeUpdate:@"DELETE FROM User WHERE Name = ?",@"张三"];
//5.查询数据
//执行查询语句
FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM MyDBb1"];
//遍历结果
while ([resultSet next]) {
int ID = [resultSet intForColumn:@"id"];
NSString *name = [resultSet stringForColumn:@"name"];
NSData *age = [resultSet dataForColumn:@"age"];
NSLog(@"第二种1:%d %@ %@", ID, name, age);
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 150, 150)];
imageV.image = [UIImage imageWithData:age];
imageV.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageV];
}
FMResultSet *resultSet1 = [db executeQuery:@"SELECT * FROM User1"];
//遍历结果
while ([resultSet1 next]) {
int ID = [resultSet1 intForColumn:@"id"];
NSString *name = [resultSet1 stringForColumn:@"name"];
NSString *age = [resultSet1 stringForColumn:@"age"];
NSLog(@"第二种2:%d %@ %@", ID, name, age);
}
}
@end