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

后端 未结 2 405
粉色の甜心
粉色の甜心 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: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.

提交回复
热议问题