我打开数据库,但是当我检查语句是否正常时,我收到以下错误。
Error no such table: Event
以下是代码的一部分:
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = @"SELECT nic,subject,location,participants,startDate,endDate FROM Event";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)
如果我没有打开数据库,我不会收到任何错误,但如果我再次调用该函数,我会得到:
library routine called out of sequence
创建数据库的代码:
-(BOOL)createDB
{
// Get the documents directory
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"reglis.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt ="DROP TABLE IF EXISTS Client;CREATE TABLE Client (nic VARCHAR PRIMARY KEY NOT NULL UNIQUE , name VARCHAR, location VARCHAR, postalCode VARCHAR);DROP TABLE IF EXISTS Meio;CREATE TABLE Meio (id_meio VARCHAR PRIMARY KEY NOT NULL , nic VARCHAR, email VARCHAR, telefone VARCHAR, telemovel VARCHAR, id_tipo_meio VARCHAR);DROP TABLE IF EXISTS Event;CREATE TABLE Event (nic VARCHAR PRIMARY KEY NOT NULL , subject VARCHAR, location VARCHAR, participants VARCHAR, startDate VARCHAR, endDate VARCHAR);";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create tables");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
答案 0 :(得分:1)
您的错误消息“没有这样的表”,显然告诉您该表在数据库中不存在。如果在开发期间的任何时候创建了数据库但无法创建表,则会发生这种情况。如果发生这种情况,sqlite3_open
将会创建一个空白数据库(显然没有event
表)。随后尝试运行程序将找到这个空白数据库并错误地断定它不需要创建表。
因此,我建议您从设备/模拟器中删除应用程序,以便删除在开发/测试过程中无意中创建的任何空白数据库。然后再次尝试运行该应用。或者,如有必要,可以更改代码以创建表格(通过create table if not exists ...
,但不包含您的drop
语句),无论它是否找到数据库。