Reputation: 2169
I open the database, but when i check if the statement is OK, i get the following error.
Error no such table: Event
Here is part of the code:
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)
If i don´t open the database i don´t get any error, but if i call the function again i get:
library routine called out of sequence
The code to create the database:
-(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;
}
Upvotes: 2
Views: 10104
Reputation: 438287
Your error message, "no such table", obviously is telling you that the table doesn't exist in the database. This can happen if, anytime during your development, you created the database but failed to create the tables. And if that ever happened, sqlite3_open
will, if it doesn't find the database, will create a blank database (with, obviously, no event
table). And subsequent attempts to run your program will find this blank database and incorrectly conclude that it doesn't need to create the tables.
So, I'd suggest remove the app from your device/simulator so that it will remove any blank databases that were inadvertently created earlier in your development/testing process. Then try running the app again. Or change your code to create the tables if necessary (via create table if not exists ...
, but do not include your drop
statements) regardless of whether it found the database or not.
Upvotes: 1