2010-09-06 41 views
0

我想在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); 
} 
... 

如果有人能指出我哪裏可能會出錯,那將是一件好事。感謝您的時間已經。

+0

感謝您使用

標籤,但它會如果你使用'YOUR CODE'突出顯示代碼或者點擊編輯器上的111按鈕,那麼效果會更好。 –
                        
                            
    vodkhang
                                2010-09-06 12:05:27
                            
                        
                    

+0

我嘗試過使用代碼塊,但是當我在代碼塊中粘貼代碼時,只有第一條#import行保留在塊中,並且代碼的其餘部分從塊中退出。我至少在十分鐘內掙扎,但沒有成功。道歉,如果前塊違反了什麼,但我不能讓代碼塊爲我工作。 – kumar 2010-09-06 13:26:51

回答

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]; 
} 

此,國際海事組織,是更容易調試。

+0

對。 sqlite3_prepare_v2拋出SQLITE_ERROR。非常感謝FMDB。但我仍然想保持這個開放一段時間,看看我做錯了什麼。再次感謝。 – kumar 2010-09-06 18:39:28

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; 
+0

「沒有這樣的表:名字」在黑暗中的野生刺傷,但它看起來像你沒有在你的數據庫中稱爲「名稱」的表。 – 2010-09-07 15:09:03

+0

我實際上已經使用Firefox擴展,SQLite Manager驗證了這一點,並且在終端中做了一個.dump,並且兩者都否定了Xcode控制檯中的聲明,所以在該想法上運氣不佳。 – kumar 2010-09-07 17:28:10