hgpl
hgpl

Reputation: 869

SQLite scripting with objective C

I have two .sqlite files. One for full version and one for Lite version. I am choosing one .sqlite file for one condition with this code:

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

Now when I query I want to use this self.databaseName in that. I am doing like this now:

-(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;   
}

In "SELECT * FROM Alpha" line, in place of Alpha I want to use self.databaseName, which will have database name according to the condition.

How to do this? Please help. Thanks.

Upvotes: 0

Views: 116

Answers (2)

viral
viral

Reputation: 4208

In SELECT statement, replace const char *sqlStatement = "SELECT * FROM Alpha"; with following two lines:

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

OR

Replace const char *sqlStatement = "SELECT * FROM Alpha"; with

const char *sqlStatement;
if (FULLVERSION) {
    sqlStatement = "SELECT * FROM Alpha";
} else {
    sqlStatement = "SELECT * FROM AlphaLite";
}

Upvotes: 1

ogres
ogres

Reputation: 3690

You could use Stringwithformat , then get utf8string

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

Upvotes: 0

Related Questions