在iphone应用程序中的sqlite db错误:没有这样的表

时间:2012-03-23 13:35:29

标签: iphone objective-c ipad sqlite

在iphone应用程序中将数据推送到我的sqlite数据库时遇到问题。 这是我创建数据库的方式:

-(IBAction)createButtonPressed:(id)sender
{
    NSLog(@"createButtonPressed: called");
    status.text=[status.text stringByAppendingString:@"create button pressed.\n"];
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    self.databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"database.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        status.text=[status.text stringByAppendingString:@"creating database... "];
        status.text=[status.text stringByAppendingFormat:@"path: %@\n\n", databasePath];

    const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            status.text=[status.text stringByAppendingString:@"database opened successfully\n"];

            char *errMsg;
            const char *sql_stmt;
            NSString *sqlStatement=[[NSString alloc] init];

            /*
             CREATING JOB TABLE
             */
            sqlStatement=@"";
            [sqlStatement stringByAppendingString:@"CREATE TABLE IF NOT EXISTS JOB "];
            [sqlStatement stringByAppendingString:@"(ID INTEGER PRIMARY KEY AUTOINCREMENT, "];
            [sqlStatement stringByAppendingString:@"NUMBER TEXT, "];
            [sqlStatement stringByAppendingString:@"DATE_LOADING INTEGER, "];
            [sqlStatement stringByAppendingString:@"DATE_DEPARTURE INTEGER, "];
            [sqlStatement stringByAppendingString:@"DATE_UNLOADING INTEGER, "];
            [sqlStatement stringByAppendingString:@"ORIGIN INTEGER, "];
            [sqlStatement stringByAppendingString:@"DESTINATION INTEGER, "];
            [sqlStatement stringByAppendingString:@"SUPPLIER INTEGER, "];
            [sqlStatement stringByAppendingString:@"RAILROAD INTEGER, "];
            [sqlStatement stringByAppendingString:@"FOREMAN INTEGER, "];
            [sqlStatement stringByAppendingString:@"WEATHER INTEGER, "];
            [sqlStatement stringByAppendingString:@"COMMENT TEXT, "];
            [sqlStatement stringByAppendingString:@"MODIFY_STATUS INTEGER)"];

            sql_stmt = [sqlStatement UTF8String];

            if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                status.text=[status.text stringByAppendingString:@"failed to create JOB table\n"];
            }
            else
            {
                status.text=[status.text stringByAppendingString:@"JOB table created\n"];
            };


        status.text=[status.text stringByAppendingString:@"closing database...\n"];
        sqlite3_close(database);

    }
    else
    {
        status.text=[status.text stringByAppendingString:@"failed to open/create database\n"];
    };
}
else
{
    status.text=[status.text stringByAppendingString:@"database exists "];
    status.text=[status.text stringByAppendingFormat:@"path: %@\n\n", databasePath];
};

[status scrollRangeToVisible:NSMakeRange([status.text length], 0)];

}

一切都与创造是可以的。的 BUT! 当我尝试将数据推送到我的表时,我收到一个错误:“没有这样的表JOB” 推送数据代码:

NSLog(@"populateButtonPressed: called");
status.text=[status.text stringByAppendingString:@"populate button pressed.\n"];

NSFileManager *filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: databasePath ] == YES)
{
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        status.text=[status.text stringByAppendingString:@"database opened successfully\n"];

        char *errMsg;
        const char *sql_stmt;
        NSString *sqlStatement=[[NSString alloc] init];

        /*
         POPULATING JOB TABLE
         */
        sqlStatement=@"INSERT INTO JOB (NUMBER, DATE_LOADING, DATE_DEPARTURE, DATE_UNLOADING, ORIGIN, DESTINATION, SUPPLIER, RAILROAD, FOREMAN, WEATHER, COMMENT, MODIFY_STATUS) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        sql_stmt = [sqlStatement UTF8String];
        sqlite3_stmt *stmt;
        sqlite3_prepare_v2(database, sql_stmt, -1, &stmt, nil);
        sqlite3_bind_text(stmt, 1, "number", -1, SQLITE_STATIC); //number
        sqlite3_bind_int(stmt, 2, 100); //date_loading
        sqlite3_bind_int(stmt, 3, 101); //date_departure
        sqlite3_bind_int(stmt, 4, 102); //date_unloading
        sqlite3_bind_int(stmt, 5, 103); //origin
        sqlite3_bind_int(stmt, 6, 104); //destination
        sqlite3_bind_int(stmt, 7, 105); //supplier
        sqlite3_bind_int(stmt, 8, 106); //railroad
        sqlite3_bind_int(stmt, 9, 107); //foreman
        sqlite3_bind_int(stmt, 10, 108); //weather
        sqlite3_bind_text(stmt, 11, "commentstring", -1, SQLITE_STATIC); //comment
        sqlite3_bind_int(stmt, 12, 1);

        if (sqlite3_step(stmt)!=SQLITE_DONE)
        {
            status.text=[status.text stringByAppendingString:@"WARNING!!! Could not step (execute) stmt\n"];
            status.text=[status.text stringByAppendingFormat:@"error: %@\n", [NSString stringWithUTF8String:sqlite3_errmsg(database)]];
        }
        else
        {
            status.text=[status.text stringByAppendingString:@"it seems like stmt step (execute) was successfull"];
        };

        if(sqlite3_finalize(stmt)==SQLITE_DONE)
        {
            status.text=[status.text stringByAppendingString:@"finalizing stmt was successfull\n"];
        }
        else
        {
            status.text=[status.text stringByAppendingString:@"WARNING!!! failed finalizing stmt\n"];
        };


        status.text=[status.text stringByAppendingString:@"closing database...\n"];
        sqlite3_close(database);

    }
    else
    {
        status.text=[status.text stringByAppendingString:@"failed to open database\n"];
    };
}
else
{
    status.text=[status.text stringByAppendingString:@"database does not exist. you need to create it.\n"];
};

[status scrollRangeToVisible:NSMakeRange([status.text length], 0)];

数据库存在于路径并成功打开。 iOS5 ipad应用程序,使用ARC和libsqlite3.dylib,我在模拟器中运行我的应用程序。

1 个答案:

答案 0 :(得分:1)

你的数据库路径是否正确?你为什么不先检查你的终端?

> sqlite3 <your_db_path>

> show tables;