在iOS问题中打开SQLite文件

时间:2015-03-16 06:47:24

标签: ios sqlite

我正在尝试打开一个SQLite文件并将其上传到我的项目中但是我总是得到错误no such table: Words,而我可以在sql app中获得结果。

当我调试时,我发现我无法输入if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK)

请问我的问题在哪里?

我的代码:

- (void)GetData{

NSString *docsDir;
NSArray *dirPaths;

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"wordsDb.sqlite"]];

sqlite3 *database;
sqlite3_stmt *selectstmt;

if(sqlite3_open([_databasePath UTF8String], &database) == SQLITE_OK) {

    NSString *sqlString = [NSString stringWithFormat:@"SELECT words FROM Words"];
    NSLog(@"getCurrentUserInfo:%@",sqlString);
    const char *SqlCommand = [sqlString UTF8String];

    if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {

        NSLog(@"success!");
        while (sqlite3_step(selectstmt) == SQLITE_ROW) {
            NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
            NSLog(@"select result:%@",userInfoStr);
        }
    }
    else
    {
        NSLog(@"%s",sqlite3_errmsg(database));
    }
    sqlite3_finalize(selectstmt);
}
  sqlite3_close (database);
}

1 个答案:

答案 0 :(得分:1)

试试这个

此方法将数据库表单xcode-resource文件夹复制到文档文件夹

- (void)copyAndInitialiseDatabase{

    @try {
        // get the database path
        documentDBPath = [NSString stringWithFormat:@"%@/Documents/%@.sqlite",NSHomeDirectory(),@"wordsDb"];

        if ([[NSFileManager defaultManager] fileExistsAtPath:documentDBPath] == NO){// database is not available in document folder

            NSString *resourceDBPath = [[NSBundle mainBundle] pathForResource:@"wordsDb" ofType:@"sqlite"];

            if (resourceDBPath == nil){// check the database in souppotingFiles (resource) in our applicataion

                    NSLog(@" %s : %d : %s dabase is not found in resource folder. Please check the name of database or copy in resource folder.",__FILE__,__LINE__,__PRETTY_FUNCTION__);
                return;
            }// data base available in resource folder then copy to resource folder to document folder
            [[NSFileManager defaultManager] copyItemAtPath:resourceDBPath toPath:documentDBPath error:nil];
        }
        // database is copied, open the database
        if (sqlite3_open_v2([documentDBPath  UTF8String], &_database, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK){// database is open success

                NSLog(@"database is opend successfully");

        }else{// database is open fail

                NSLog(@"fail to open");

            sqlite3_close(_database);

                NSLog(@" %s: %d: %s fail to open the database. error == %s",__FILE__,__LINE__,__PRETTY_FUNCTION__,sqlite3_errmsg(_database));
        }
    }
    @catch (NSException *exception) {
    }
    @finally {
    }
}

-(IBAction)getThedata:(UIButton *)sender {
    [self copyAndInitialiseDatabase];
    sqlite3 *database;
    sqlite3_stmt *selectstmt;

    if(sqlite3_open([documentDBPath UTF8String], &database) == SQLITE_OK) {

        NSString *sqlString = [NSString stringWithFormat:@"SELECT words FROM Words"];
        NSLog(@"getCurrentUserInfo:%@",sqlString);
        const char *SqlCommand = [sqlString UTF8String];

        if (sqlite3_prepare_v2(database, SqlCommand, -1, &selectstmt, NULL) == SQLITE_OK) {

            NSLog(@"success!");
            while (sqlite3_step(selectstmt) == SQLITE_ROW) {
                NSString *userInfoStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
                NSLog(@"select result:%@",userInfoStr);
            }
        }
        else
        {
            NSLog(@"%s",sqlite3_errmsg(database));
        }
        sqlite3_finalize(selectstmt);
    }
    sqlite3_close (database);
}