2013-05-10 101 views
0

我正在开发一个使用sqlite3数据库文件的iphone应用程序。我使用下面的代码从sqlite3数据库中选择;sqlite3_prepare_v2()== SQLITE_OK返回NO并且没有找到现有表

NSString* dbYolu = [NSString stringWithFormat:@"%@/emhdb3.sqlite3",NSHomeDirectory()]; 

sqlite3* db; 

NSLog(@"%@ dbyolu : ", dbYolu); 

if (sqlite3_open([dbYolu UTF8String], &db) == SQLITE_OK) 
{ 
    NSString *query = [NSString stringWithFormat: @"SELECT username, mobile FROM peopleto WHERE namesurname=\"%@\"", name.text]; 
    NSLog(@"%@ : query", query); 
    sqlite3_stmt *stmt; 

    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, nil) == SQLITE_OK) // statement stmt returns null and returns SQLITE_OK = FALSE. 
    { 
     NSLog(@"stepped in SQLITE_OK is TRUE"); 
     while (sqlite3_step(stmt) == SQLITE_ROW) 
     { 
      NSString* ders_kodu = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)]; 
      double not = sqlite3_column_double(stmt, 1); 

      NSLog(@"%@ : %f", ders_kodu, not); 
     } 
    } 
    else 
    { 
     NSLog(@"%@ - but failed", stmt); 
    } 
    sqlite3_finalize(stmt); 
} 
else 
    NSLog(@"db could not be opened"); 

它未能在 “sqlite3_prepare_v2(分贝,[查询UTF8字符串],-1,&语句,无)== SQLITE_OK” 上方点。并且错误是:“错误:没有这样的表:peopleto。但它成功返回表格的内容,当我在sql浏览器中运行此查询时,我的意思是这个查询是正确的并且正在工作。

btw,与上viewLoad

- (void)viewDidLoad 
{ 
NSString *docsDir; 
NSArray *dirPaths; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 

// Build the path to the database file 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"emhdb3.sqlite3"]]; 

NSLog(@"dbpath : %@", databasePath); 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: databasePath ] == NO) 
{ 
    const char *dbpath = [databasePath UTF8String]; 
    NSLog(@"db not found"); 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     NSLog(@"SQLITE_OK is passed"); 
     //.... some codes here, doesn't metter for me because SQLITE_OK = true 
    } else { 
     status.text = @"Failed to open/create database"; 
    } 
} 
else if ([filemgr fileExistsAtPath:databasePath] == YES) 
{ 
    NSLog(@"i found the file here : %s",[databasePath UTF8String]); 
} 
NSLog(@"completed"); 
[super viewDidLoad]; 
} 

下面的代码路径我有数据库文件在我的项目文件夹,并添加到项目中。 我缺少什么?

感谢

///// //

我很抱歉更新我的问题,但stackoverflow不允许我添加新的,

请在下面找到我的更新;

这里是更新的版本的代码:

viewLoad part;

- (void)viewDidLoad 
{ 
[self createEditableCopyOfDatabaseIfNeeded]; 

NSString *docsDir; 
NSArray *dirPaths; 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = [dirPaths objectAtIndex:0]; 
databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"emhdb3.sqlite3"]]; 

NSLog(@"dbpath : %@", databasePath); 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: databasePath ] == NO) 
{ 
const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = "CREATE TABLE IF NOT EXISTS peopleto (pId INTEGER PRIMARY KEY AUTOINCREMENT, namesurname TEXT, mobile TEXT)"; 
     if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 
      status.text = @"Failed to create table"; 
     } 
     sqlite3_close(contactDB); 
    } else { 
     status.text = @"Failed to open/create database"; 
    } 
} 
else if ([filemgr fileExistsAtPath:databasePath] == YES) 
{ 
    NSLog(@"%s is filepath",[databasePath UTF8String]); 
} 
NSLog(@"checkpoint 4"); 
[super viewDidLoad]; 
} 

buraya巴斯卡birşeyYAZ

- (void)createEditableCopyOfDatabaseIfNeeded { 
NSLog(@"entered createEditableCopyOfDatabaseIfNeeded"); 
BOOL success; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"emhdb3.sqlite3"]; 
success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success) 
{ 
    NSLog(@"success == YES returned"); 
    return; 
} 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"emhdb3.sqlite3"]; 
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
if (!success) { 
    NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
} 
else{ 
    NSLog(@"checkpoint 2"); 
} 
} 

和所执行的选择查询的一部分;

-(IBAction)findcontact2:(id)sender 
{ 
NSString *dbYolu = databasePath; 
sqlite3* db; 

NSLog(@"%@ dbyolu : ", dbYolu); 

if (sqlite3_open([dbYolu UTF8String], &db) == SQLITE_OK) 
{ 
    NSString *query = [NSString stringWithFormat: @"SELECT namesurname, mobile FROM peopleto"]; //any query 

    NSLog(@"%@ : query", query); 

    sqlite3_stmt *stmt; 

    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, nil) == SQLITE_OK) 
    { 
     NSLog(@"SQLITE is OK"); 
     while (sqlite3_step(stmt) == SQLITE_ROW) 
     { 
      NSString* ders_kodu = [NSString stringWithUTF8String:(char*)sqlite3_column_text(stmt, 0)]; 
      double not = sqlite3_column_double(stmt, 1); 

      NSLog(@"%@ : %f", namesurname, mobile); 
     } 
    } 
    else 
    { 
     NSLog(@"Error=%s",sqlite3_errmsg(db)); // error message : Error=no such table: peopleto 
     NSLog(@"%@ - is stmt", stmt); //stmt is null 
    } 
    sqlite3_finalize(stmt); 
} 
else 
    NSLog(@"could not open the db"); 
} 

正如我上面所指出的,错误是错误=没有这样的表:peopleto和语句则返回null

回答

0

请检查DB这是你的主包中包含的表,然后尝试将它复制到资源文件夹和您的快速回复crazycreator上述

1

你复制你的SQL文件到文件,如果不是你需要做的第一。在将SQL文件添加到项目中时,它会添加到您的包中,而不是添加到文档目录中。你需要复制,首先

- (void)createEditableCopyOfDatabaseIfNeeded { 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) 
     return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 
+0

感谢申请了很多,我想问一些问题,以澄清更多的理解; 1 - 哪里应该是数据库文件位于? (物理)2 - 上面的代码在哪里查找文件? 3 - 代码是否将文件复制到缓存或物理位置以允许应用程序写入/读取?我问这些问题,因为fileexists命令可以找到我的数据库文件(文件存在过程通过),错误是“没有这样的人表”。我明白,该文件是由应用程序找到,但表不能。 – 2013-05-10 05:09:59

+0

@MuratCalim我会很乐意回答你的问题,请拍 – DivineDesert 2013-05-10 05:12:09

+0

我应用你的代码,并通过if(成功)返回点。这是不是意味着“数据库文件存在于路径可写数据库路径”?但在此之后,我的应用程序返回相同的错误“没有这样的表peopleto”。但我可以看到并查询人们的表格!? – 2013-05-10 05:20:53