我想在viewDidLoad例程中打開一個sqlite數據庫,並試圖發送一個sql查詢到數據庫,但sqlite_step()每次都失敗。我不確定這裏有什麼問題。我只是試圖在sqlite3上作爲hello world的嘗試。Sqlite3_step()在這個查詢中繼續返回SQLITE_MISUSE。任何指針?
#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);
}
...
如果有人能指出我哪裏可能會出錯,那將是一件好事。感謝您的時間已經。
感謝您使用
標籤,但它會如果你使用'YOUR CODE'突出顯示代碼或者點擊編輯器上的111按鈕,那麼效果會更好。 – vodkhang 2010-09-06 12:05:27我嘗試過使用代碼塊,但是當我在代碼塊中粘貼代碼時,只有第一條#import行保留在塊中,並且代碼的其餘部分從塊中退出。我至少在十分鐘內掙扎,但沒有成功。道歉,如果前塊違反了什麼,但我不能讓代碼塊爲我工作。 – kumar 2010-09-06 13:26:51