我有一个名为China.sqlite nel Bundle的数据库,它由16个表组成。我必须从名为Menu的表中打开表元素。我该怎么办?提前致谢
答案 0 :(得分:0)
sqlite3 *database;
NSString *databasePath = ......;
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){
NSString *sqlString = @"SELECT ......."; //Query
const char *sql = [sqlString UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
//Get the first 3 coloms of table
NSString *col1 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 0)];
NSString *col2 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)];
NSNumber *col3 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 2)];
//save them in dictionary
returnDictionary = [NSDictionary dictionaryWithObjects:[NSArray col1, col2, col3, nil]
forKeys:[NSArray arrayWithObjects:@"Col1", @"Col2", @"Col3", nil]];
}
} else {
//Error handling
}
}
假设您的数据库文件位于主包中
NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"China" ofType:@"sqlite"];