我有一个名为 China.sqlite nel Bundle 的数据库,它由 16 个表组成。我必须从名为 Menu 的表格中打开表格元素。我该怎么办?提前致谢
问问题
170 次
1 回答
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"];
于 2012-09-25T14:08:50.763 回答