iphone - 如何在sqlite查询中传递表名

标签 iphone objective-c ios

我需要从sqlite数据库动态获取表格名称。

我的代码

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


}

但是我没有得到。

我收到一条警告,未使用变量 tableName。

实际字符串是 const char *sqlStatement = "select * from allcategories";

我如何在该类别中动态传递该表名称。

谁能帮帮我吗?

提前谢谢你。

最佳答案

试试这个,

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

关于iphone - 如何在sqlite查询中传递表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5337954/

相关文章:

iphone - 更改 iPhone 启动画面时间

ios - 返回应用程序按钮,从我的应用程序开始

ios - 在单个 UICollectionView 和 UIView 中使用布局组合

ios - 从一个 View Controller 向另一个 View Controller 发出传递值

ios - 如何检查 Parse key 是否为空?如果是这样,那么将其更改为 0,而不是什么都没有

iphone - 在 NSDateFormatter 中更改一周的第一天

iphone - 在 IOS 中实现 AES256 加密

iphone - iOS 中图像的 CITemperatureAndTint

objective-c - 关闭键盘的简单方法?

c++ - 将源文件类型更改为 Objective-C++ 后的链接器错误