xcode iphone SQLite 选择查询不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1108003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iphone SQLite Select Query doesn't work
提问by oxigen
In the code below, I am connecting to an SQLite Database, the SELECT query didn't work.
在下面的代码中,我连接到 SQLite 数据库,SELECT 查询不起作用。
I hope you can help me.
我希望你能帮助我。
Thanks
谢谢
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select name,score from game Where name='interclock'";
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) {
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sonu?" message:[NSString stringWithFormat:@"Oyun ad? %s Skor:%s",aName,aScore]
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
//NSString *aName = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 2)];
//NSString *aScore = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 3)];
// Create a new animal object with the data from the database
DatabaseClass *dbOBJ = [[DatabaseClass alloc] initWithName:aName score:aScore];
// Add the animal object to the animals Array
[scores addObject:dbOBJ];
[dbOBJ release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"SQL Query Dont Work"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Connection"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
回答by oxigen
This is incorrect
这是不正确的
NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
must be
必须是
NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
回答by Jasseem
It could be correct depending on what column you want to get. In this example, I think that the table of the database contain 3 columns : id, aName, and aScore. The problem is not there but here:
根据您想要获得的列,它可能是正确的。在这个例子中,我认为数据库的表包含 3 列:id、aName 和 aScore。问题不在这里,而是在这里:
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
Verify that your compiledStatement
returns a good value.
验证您的compiledStatement
返回值是否合理。
回答by Savas Adar
you should get path like this;
你应该得到这样的路径;
NSString *path = [[NSBundle mainBundle]pathForResource:@"sarkiSozleri"ofType:@"sqlite"];