@
iluhcm 你好,我查询数据是用另一种方式,跟查询表名不一样,都是从网上学来的,sqlite3真心第一次玩,数据库也是第一次玩。代码在这里:
- (NSArray *)getAllPersonsWithTableName:(NSString *)aTableName
{
[self openSqliteDatabase];
if (![self isTableExist:aTableName])
{
NSLog(@"要查询的表不存在,请检查表名");
return nil;
}
NSString *query = [NSString stringWithFormat:@"select * from '%@'",aTableName];
sqlite3_stmt *stmt;
NSMutableArray *mutable = [NSMutableArray array];
int result = sqlite3_prepare_v2(_database, [query UTF8String], -1, &stmt, nil);
if (result == SQLITE_OK)
{
while (sqlite3_step(stmt) == SQLITE_ROW)
{
unsigned int ID = (int)sqlite3_column_int(stmt, 0);
char *name = (char *)sqlite3_column_text(stmt, 1);
NSString *nameString = [[NSString alloc] initWithUTF8String:name];
char *desc = (char *)sqlite3_column_text(stmt, 2);
NSString *descString = [[NSString alloc] initWithUTF8String:desc];
NSLog(@"id =%d,name = %s,desc = %s",ID,name,desc);
Person *person = [[Person alloc] initWithName:nameString desc:descString];
[mutable addObject:person];
}
sqlite3_finalize(stmt);
} else
{
NSLog(@"result = %d,SQL error or missing database",result);
}
sqlite3_close(_database);
return mutable;
}