-(void)createTable:(NSString*)tableName{FMDatabase*db=[selfdbConnect:@"test.db"];// tableの作成NSString*sql=[[NSStringalloc]initWithFormat:@"create table if not exists %@ (id INTEGER PRIMARY KEY, uid TEXT, createtime TEXT)",tableName];[dbopen];[dbexecuteUpdate:sql];[dbclose];}
select文の実行
12345678910111213141516171819
-(void)selectData:(NSString*)tableName{FMDatabase*db=[selfdbConnect:@"test.db"];// select文NSString*sql=[[NSStringalloc]initWithFormat:@"select * from %@",tableName];[dbopen];FMResultSet*rs=[dbexecuteQuery:sql];while([rsnext]){NSIntegeridNum=[[rsstringForColumnIndex:0]intValue];NSString*uid=[rsstringForColumnIndex:1];NSString*createtime=[rsstringForColumnIndex:2];NSLog(@"id: %ld, uid: %@, createtime: %@",(long)idNum,uid,createtime);}[dbclose];}
insert文の実行
12345678910111213
-(void)insertData:(NSString*)tableNameuserID:(NSString*)uidcreatetime:(NSString*)createtime{FMDatabase*db=[selfdbConnect:@"test.db"];// insert文NSString*sql=[[NSStringalloc]initWithFormat:@"insert into %@ (uid, createtime) values('%@', '%@')",tableName,uid,createtime];[dbopen];[dbexecuteUpdate:sql];[dbclose];}
update文の実行
12345678910111213
-(void)updateData:(NSString*)tableNameuserID:(NSString*)uidcreatetime:(NSString*)createtime{FMDatabase*db=[selfdbConnect:@"test.db"];// update文NSString*sql=[[NSStringalloc]initWithFormat:@"update %@ set uid='%@', createtime='%@' where id=1",tableName,uid,createtime];[dbopen];[dbexecuteUpdate:sql];[dbclose];}
delete文の実行
1234567891011
-(void)deleteData:(NSString*)tableName{FMDatabase*db=[selfdbConnect:@"test.db"];// delete文NSString*sql=[[NSStringalloc]initWithFormat:@"delete from %@",tableName];[dbopen];[dbexecuteUpdate:sql];[dbclose];}
count文の実行
1234567891011121314151617
-(NSInteger)countData:(NSString*)tableName{FMDatabase*db=[selfdbConnect:@"test.db"];// count文NSString*sql=[[NSStringalloc]initWithFormat:@"select count(*) as count from %@",tableName];[dbopen];FMResultSet*rs=[dbexecuteQuery:sql];NSIntegercnt=0;if([rsnext]){cnt=[rsintForColumn:@"count"];}[dbclose];returncnt;}