2013-04-07 50 views
2

我創建了我的表並使用firefox sqlite管理器擴展插入了我的數據,保存並添加了我的sqlitefile,它是「notifications.sqlite」,之後我編寫了此代碼,但它會輸出我沒有什麼如何使用firefox插件創建.sqlite文件

打開.sqlite文件和.db文件有什麼不同嗎?我怎樣才能正常打開我的數據庫?

* UPDATE: *調試完畢後作爲朋友評論它在這條線的問題:

databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"notifications.sqlite"]]; 

if ([filemgr fileExistsAtPath: databasePath ] == YES) 

如果不是YES這樣就失去了一切,所以databasePath有點問題

我的表如下:

CREATE TABLE "quotes_type" ("type_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "content" VARCHAR) 

,這裏是我的努力打開.sqlite文件:

- (void)viewDidLoad{ 

    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *docsDir = [dirPaths objectAtIndex:0]; 
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"notifications.sqlite"]]; 
    sqlite3_stmt *statement; 
    NSFileManager *filemgr = [NSFileManager defaultManager]; 
    if ([filemgr fileExistsAtPath: databasePath ] == YES) 
    { 
    if (sqlite3_open([databasePath UTF8String], &contactDB) == SQLITE_OK) 
    { 

     NSString *qrycount = [NSString stringWithFormat: @"select * from quotes_type"]; 

     const char *count_stmt = [qrycount UTF8String]; 

     sqlite3_prepare_v2(contactDB, count_stmt, -1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_ERROR) { 
      NSAssert1(0,@"Error when selecting rows %s",sqlite3_errmsg(contactDB)); 
     } else { 
       NSLog(@"negin"); 
      int myid=sqlite3_column_int(statement, 0); 
      NSString *text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,1)]; 
      NSLog(@"myid is %d",myid); 
      NSLog(@"text is %@",text); 
      sqlite3_finalize(statement); 
      sqlite3_close(contactDB); 
     } 
    } 
    else NSLog(@"openning has problem"); 
    } 
} 
+0

你使用調試器來檢查哪些說法究竟失敗?爲什麼不使用'sqlite3_errmsg()'來報告錯誤信息而不是「打開有問題」? – 2013-04-07 14:36:51

+0

我曾經使用它,它會去打開語句問題是在sqlite_open之後的某處,因爲我沒有任何nslog輸出 – Nickool 2013-04-07 14:40:23

+0

我編輯了我的問題,但同時它似乎有人正在編輯我,我失去了我我appologize – Nickool 2013-04-07 14:44:38

回答

1

你在Documents檢查數據庫。但你曾經在那裏複製過嗎?如果沒有,你永遠不會在那裏找到它。您確實想檢查該文件,如果找不到,請將其從捆綁軟件複製到Documents文件夾。因此,如果你不Documents找到數據庫,您應該通過複製:

NSError *error; 
NSString *bundleDatabasePath = [[NSBundle mainBundle] pathForResource:@"notifications" ofType:@"sqlite"]; 
NSAssert(bundleDatabasePath, @"database not found in bundle"); 
if (![filemgr copyItemAtPath:bundleDatabasePath toPath:databasePath error:&error]) 
    NSLog(@"%s: copyItemAtPath failure: error = %@", __FUNCTION__, error); 
0

您可以使用FMDB來管理你的SQLite數據庫,這是很容易。

github上:FMDB