【发布时间】:2012-12-10 09:06:57
【问题描述】:
我在模拟器中运行一个 iOS 程序,我收到了这个错误
'ISDatabaseSQLiteException',原因:'执行语句失败:'create table GroceryItem(primaryKey integer primary key autoincrement, name text NOT NULL, number integer NOT NULL)' with message: table GroceryItem already exists'
这是我在 AppDelegate.m 中使用的代码
self.database = [[[ISDatabase alloc] initWithFileName:@"db20121207.sqlite"] autorelease];
if(![[database tableNames] containsObject:@"GroceryItem"])
{
[database executeSql:@"create table GroceryItem(primaryKey integer primary key autoincrement, name text NOT NULL, number integer NOT NULL)"];
[database executeSql:@"insert into GroceryItem (name, number) values('apple', 5)"];
[database executeSql:@"insert into GroceryItem (name, number) values('zuoyou', 3)"];
}
else
{
[database executeSql:@"insert into GroceryItem (name, number) values('apple', 5)"];
[database executeSql:@"insert into GroceryItem (name, number) values('zuoyou', 3)"];
}
我有这个方法可以在sqlite_master 中列出表名
- (NSArray *) tables
{
return [self executeSql:@"select * from sqlite_master where type = 'table'"];
}
- (NSArray *) tableNames
{
NSLog(@"%@", [[self tables] valueForKey:@"name"] );
NSLog(@"%u", [[[self tables] valueForKey:@"name"] count] );
return [[self tables] valueForKey:@"name"];
}
但控制台显示
杂货清单[7439:c07] ( “sqlite_sequence” )
2012-12-10 16:59:40.305 GroceryList[7439:c07] 1
只得到表sqlite_sequence,计数为1;从上面的异常中,它说“表 GroceryItem 已经存在”
【问题讨论】: