我正在開發一個使用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
感謝申請了很多,我想問一些問題,以澄清更多的理解; 1 - 哪裏應該是數據庫文件位於? (物理)2 - 上面的代碼在哪裏查找文件? 3 - 代碼是否將文件複製到緩存或物理位置以允許應用程序寫入/讀取?我問這些問題,因爲fileexists命令可以找到我的數據庫文件(文件存在過程通過),錯誤是「沒有這樣的人表」。我明白,該文件是由應用程序找到,但表不能。 – 2013-05-10 05:09:59
@MuratCalim我會很樂意回答你的問題,請拍 – DivineDesert 2013-05-10 05:12:09
我應用你的代碼,並通過if(成功)返回點。這是不是意味着「數據庫文件存在於路徑可寫數據庫路徑」?但在此之後,我的應用程序返回相同的錯誤「沒有這樣的表peopleto」。但我可以看到並查詢人們的表格!? – 2013-05-10 05:20:53