0

我有两个 .sqlite 文件。一份用于完整版,一份用于精简版。我正在使用以下代码为一种条件选择一个 .sqlite 文件:

if (FULLVERSION) {
self.databaseName = @"Alpha.sqlite";
}
else {
self.databaseName = @"AlphaLite.sqlite";
}

现在,当我查询时,我想在其中使用这个 self.databaseName。我现在这样做:

-(NSMutableArray *) Alpha {
    sqlite3 *database;
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "SELECT * FROM Alpha";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

    }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);

    return alpha;   
}

在“SELECT * FROM Alpha”行中,我想使用 self.databaseName 代替 Alpha,它将根据条件具有数据库名称。

这该怎么做?请帮忙。谢谢。

4

2 回答 2

1

SELECT声明中,替换const char *sqlStatement = "SELECT * FROM Alpha";为以下两行:

NSString *sqlString = [NSString stringWithFormat:@"SELECT * FROM %@", self.databaseName];
const char *sqlStatement = [sqlString UTF8String];

或者

替换const char *sqlStatement = "SELECT * FROM Alpha";

const char *sqlStatement;
if (FULLVERSION) {
    sqlStatement = "SELECT * FROM Alpha";
} else {
    sqlStatement = "SELECT * FROM AlphaLite";
}
于 2013-03-29T10:04:18.893 回答
0

您可以使用 Stringwithformat ,然后获取 utf8string

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@",self.databaseName];
const char *sqlStatement = [sql UTF8String];
于 2013-03-29T10:05:20.740 回答