开发者

how to pass table name in the sqlite query

开发者 https://www.devze.com 2023-02-18 12:52 出处:网络
i need to get tabled name dynamically from the sqlite database. my code -(void) readItemsFromDatabaseforTable:(NSString *)tableName {

i need to get tabled name dynamically from the sqlite database.

my code

-(void) readItemsFromDatabaseforTable:(NSString *)tableName {
    // Setup the database object
    sqlite3 *database;

    // Init the animals Array
    itemsList = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select * from %@",tableName ;
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSInteger aDescription =(compiledStatement, 2);
                //  NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                // Create a new animal object with the data from the database
                Category *item = [[Category alloc] initWithName:aName Quantity:aDescription];

                // Add the animal object to the animals Array开发者_开发技巧
                [itemsList addObject:item];

                [item release];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);


}

but i did n't get.

I got a warning unused variable tableName.

actual string is const char *sqlStatement = "select * from allcategories" ;

how can i pass that table name dynamically in that category.

can any one please help me.

Thank u in advance.


Try this,

NSString *sql_str = [NSString stringWithFormat:@"select * from %@", tableName];
const char *sqlStatement = (char *)[sql_str UTF8String];


const char *sqlStatement = "select * from %@",tableName ;

change above line to

const char *sqlStatement = (const char *) [[NSString stringWithFormat:@"select * from %@", tableName]  UTF8String];


const char *sqlStatement = "select * from %@",tableName ;

Change this line to:

const char *sqlStatement = [NSString stringWithFormat: @"select * from %@",tableName];

A better option would be to make this sqlStatement a string because i dont know if char can store an NSString object or not...

Try:

NSString *sqlStatement = [NSString stringWithFormat:@"select * from %@",tableName];

This will most certainly work, I tried it myself (provided your tableName is correct).

NSLog tableName to see if you are passing the correct table name.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消