我有一个数据库有一个'字' table(存储单词)我希望检索并将数据发送到另一个ViewController,但是我没有收到实际的单词,而是获得了id
。
这是我的代码:
(void)getListData {
//tableData = [[NSMutableArray alloc]init];
sqlite3_stmt *statement;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dexternotedb.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
WordListViewController *temp = APP_DELEGATE.wordListViewController;
NSLog(@"word id %ld",(long)[temp.wordId integerValue]);
NSString *sql=[NSString stringWithFormat:@"SELECT * FROM words WHERE word_id=\"%@\"",APP_DELEGATE.wordListViewController.wordId];
const char *read_stmt = [sql UTF8String];
NSLog(@"select query%@",sql);
if(sqlite3_prepare_v2(database, read_stmt, -1, &statement, NULL) == SQLITE_OK){
sqlite3_bind_int(statement, 1, [temp.wordId integerValue]);
while(sqlite3_step(statement) == SQLITE_ROW) {
NSString *word1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
[word addObject:word1];
NSString *meaning1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
[meaning addObject:meaning1];
NSString *sentence1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
[Sentence addObject:sentence1];
}
sqlite3_finalize(statement);
} else {
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
答案 0 :(得分:0)
首先:
不要使用“Select *”,因为您实际上并不知道列的顺序,而是使用列索引来访问它们。
所以你应该使用真实姓名“Select,id,word ....”
第二
所有列检索都指向相同的索引0:
[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
这就是为什么你得到相同的数据(id),因为它只有第一个索引。因此,将其更改为正确的索引1,2,3 ......您将获得正确的数据..
使用此SQL(检查列名是否正确)
SELECT word_id, word, meaning, sentence FROM words WHERE word_id=\"%@\"
然后您可以使用sqlite3_column_text的正确索引:
NSString *word1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
[word addObject:word1];
NSString *meaning1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
[meaning addObject:meaning1];
NSString *sentence1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
[Sentence addObject:sentence1];