尝试在iOS xcode中执行select查询时的SQLITE_MISUSE

时间:2014-03-27 13:49:17

标签: ios sqlite

我有一个类来访问我的数据库,当我想要一个实例时,我调用了这个函数

 +(DatabaseHelper*) getInstanse
{
if (!instance) {
    instance = [[super allocWithZone:NULL] init];
    [instance createDatabase];
}
return instance;
}

这里是createDatabase功能

-(void) createDatabase
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
                [docsDir stringByAppendingPathComponent: databaseName]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        [self createTableProduct];
        [self createTableCategory];
        [self createTableState];
        [self createTableAgency];
        [self createTableNews];
        [self createTableKnowledge];
        [self createTableVideos];
        [self createTableProductComment];
        sqlite3_close(database);
    }
    else {
        NSLog(@"Failed to open/create database");
    }
}

}

我没有创建数据库的问题并插入其中但是当我想要执行select查询时,它说sqlite_misuse

这是我的选择查询

-(NSMutableArray*) getAllAgencies
{
[self createEditableDatabase];
NSMutableArray *agencies = [[NSMutableArray alloc] init];
NSString *query = [[NSString alloc] initWithFormat:@"SELECT * FROM %@_%@", agencyTable, [Shares getLanguage]];
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, [query cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL);
if (sqlResult == SQLITE_OK)
{
    while (sqlite3_step(statement) == SQLITE_ROW)
    {
        int Id = sqlite3_column_int(statement, 0);
        char *nameChars = (char *) sqlite3_column_text(statement, 1);
        char *phoneChars = (char *) sqlite3_column_text(statement, 2);
        char *addressChars = (char *) sqlite3_column_text(statement, 3);
        char *faxChars = (char *) sqlite3_column_text(statement, 4);
        char *emailChars = (char *) sqlite3_column_text(statement, 5);
        char *detailsChars = (char *) sqlite3_column_text(statement, 6);
        int stateID = sqlite3_column_int(statement, 7);
        float latitude = (float) sqlite3_column_double(statement, 8);
        float longtitude = (float) sqlite3_column_double(statement, 9);
        NSString *name = [[NSString alloc] initWithUTF8String:nameChars];
        NSString *phone = [[NSString alloc] initWithUTF8String:phoneChars];
        NSString *address = [[NSString alloc] initWithUTF8String:addressChars];
        NSString *fax = [[NSString alloc] initWithUTF8String:faxChars];
        NSString *email = [[NSString alloc] initWithUTF8String:emailChars];
        NSString *details = [[NSString alloc] initWithUTF8String:detailsChars];
        AgencyData *data = [[AgencyData alloc] init];
        [data setId:Id];
        [data setName:name];
        [data setPhone:phone];
        [data setAddress:address];
        [data setFax:fax];
        [data setEmail:email];
        [data setDetails:details];
        [data setStateId:stateID];
        [data setLatitude:latitude];
        [data setLongtitude:longtitude];
        [agencies addObject:data];
    }
}
sqlite3_finalize(statement);

return agencies;
}

这里是createEditableDatabase函数

- (void) createEditableDatabase {
// Check to see if editable database already exists
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *writableDB = [documentsDir stringByAppendingPathComponent:@"panberes.db"];
success = [fileManager fileExistsAtPath:writableDB];
// The editable database already exists
if (success) return;
// The editable database does not exist
// Copy the default DB to the application Documents directory.
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath]
                         stringByAppendingPathComponent:@"panberes.db"];
success = [fileManager copyItemAtPath:defaultPath
                               toPath:writableDB error:&error];
if (!success) {
    NSAssert1(0, @"Failed to create writable database file:'%@'.",
              [error localizedDescription]);
}
}

我需要一些帮助

1 个答案:

答案 0 :(得分:0)

我认为问题出在您使用createDatabase方法创建数据库之后,然后关闭数据库(sqlite_close(database))。您永远不会重新打开数据库来执行sqlite查询。方法[self createEditableDatabase]仅复制可编辑数据库文件(如果它首先不存在),但它不会打开数据库。 Sqlite会抛出一个" MISUSE"尝试运行查询并且没有打开的数据库时出现错误。

此外,如果createDatabasecreateEditableDatabase正在处理同一个数据库,则代码中不清楚。如果是,则最好打开数据库并保持打开状态,直到您完成它为止。多次打开和关闭数据库会导致性能下降。