2013-04-07 48 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