我在我的項目中使用sqlite數據庫mathFActs,通過Sqlite數據庫瀏覽器創建它並將其添加到Xcode。以下是從一個視圖控制器類我的代碼查詢語句不執行xcode中的sqlite數據庫
- (void)viewDidLoad
{ [super viewDidLoad];
[self copyDatabaseIfNeeded];
[self getInitialDataToDisplay:[self getDBPath]];
}
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPth = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPth];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mathFActs"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"mathFActs"];
}
-(void) getInitialDataToDisplay:(NSString *)databasePath{
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(@"open");
const char *sql = "select Question from math ";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
NSLog(@"prepare");
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
//address.text = addressField;
qstn.text=addressField;
sqlite3_finalize(selectstmt);
}}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
}
當我運行該項目,其打印開放,但不準備意味着它不執行查詢語句.. plz幫助我解決我的問題
在您的else語句中寫入NSLog(@「錯誤:無法選擇消息'%s'的詳細信息。」,sqlite3_errmsg(database));並告訴我錯誤 –
其給予的消息...'文件被加密或不是數據庫 – Arpi
什麼消息?打印這裏.. –