@synthesize db;
@synthesize dbName;
-(void)openAndCreate
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"test.db"];
db = [FMDatabase databaseWithPath:dbPath];
if(![db open])
{
NSLog(@"不能打开sqlite");
}
}
-(void)openDatabase
{
[db setShouldCacheStatements:YES];
}
-(void)createTable
{
[db executeUpdate:@"CREATE TABLE User (Name text,Age integer)"];
}
-(void)insertTable
{
[db executeUpdate:@"INSERT INTO User (Name,Age) VALUES (?,?)",@"李四",[NSNumber numberWithInt:20]];
}
-(void)showTable
{
FMResultSet *rs=[db executeQuery:@"SELECT * FROM User"];
//NSString *aa=[db stringForQuery:@"SELECT Name FROM User WHERE Age = ?",@"20"];
rs=[db executeQuery:@"SELECT * FROM User WHERE Age = ?",@"20"];
while ([rs next]){
NSLog(@"%@ %@",[rs stringForColumn:@"Name"],[rs stringForColumn:@"Age"]);
}
[rs close];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self openAndCreate]; //创建数据库要慎重,创建前要判断是否已有该名字的数据库?貌似没有覆盖啊?再查查
//[self createTable];
//[self insertTable];
[self showTable];
// Do any additional setup after loading the view, typically from a nib.
}