sqlite3_prepare_v2()——没有这样的表

时间:2022-06-01 16:41:46

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;
}

1 个解决方案

#1


0  

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.

您的错误消息“没有这样的表”显然告诉您,该表在数据库中不存在。如果在开发过程中,您创建了数据库,但是没有创建表,那么就会发生这种情况。如果发生这种情况,sqlite3_open将创建一个空白的数据库(显然没有事件表)。运行程序的后续尝试将会发现这个空白数据库,并错误地得出它不需要创建表的结论。

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.

因此,我建议从您的设备/模拟器中删除该应用程序,以便它将删除在您的开发/测试过程中无意中创建的任何空白数据库。然后再试着运行这个应用程序。或者更改代码以创建表(如果不存在,通过创建表……),但不要包含drop语句),无论它是否找到数据库。

#1


0  

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.

您的错误消息“没有这样的表”显然告诉您,该表在数据库中不存在。如果在开发过程中,您创建了数据库,但是没有创建表,那么就会发生这种情况。如果发生这种情况,sqlite3_open将创建一个空白的数据库(显然没有事件表)。运行程序的后续尝试将会发现这个空白数据库,并错误地得出它不需要创建表的结论。

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.

因此,我建议从您的设备/模拟器中删除该应用程序,以便它将删除在您的开发/测试过程中无意中创建的任何空白数据库。然后再试着运行这个应用程序。或者更改代码以创建表(如果不存在,通过创建表……),但不要包含drop语句),无论它是否找到数据库。