[英]how to pass table name in the sqlite query
i need to get tabled name dynamically from the sqlite database. 我需要从sqlite数据库动态获取表名。
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. 我得到了一个警告未使用的变量tableName。
actual string is const char *sqlStatement = "select * from allcategories" ; 实际字符串为const char * sqlStatement =“从所有类别中选择*”;
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 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... 一个更好的选择是使此sqlStatement
为字符串,因为我不知道char是否可以存储NSString
对象。
Try: 尝试:
NSString *sqlStatement = [NSString stringWithFormat:@"select * from %@",tableName];
This will most certainly work, I tried it myself (provided your tableName
is correct). 我自己尝试过(如果您的tableName
正确),这肯定可以工作。
NSLog tableName
to see if you are passing the correct table name. NSLog tableName
来查看您是否传递了正确的表名。
Hope this helps. 希望这可以帮助。
const char *sqlStatement = "select * from %@",tableName ;
更改为
const char *sqlStatement = (const char *) [[NSString stringWithFormat:@"select * from %@", tableName] UTF8String];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.