我试图在viewDidLoad例程中打开一个sqlite db并尝试将sql查询发送到db,但sqlite_step()每次都失败。我不确定这里有什么问题。我只是尝试将此作为sqlite3的hello world尝试。
#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) values(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);
}
...
如果有人能指出我可能出错的地方,那就太好了。谢谢你的时间。
答案 0 :(得分:0)
您是否确认该声明编译正常?您有success = sqlite3_prepare_v2(...);
行,但如果success
不是SQLITE_OK
,则不会执行任何操作。您确认是SQLITE_OK
吗?
另一方面,如果你使用像FMDB这样的包装器,你可以用以下代码替换上面的所有代码:
- (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];
}
这个,IMO,很多更容易调试。
答案 1 :(得分:0)
我这样做但它仍然不起作用,因为在控制台中没有显示任何内容。
#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]; } ...
很抱歉,但我无法使用代码块,所以再次使用pre。
编辑:当我在FMDB中打开登录时,如下所示:
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];
...,我在控制台中得到了这个:
[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;