Sqlite3_step() keeps returning SQLITE_MISUSE on this query. Any pointers?
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 here. I am just trying this as a hello world attempt at sqlite3.
#import "RootViewController.h"
#import "sqlite3.h"
static sqlite3_stmt *statement = nil;
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *dbname = @"name.sqlite";
sqlite3 *database;
int success;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:dbname];
// Open the database. The database was prepared outside the application.
success = sqlite3_open([path UTF8String], &database);
if (success == SQLITE_OK) {
NSLog(@"database opening successful : %d", success);
} else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSLog(@"database opening failed : %d", success);
// Additional error handling, as appropriate...
}
if (statement == nil) {
const char *sql = "insert into name(nid, name) v开发者_如何学JAVAalues(6, 'testname');";
success = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
NSLog(@"query prepare status: %d", success);
if (success != SQLITE_OK) {
}
}
success = sqlite3_step(statement); //things fail here, as all calls above return ok
NSLog(@"query step status: %d", success);
if (success != SQLITE_DONE) {
}
sqlite3_reset(statement);
sqlite3_close(database);
}
...
It would be great if anyone could point me out where I might be wrong. Thanks for your time already.
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.
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;
加载中,请稍侯......
精彩评论