【问题标题】:Query statement is not executing of sqlite database in xcode查询语句未在 xcode 中执行 sqlite 数据库
【发布时间】:2013-01-05 16:53:15
【问题描述】:

我在我的项目中使用 sqlite 数据库 mathFActs,我通过 Sqlite 数据库浏览器创建并将其添加到 Xcode。以下是我来自视图控制器类的代码

- (void)viewDidLoad
{   [super viewDidLoad];

    [self copyDatabaseIfNeeded];
    [self getInitialDataToDisplay:[self getDBPath]];
}


- (void) copyDatabaseIfNeeded {
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dbPth = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPth];

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mathFActs"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"mathFActs"];

}

-(void) getInitialDataToDisplay:(NSString *)databasePath{

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
      NSLog(@"open");
    const char *sql = "select Question  from math ";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"prepare");
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                //address.text = addressField;

                qstn.text=addressField;         
               sqlite3_finalize(selectstmt); 

            }}
        else
            sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

}

当我运行项目时它打印打开但不准备意味着它没有执行查询语句..请帮助我解决我的问题

【问题讨论】:

  • 在你的 else 语句中写入 NSLog(@"Error: failed to select details with message '%s'.", sqlite3_errmsg(database));告诉我错误
  • 其给出的信息...'文件已加密或不是数据库
  • 什么信息?在这里打印..
  • '文件已加密或不是数据库'
  • 无论你在哪里写过 mathFActs 都替换为 mathFActs.sqlite

标签: ios database xcode sqlite


【解决方案1】:

试试这个:-

-(void) getInitialDataToDisplay:(NSString *)databasePath
{
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
     {
      NSLog(@"open");
      const char *sql = "select Question  from math ";
      sqlite3_stmt *selectstmt;
      if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"prepare");
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                //address.text = addressField;

                qstn.text=addressField;         


            }
            sqlite3_reset(selectstmt);
       }
       else
       {
          NSLog(@"Error: failed to select details with message '%s'.", sqlite3_errmsg(database));
       }
       sqlite3_finalize(selectstmt);
       sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

}

编辑:-

-(void)copyDatabaseIfNeeded
{
    @try
    {
        NSFileManager *fmgr=[NSFileManager defaultManager];
        NSError *error;
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *path=[paths objectAtIndex:0];
        dbPath=[path stringByAppendingPathComponent:@"mathFActs.sqlite"];
        if(![fmgr fileExistsAtPath:dbPath]){
            NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"Addict.sqlite"];
            if(![fmgr copyItemAtPath:defaultDBPath toPath:dbPath error:&error])
                NSLog(@"failure message----%@",[error localizedDescription]);
        }

    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception: %@", exception);
    }

}

-(NSString *)getDBPath
{
    //Search for standard documents using NSSearchPathForDirectoriesInDomains
    //First Param = Searching the documents directory
    //Second Param = Searching the Users directory and not the System
    //Expand any tildes and identify home directories.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"mathFActs.sqlite"];
}

-(void)openDatabase
{
    [self copyDatabaseIfNeeded];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        //Database Opened
        NSLog(@"Database opened");
    }
    else
    {
        NSLog(@"Database cannot be opened");
    }
}

-(void)closeDatabase
{
    sqlite3_close(database);
}

希望对你有帮助

【讨论】:

  • 仍然给出错误...。错误:无法选择详细信息,并显示消息“文件已加密或不是数据库”。
  • 使用我在上面添加的这段代码并从您的设备中删除您的应用程序,清理它并重建并测试它。它会工作
  • 您的问题解决了吗,请将此答案标记为已完成,因为这有助于其他人参考
猜你喜欢
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多