FMDB -- SQLite( 自封装的DataBaseManager )
SQLite是一种小型的,专们应用在嵌入式中的数据库。 数据库语句都是一样的,在这里,复习一些基本的语句。
----------------------------------------- 复习SQL基本知识 ----------------------------------------- 创建数据库 create database if not exists 数据库名 ; //不存在则创建该数据库
创建表 create table if not exists 表名 (id integer primary key autoincrement(not null),name,intro);//不存在才创建该表 /** 1:一个数据表中,可以多个唯一键,但是主键只能有一个,且主键的值不能为空。唯一键的值可以为空。 2:主键 自增,只能使用integer的类型。 */
在某表中: 增 insert into 表名(字段1,字段2,……)values (值1,值2,……); 删 1: delete * from 表名 //删除该表中的所有数据 2: delete from 表名 where (条件判断)//删除符合条件的一行数据 改 update 表名 set 字段名=新值 (修改多个字段,中间用逗号隔开)where (条件) 查 1:select * from 表名 //所有该表中所有的数据 2:select 字段1,字段2……from 表名 where (条件)//搜索该表中这几个字段 3:select 字段1,字段2…… into #table from 表名 where (条件)//搜索该表中这几个字段,然后将信息保存到临时表#table中,临时表存在于内存中。
SQL中的数据类型
integer:整型数据,大小为4个字节。 bigint:整型数据,大小为8个字节。 smallint:整型数据,大小为2个字节。 tinyint:正整型数据,大小为1个字节。 float:浮点数据,大小为4个字节。 double:浮点数据,大小为8个字节。 real:浮点数据,大小8个字节。 char(n):非变长字符串,不超过n个字符(n < 255) //不够n,仍然存储n个字节 varchar(n):变长字符串,不超过n个字符(n <= 4000)//最常用 存储空间,随字符串到小改变 text:变长非unicode字符串,存储超大数据(n <= 2^31-1) date:日期数据,年份-月份-日期 time:时间数据,时:分:秒 datetime:日期时间数据,年份-月份-日期 时:分:秒 timestamp:日期时间数据,年份-月份-日期 时:分:秒 毫秒 【注】字符串和日期时间,需要加入单引号,日期必须严格按照格式,如14年6月2日,必须写作2014-06-02,不能写作14-6-2,时间也一样需要补零。 where语句的筛选运算符
= 或 == 或 like 等于 > 大于 < 小于 >= 大于等于 <= 小于等于 <> 不等于 !> 不大于 !< 不小于 %舒% 包含子字符串"舒"
---------------------------------------------------------------------------- 在iOS开发中,使用第三库,FMDB来帮助我们使用SQLite。 ----------------------------------------------------------------------------
1:首先需要手动导入:libsqlite3.dylib 2: 包含头文件:#import"FMDatabase.h" 3:创建SQLite数据库到沙盒目录 //数据库 存在沙盒目录的Documents NSString *path=[NSStringstringWithFormat:@"%@/Documents/data.db",NSHomeDirectory()];
//数据库操作类 FMDatabase*_db=[[FMDatabasealloc]initWithPath:path];//实例化数据库操作对象,回到指定的路径中去操作该数据库,如果没有,则自动创建该数据库。 4:打开数据库 BOOL res=[self.dbopen]; if (!res) { NSLog(@"数据库打开失败"); return; } 5:进行SQL 有两种: a 不带结果的SQL语句,使用 executeUpdate 执行。 例如: //创建表 res=[self.dbexecuteUpdate:@"create table if not exists USER(uid integer primary key autoincrement,name,intro,image)"]; //插入数据BOOL res=[self:@"insert into USER(name,image)values(?,?,?)",selfnameLabeltextintroLabel//删除数据 NSString *delStr=@"delete from USER WHERE uid=3"; BOOL res=[self.dbexecuteUpdate:delStr]; //更新数据 NSString *sql=@"update USER SET name='诸葛亮',intro='蜀国丞相' where uid=2"; self.dbexecuteUpdate:sql]; b:带有返回结果的SQL语句,使用 executeQuery 来执行,它返回的是一个结果集,结果集就是在内存中存在的一张表格,类似临时表。 //查询所有数据 FMResultSet *resultSet=[self.dbexecuteQuery:@"select * from USER"]; if (resultSet) { 遍历结果集 while ([resultSetnext]) { int uid=[resultSetintForColumn:@"uid"]; NSString *name = [resultSetstringForColumn:@"name"];
NSString *intro = [resultSetstringForColumn:@"intro"]; NSLog(@"id=%d,name=%@,intro=%@",uid,intro); // double score = [resultSet doubleForColumn:@"score"];
} } ----------------------------- ------ 在FMDB 上 再次封装 下载地址:http://download.csdn.net/detail/u010165653/8315195 ------------------------------------ 使用方法: 导入文件, 注意:使用增、删、改、查 之前必须打开数据库 //01 创建 数据库
NSString *path=[NSStringstringWithFormat:@"%@/Documents/testDB.db",NSHomeDirectory()]; NSLog(@"SQL Addr:%@",path); _dbManager=[[AZDataBaseManageralloc] initWithPath:path];
//打开数据库 [_dbManager open];
//02 创建表 NSDictionary *dic=@{@"name":@"varchar(100)":@"varchar(10)":@"varchar(20)"};
//主键类型的值为:为“integer”或则 “INTEGER” ,主键为自增 [_dbManager createTableWithName:@"user"primaryKey:@"uid"type:@"integer"otherColumn:dic]; #if 0 //03 增 NSDictionary *insertDic1=@{@"name":张三",@"sex":男@"age":@"33"}; [_dbManager insertRecordWithColumns:insertDic1 toTable:@"user"];
NSDictionary *insertDic2=@{李小燕女@"23"}; [_dbManager insertRecordWithColumns:insertDic2 toTable:@"user"];
NSDictionary *insertDic3=@{王东@"35"}; [_dbManager insertRecordWithColumns:insertDic3 toTable:@"user"];
NSDictionary *insertDic4=@{老张@"32"}; [_dbManager insertRecordWithColumns:insertDic4 toTable:@"user"]; #endif
#if 0 删
//全部删除 [_dbManager removeRecordWithCondition:nil fromTable:@"user"];
#endif
//条件删除
[_dbManager removeRecordWithCondition:@"where age='23'"fromTable:@"user"];
改
NSDictionary *updataDic=@{@"31"}; [_dbManager updataRecordWithColumns:updataDic Condition:@"where age='35' "toTable:@"user"];
查 #if 0 //全部查询 FMResultSet *rs=[_dbManager findColumnNames:nil recordsWithCondition:@"user"]; while (rs.next) { NSLog(@"%d--%@--%@--%@",[rs intForColumn:@"uid"],[rs stringForColumnIndex:1],[rs stringForColumnIndex:2],[rs stringForColumn:@"age"]); } #endif
//部分查询 NSArray *ary=@[@"sex",26)">@"name"];
FMResultSet *rest=[_dbManagerfindColumnNames:ary recordsWithCondition:@"where age='31'"fromTable:@"user"];
while (rest.next) { NSLog(@"%@--%@",[reststringForColumn:@"sex"],[reststringForColumnIndex:1]); }
//关闭数据库 [_dbManager close]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |