sqlite判断表字段是否存在

yinqi025 2015-09-17 02:19:25
需求:
判断是否存在该字段 如果存在就不更新 如果存在更新表字段
ALTER TABLE 表 add column 新列.....





参考sqlite更新表
create table if not exists BillPay_waimai (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"order_num" NVARCHAR(50),
"outer_order_id" NVARCHAR(50),
"outer_support_id" int(5),
"order_seq" INTEGER,
"start_deliverytime" NVARCHAR(50),
"end_deliverytime" NVARCHAR(50),
"is_print" int(5),
"order_status" int(5),
"total_pay" NVARCHAR(50),
"json_result" TEXT,
"Creation_time" datetime);
...全文
166 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
前言之前是一直做web开发,对于做web开发的人而言一定熟悉各种ORM,各种语言针对mysql的ORM有很多,比如PHP的各类框架yii,thinkphp,laravel,ruby语言的rails, GO语言的beego等,IOS开发则面对的数据库是sqlite。FMDB 是基于sqlite的数据库操作类,稳定,但用起来还是不够简洁,PYFMDB是基于FMDB的更高层次的数据库操作类。程序介绍 PYFMDB分为三部分,PYFMDB 基于FMDB负责数据库底层操作处理,PYTable是自定义Table的基类,提供基于具体数据库的操作,是更高层次的封装PYFMDB,PYStructure是定义数据库结构处理类。快速入门导入PYFMDB你可以在 Podfile 中加入下面一行代码来使用PYFMDBpod 'PYFMDB'创建自定义Table类创建一个Table类继承PYTable,例如演示代码中创建了CarTable类。设置数据库名在CarTable.m 中 重写如下方法:-(NSString *)tableName{     return @"car"; }设置数据库结构    在CarTable.m 中 重写如下方法:-(PYStructure *)structure{     PYStructure * st = [[PYStructure alloc] init];     [st addWithField:@"id" andType:PYStructureTypeAutoInc];     [st addWithField:@"name" andType:PYStructureTypeNormalText];     [st addWithField:@"wheels" andType:PYStructureTypeNormalInt];     return st; }PYStructureType PYStructureTypeAutoInc = 0,//主键,int类型,自增 PYStructureTypePrimaryInt = 1,//主键,int类型,自增 PYStructureTypePrimaryText = 2,//主键,text类型 PYStructureTypeNormalInt = 3,//普通列,int类型 PYStructureTypeNormalText = 4,//普通列,text类型CarTable *table = [[CarTable alloc] init];新增数据普通新增数据NSDictionary *fields = @{@"name":@"宝马",@"wheels":@1};  [table addFields:fields];新增或者更新数据【判断数据是否已存在,存在则更新数据,不存在则新增数据】NSDictionary *fields = @{@"name":@"宝马",@"wheels":@1}; [table addOrUpdateFields:fields andWhere:@"name='\u5b9d\u9a6c'"];判断是否已经存在数据,仅不存在数据时更新数据NSDictionary *fields = @{@"name":@"宝马",@"wheels":@1}; [table addFieldsIfNotExist:fields];删除数据指定字段删除NSString *where = @"name='\u5b9d\u9a6c'"; [table deleteWithWhere:where];多种条件删除NSString *where = @"name='\u5b9d\u9a6c' and id >=5"; [table deleteWithWhere:where];清空数据[table truncate];更新数据更新多个字段NSString *where = @"name='\u5b9d\u9a6c'"; NSDictionary *fields = @{@"name":@"奔驰",@"wheels":@2}; [table updateFields:fields andWhere:where];更新1个字段[table setField:@"name" andValue:@"奔驰" andWhere:@"name='\u5b9d\u9a6c'"];查询数据查询全部数据,全部字段,返回的结果为NSArrayNSArray *results = [table selectAll];按条件查询数据,全部字段,返回的结果为NSArrayNSString *where = @"name='\u5b9d\u9a6c'"; NSArray *results = [table selectWithWhere:where];按条件查询数据,指定字段,返回结果为NSArray 多个字段用半角逗号隔开NSString *where = @"name='\u5b9d\u9a6c'"; NSString *fields = @"id,wheels"; NSArray *results = [table selectWithWhere:where andFields:fields];按条件查询数据,指定字段,设置分页,返回结果为NSArray 要查询全部字段时 用 * 代查询全部字段NSString *where = @"name='\u5b9d\u9a6c'"; NSString *fields = @"id,wheels"; //NSString *fields = @"*"; NSArray *results = [table selectWithWhere:where andFields:fields andPage:1 andPageSize:10];//取第一页,每页10条按条件查询数据,指定字段,设置分页,设置排序,返回结果为NSArray 排序中 desc 代 降序,asc代升序 单个字段排序 如 id desc 多个字段排序 如 id,wheel ascNSString *where = @"name='\u5b9d\u9a6c'"; NSString *fields = @"id,wheels"; //NSString *fields = @"*"; NSArray *results = [table selectWithWhere:where andFields:fields andPage:1 andPageSize:10 andOrder:@"id desc"];按条件查询单行数据,返回结果为NSDictionaryNSString *where = @"name='\u5b9d\u9a6c'"; NSDictionary *result = [table findWithWhere:where];按条件查询单行单个字段数据,返回结果为id类型id result = [table getField:@"name" andWhere:@"id=1"];NSUInteger tableCount = [table count];判断是否为空if([table isEmpty]){         //table is empty  }判断数据是否存在NSDictionary *fields = @{@"name":@"宝马",@"wheels":@1};     if([table hasFields:fields]){         //数据已存在     }判断where查询是否有数据if([table hasWhere:@"name='\u5b9d\u9a6c'"]){         //有查询结果     }原生sql支持执行一个sql查询NSString *sql = @"select * from car"; NSArray *results = [table executeQueryWithSql:sql];执行一个sql操作,如更新,删除,插入等NSString *sql = @"delete from car where name='BMW'"; BOOL result = [table executeUpdateWithSql:sql];调试信息NSLog(@"dbpath:%@",table.databasePath);//数据库位置 NSLog(@"lastSql:%@",table.lastSql);//最后执行的sql NSLog(@"dbname:%@",table.databaseName);//数据库名 NSLog(@"tablename:%@",table.tableName);//数据名 NSLog(@"table structure:%@",table.structure.structureDictory);//数据结构 NSLog(@"table fields:%@",table.structure.fieldsString);//数据字段 标签:PYFMDB

2,209

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 其他数据库
社区管理员
  • 其他数据库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧