【问题标题】:confused in sqlite select statement在 sqlite select 语句中感到困惑
【发布时间】:2023-03-23 06:32:01
【问题描述】:

我正在尝试从我的数据库中获取数据,但我没有解决 我试试这样

-(无效)信息

{
    //[self createEditableCopyOfDatabaseIfNeeded];
    NSString *filePath = [self getWritableDBPath];

    sqlite3 *database;
    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
    {
        NSString *qu = @"Select  Name FROM empl where SyncStatus ='1'";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, [qu UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) 
        {
            char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);

            if (firstColumn==nil)
            {
                NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease];
                NSLog(@"%c",name);

            }
            if(sqlite3_step(compiledStatement) != SQLITE_ROW ) 
            {

                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"User is not valid" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
                alert = nil;
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

它会崩溃并且 它向我显示错误原因:'* +[NSString stringWithUTF8String:]: NULL cString' * 首次抛出时调用堆栈:

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    这样使用

    if(sqlite3_step(compiledStatement) == SQLITE_ROW ) 
                {
                    char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
                    NSString *name= [[NSString alloc]initWithUTF8String:firstColumn];
    
    
                }
    

    希望对你有帮助

    【讨论】:

    • 谢谢你帮助我,amresh 也这么说
    【解决方案2】:

    我认为 firstColumn 是 NULL ,它会使程序崩溃。您尝试检查数据库中的数据是否为空。

    【讨论】:

    • 我的数据库不是空的,我在 sqlite 浏览器中触发 Select JourneyName FROM UserJourney where SyncStatus ='1' 它给了我数据
    【解决方案3】:

    这实际上很明显,因为您正在尝试获取 NULL 数据 尝试使用此代码检查代码中的 NULL:

    if (sqlite3_column_text(compiledStatement, 0)!= nil)
    

    if (sqlite3_column_text(compiledStatement, 0)!= nil)
        char *firstColumn = (char *)sqlite3_column_text(compiledStatement, 0);
    else 
        char *firstColumn = @"";
    
    NSString *name= [[NSString stringWithUTF8String:firstColumn]autorelease];
    NSLog(@"%c",name);
    

    试试这个... 希望对您有所帮助....

    【讨论】:

    • 你的回答也接近我的问题但是谢谢你的帮助
    【解决方案4】:

    我认为这段代码有两个问题。

    • 正如已经指出的那样,您正在检查等于 nil 而不是 与 nil 不同。
    • sql 函数的顺序应该是:
      1. sqlite3_open
      2. sqlite3_prepare_v2
      3. sqlite3_step
      4. sqlite3_column_text
    您还可以在以下位置阅读有关此语句顺序的信息: http://www.iosdevelopment.be/sqlite-tutorial/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      相关资源
      最近更新 更多