#import "SQLViewController.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#define DATABASE_NAME @"Temp.db"
@implementation SQLViewController
-(id)initInfo:(int)page
if (self = [super init]) {
//注解1
pageID = page;
return self;
- (void)viewDidLoad
[super viewDidLoad];
//注解2
//图片的地址
NSMutableArray *iconurls = [[[NSMutableArray alloc] initWithObjects:
@"bundle://0.jpg",
@"bundle://1.jpg",
@"bundle://2.jpg",
@"bundle://3.jpg",
nil] autorelease];
NSMutableArray *names = [[[NSMutableArray alloc] initWithObjects:
@"雨松MOMO",
@"RORO娃娃",
@"小老虎",
@"哇咔咔",
nil] autorelease];
NSMutableArray *sexs = [[NSMutableArray alloc] initWithObjects:
@"男",
@"女",
@"男",
@"女",
nil];
NSMutableArray *address = [[[NSMutableArray alloc] initWithObjects:
@"北京市朝阳区",
@"北京市西城区",
@"北京市海淀区",
@"北京市东城区",
nil] autorelease];
NSMutableArray *ages = [[[NSMutableArray alloc] initWithObjects:
[NSNumber numberWithInt:28],
[NSNumber numberWithInt:27],
[NSNumber numberWithInt:26],
[NSNumber numberWithInt:25],
nil] autorelease];
//获取单例对象实例
instance = [SingleDate getInstance];
//读取数据库
db = [instance loadDB:DATABASE_NAME];
//结果集
FMResultSet *rs = nil;
//判断当前页面的ID
switch (pageID)
case 0:
//注解3
if(![db tableExists:@"Message"])
//创建数据库
if ([db executeUpdate:@"CREATE TABLE Message (_id INTEGER PRIMARY KEY AUTOINCREMENT,IconUrl text, Name text, Sex text,Address text,Age integer)"])
NSLog(@"创建表成功");
for(int i=0 ;i<[iconurls count]; i++)
//遍历测试数据数组,然后将测试数据写入数据库当中
if([db executeUpdate:@"INSERT INTO Message (IconUrl,Name,Sex,Address,Age) VALUES (?,?,?,?,?)", [iconurls objectAtIndex:i],[names objectAtIndex:i], [sexs objectAtIndex:i],[address objectAtIndex:i],[ages objectAtIndex:i]])
NSLog(@"插入数据成功");
//返回一个结果集
rs=[db executeQuery:@"SELECT * FROM Message"];
break;
case 1:
//判断当数据库中含有Message表时
if([db tableExists:@"Message"])
//注释4
if([db executeUpdate:@"UPDATE Message SET Name = ? WHERE _id = ? ", @"雨松MOMO(已修改)",[NSNumber numberWithInt:1]])
NSLog(@"更新一个数据成功");
if([db executeUpdate:@"UPDATE Message SET Name = ? , Sex = ? , Address = ? WHERE _id = ? ", @"雨松MOMO(已修改)",@"男(已修改)",@"北京市朝阳区(已修改)",[NSNumber numberWithInt:1]])
NSLog(@"更新多条数据成功");
//返回一个结果集
rs=[db executeQuery:@"SELECT * FROM Message"];
break;
case 2:
//判断当数据库中含有Message表时
if([db tableExists:@"Message"])
//删除数据
if([db executeUpdate:@"DELETE FROM Message WHERE _id = ?",[NSNumber numberWithInt:2]])
NSLog(@"删除数据成功");
//返回一个结果集
rs=[db executeQuery:@"SELECT * FROM Message"];
break;
case 3:
//返回一个结果集
rs=[db executeQuery:@"SELECT * FROM Message WHERE _id = ? ",[NSNumber numberWithInt:3]];
break;
default:
break;
NSMutableArray * array = [[[NSMutableArray alloc]init] autorelease];
//注释5
while ([rs next])
[array addObject:[TTTableImageItem itemWithText:[rs stringForColumn:@"Name"]
imageURL:[rs stringForColumn:@"IconUrl"]
URL:[NSString stringWithFormat: @"%@%@", @"tt://MessageView/", [rs stringForColumn:@"_id"]]]];
//用完以后一定要关闭
[rs close];
//设置列表自适应高度
self.variableHeightRows = YES;
//设置列表现实的数据
self.dataSource = [TTSectionedDataSource dataSourceWithArrays:@"当前数据库信息",array,nil];