您的位置:首页 > 数据库

FMDB的使用, Sqlite常用语句

2014-08-08 16:33 246 查看
项目很多要用到数据库,数据库语句有时候容易忘记,所以把常用的一些简单语句写下来,以后忘记了可以直接来博客复制,哈哈

项目中首先加入libsqlite3.dylib这个函式库。

一.实例化FMDatabase

//读取数据库
-(FMDatabase* )loadDB:(NSString *)dbName
{
//localDBPath: 数据库路径,在Document中。
NSString *localDBPath;
localDBPath= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"myTest.sqlite"];
NSLog(@"dbPath:::::%@",localDBPath);

//创建数据库实例 db  这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"
FMDatabase *db= [FMDatabase databaseWithPath:localDBPath];
if (![db open]) {
NSLog(@"数据库未能创建/打开");
return nil;
}

return db;
}


也可以用工具在外部设计好数据库,拖进项目中,项目没找到数据库时,直接把项目中的数据库拷贝到Document中

-(FMDatabase* )loadDB:(NSString *)dbName
{
//localDBPath: 数据库路径,在Document中。
NSString *localDBPath;
localDBPath= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"myTest.sqlite"];
NSLog(@"dbPath:::::%@",localDBPath);

//如果document中没找到数据库,就把项目中的数据库拷贝到document中
if (![[NSFileManager defaultManager] fileExistsAtPath:localDBPath]) {
NSString *bundleDBPath = [[NSBundle mainBundle] pathForResource:@"raoxudong" ofType:@"sqlite"];
[[NSFileManager defaultManager] copyItemAtPath:bundleDBPath toPath:localDBPath error:nil];
}

//创建数据库实例 db  这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"
FMDatabase *db= [FMDatabase databaseWithPath:localDBPath];
if (![db open]) {
NSLog(@"数据库未能创建/打开");
return nil;
}

return db;
}


二.创建表

/*
注意:
在创建table 时设置的字段默认是允许为空的,即Allow Null
*/

//1、创建一个名为table1的表,有三个字段分别为string类型的Name、Age、sex
[db executeUpdate:@"create table if not exists table1 (name text,age text,sex text)"];

//2、创建一个名为table2的表,id字段自增(即:序号)
//not null表示不用需为空值
[db executeUpdate:@"create table if not exists table2 (id integer primary key AutoIncrement not null,name text,age text not null,sex text)"];


三.插入

BOOL bool2 = [db executeUpdate:@"INSERT INTO table1 (name,age,sex)VALUES(?,?,?)",@"name1",@"10",@"男"];
NSLog(@"INSERT %@!",bool2?@"success":@"faile");


四.更新

BOOL bool3 = [db executeUpdate:@"update table1 set age=?",@"12"];
NSLog(@"update %@!",bool3?@"success":@"faile");

BOOL bool4 = [db executeUpdate:@"update table1 set age=? where name=? and sex=?",@"12",@"name1",@"男"];
NSLog(@"update %@!",bool4?@"success":@"faile");


五.删除

//删除数据
BOOL bool5 = [db executeUpdate:@"DELETE FROM table1 WHERE name = ?",@"name1"];
NSLog(@"DELETE %@!",bool5?@"success":@"faile");


六.查询

取得特定的资料,则需使用FMResultSet物件接收传回的内容:

//用[rs next]可以轮询query回来的资料,每一次的next可以得到一个row裡对应的数值,并用[rs stringForColumn:]或[rs intForColumn:]等方法把值转成Object-C的型态。取用完资料后则用[rs close]把结果关闭。
FMResultSet *rs = [db executeQuery:@"SELECT name, age, FROM table1"];
while ([rs next]) {
NSString *name = [rs stringForColumn:@"name"];
int age = [rs intForColumn:@"age"];
}
[rs close]


#define QUERY_key(rs,key) ([rs stringForColumn:key]?[rs stringForColumn:key]:@"")

FMResultSet *rs=[db executeQuery:@"SELECT * FROM table2 where sex=? order by age desc",@"男"];
/*(注: asc 表示升序 , desc表示降序 , 未明确写明排序方式时默认是升序)*/
if (rs != nil &&[rs columnCount]>0) {
while ([rs next]) {

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:QUERY_key(rs, @"name") forKey:@"name"];
[dic setObject:QUERY_key(rs, @"age") forKey:@"age"];
[dic setObject:QUERY_key(rs, @"sex") forKey:@"sex"];

}
}
[db close];
[rs close];


1、查所有演员名字开头叫茱蒂的电影('%' 符号便是 SQL 的万用字符):

select * from film where starring like 'Jodie%';

2、查所有演员名字以茱蒂开头、年份晚于1985年、年份晚的优先列出、最多十笔,只列出电影名称和年份:

select title, year from film where starring like 'Jodie%' and year >= 1985 order by year desc limit 10;

3、有时候我们只想知道数据库一共有多少笔资料:

select count(*) from film;

4、有时候我们只想知道1985年以后的电影有几部:

select count(*) from film where year >= 1985;

七.增加列,即:增加字段

BOOL bool1 = [db executeUpdate:@"alter table table2 add number2 text"];
NSLog(@"add %@!",bool1?@"success":@"faile");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: