我正在模拟器中运行 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"];
}
但控制台显示
GroceryList[7439:c07] ("sqlite_sequence")
2012-12-10 16:59:40.305 GroceryList[7439:c07] 1
只得到表sqlite_sequence
,计数为1;从上面的异常中,它说“表 GroceryItem 已经存在”