Sqlite3_step() keeps returning SQLITE_MISUSE on this query. Any pointers?

后端 未结 2 406
粉色の甜心
粉色の甜心 2021-01-29 03:28

I am trying to open a sqlite db in the viewDidLoad routine and trying to send a sql query through to the db, but the sqlite_step() fails every time. I am not sure what\'s wrong

相关标签:
2条回答
  • 2021-01-29 03:50

    I did this but it still doesn't work, as in nothing shows up in the Console.

    #import "RootViewController.h"
    #import "FMDatabase.h"
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *dbname = @"name.sqlite";
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:dbname];
        FMDatabase * database = [FMDatabase databaseWithPath:path];
        FMResultSet *rs = [database executeQuery:@"select * from name;"];
        while ([rs next]) {
            NSLog(@"%d %@",
                  [rs intForColumn:@"nid"],
                  [rs stringForColumn:@"name"]);
        }
        [database close];
    }
    ...
    

    And sorry but am unable to use the code block, so using a pre again.

    Edit: When I turn logging on in FMDB, like this:

        FMDatabase * database = [FMDatabase databaseWithPath:path];
    
        [database setLogsErrors:YES];
    
        if (![database open]) { 
            NSLog(@"Could not open db."); 
        } else { 
            NSLog(@"DB Open...."); 
        }
        FMResultSet *rs = [database executeQuery:@"select * from 'name';"];
        while ([rs next]) {
            // just print out what we've got in a number of formats.
            NSLog(@"%d %@",
                  [rs intForColumn:@"nid"],
                  [rs stringForColumn:@"name"]);
        }
        [database close];
    

    ..., I get this in the Console:

    [Session started at 2010-09-07 15:38:16 +0530.]
    2010-09-07 15:38:19.178 MyDBTry[13670:207] DB Open....
    2010-09-07 15:38:19.181 MyDBTry[13670:207] DB Error: 1 "no such table: name"
    2010-09-07 15:38:19.182 MyDBTry[13670:207] DB Query: select * from name;
    
    0 讨论(0)
  • 2021-01-29 03:57

    Have you verified that the statement compiles OK? You have the success = sqlite3_prepare_v2(...); line, but never do anything if success is not SQLITE_OK. Have you verified that it is SQLITE_OK?

    On the other hand, if you were to use a wrapper like FMDB, you could replace all of your code above with:

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *dbname = @"name.sqlite";
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:dbname];
        FMDatabase * database = [FMDatabase databaseWithPath:path];
    
        [database executeUpdate:@"insert into name(nid, name) values(?, ?)", [NSNumber numberWithInt:6], @"testname"];
        [database close];
    }
    

    This, IMO, is much easier to debug.

    0 讨论(0)
提交回复
热议问题