查询不从表中检索数据

时间:2011-10-03 10:30:36

标签: iphone objective-c

我试图将数据库集成到我的项目中。这是第一次实现数据库项目。我将数据库集成到我的项目中。数据库有3列名为rowid,col1,col2.Below代码我用于访问数据来自数据库。但它不会在“如果循环”中消失。任何人都可以帮助我在这种情况下我做错了。谢谢提前。![在此输入图像描述] [1]

 /*   -(void)print
    {
        sqlite3_stmt *statement;
        qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = '%d'",1];

        if(sqlite3_prepare_v2(database, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {


             NSLog(@"hhhhhhhhh");
            while (sqlite3_step(statement) == (SQLITE_ROW)){

                   NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];


               // tv.text=qsql;



            }

        }}*/
    -(void)print
    {
        sqlite3_stmt *statement;
        //qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %d",1];
        qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];

        //const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
       const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
        if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {


            NSLog(@"hhhhhhhhh");
            while (sqlite3_step(statement) == (SQLITE_ROW)){
              //  NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
                NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];

                // tv.text=qsql;
            }

        }            

            }

    -(void) checkAndCreateDatabase{
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:databasePath];
        if(success) return;
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
        [fileManager release];
    }

    -(void) openDB {

        databaseName = @"Lighthr.sqlite";
        NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDir = [documentsPaths objectAtIndex:0];   
        databasePath = [documentDir stringByAppendingPathComponent:databaseName];
        [self checkAndCreateDatabase];  


        if(sqlite3_open([databasePath UTF8String],&database) == SQLITE_OK){
            NSLog(@"coming here???");
        }
    }


  [1]: http://i.stack.imgur.com/lm1ZV.png

4 个答案:

答案 0 :(得分:0)

因为rowid始终是整数,并且您尝试使用字符串进行访问 我必须尝试这个。

修改

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    qsql=[NSString stringWithFormat:@"SELECT * from light where rowid = %i",1];
    const char *sqlStatement = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {


        NSLog(@"hhhhhhhhh");
        while (sqlite3_step(statement) == (SQLITE_ROW)){
            NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
           // tv.text=qsql;
        }

    }
}

答案 1 :(得分:0)

你是否试图在没有“where”条款的情况下进行测试?

sqlite3_prepare_v2返回的错误代码是什么?

int sql_answer = sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL);
NSLog(@" SQL answer : %d",sql_answer );
if(sql_answer == SQLITE_OK) {
  ...

答案 2 :(得分:0)

NSString *Q_NO = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)];

以上stameny替换为以下声明

NSString *Q_NO = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%i",sqlite3_column_int(statement, 0)]];

const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];

现在用[qsql UTF8String]替换qsql_stament。它会正常工作。

答案 3 :(得分:0)

我认为你应该这样做。

- (无效)打印 {

sqlite3_stmt *声明;     qsql = [NSString stringWithFormat:@“SELECT * from light where rowid =%d”,1];

const char *qsql_stament = [qsql cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, qsql_stament, -1, &statement, NULL) == SQLITE_OK) {


    NSLog(@"hhhhhhhhh");
    while (sqlite3_step(statement) == (SQLITE_ROW)){            
        NSString *Q_NO =[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]



    }