gpt4 book ai didi

ios - 从 iPhone 应用程序中的数据库读取

转载 作者:行者123 更新时间:2023-11-29 02:06:18 24 4
gpt4 key购买 nike

下面是我从数据库读取的代码:

if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *query_stmt = "SELECT QuestionID, question, RightAnswer,WrongAnswer1, WrongAnswer2, audio FROM questions WHERE done='no' AND Catagory='%@'", Catagory;

int check = (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL));
NSLog(@"%i",check);


if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{

while(sqlite3_step(statement) == SQLITE_ROW)
{
questionID = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
questionString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
rightAnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
wrong1AnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
wrong2AnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
audioInteger = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];
break;
}

sqlite3_finalize(statement);
}
sqlite3_close(database);
}

我想读取表的第一行,其中 done='no' 且类别与变量类别中存储的类别相匹配。我认为我可以使用 while 语句然后在它循环之前中断来做到这一点。然而,while 语句的条件永远不会满足。这向我表明 sql 语句没有返回任何值。

数据库SQL如下:

DROP TABLE IF EXISTS "Questions";
CREATE TABLE "Questions" ("QuestionID" INTEGER PRIMARY KEY NOT NULL , "Question" TEXT, "RightAnswer" TEXT, "WrongAnswer1" TEXT, "WrongAnswer2" TEXT, "Done" BOOL, "Catagory" TEXT, "Audio" INTEGER);
INSERT INTO "Questions" VALUES(1,'What is the BPM of this piece?','120 BPM','70 BPM','170 BPM','no','Rhythm',1);
INSERT INTO "Questions" VALUES(2,'How far apart are the 2 notes the bass guitar plays?','4 Semitones','1 Semitone','6 Tones','no','Pitch',1);
INSERT INTO "Questions" VALUES(3,'How is the organ Panned?','Left','Right','Centre','no','Pan',1);
INSERT INTO "Questions" VALUES(4,'What are the note values of the HiHat rhythms?','Semi quaver','Quaver','Crotchet','no','Rhythm',1);
INSERT INTO "Questions" VALUES(5,'The first chord played on the organ is...','Minor','Major','Atonal','no','Pitch',1);
INSERT INTO "Questions" VALUES(6,'How is the bass sound panned in this piece?','Right','Left','Center','no','Pan',2);
INSERT INTO "Questions" VALUES(7,'How is the lead synth panned in this piece?','Left','Right','Center','no','Pan',2);
INSERT INTO "Questions" VALUES(8,'The Kick Drum Rhythm In this can abe described as...','Four to the Floor','Off beat','Minor','no','Rhythm',2);
INSERT INTO "Questions" VALUES(9,'A clap sound can be heard on beat..','2 and 4','1 and 3','3','no','Rhythm',2);
DROP TABLE IF EXISTS "Results";
CREATE TABLE "Results" ("ResultID" INTEGER PRIMARY KEY NOT NULL , "QuestionRight" INTEGER, "TotalQuestions" INTEGER, "Catagory" TEXT);

所以我想问的是:

1)有没有更好的方法只读取满足我的sql语句的第一行?

2) 我当前的方法不起作用是否有原因?

感谢您的帮助,

*************** 更新 *************

这是定义类别的地方:

CatagoryLoaded = [[NSUserDefaults standardUserDefaults] integerForKey:@"catagorysaved"];
switch (CatagoryLoaded) {
case 1:
CatagorySelected.text = [NSString stringWithFormat:@"Pan!"];
Catagory = @"pan";
break;
case 2:
CatagorySelected.text = [NSString stringWithFormat:@"Pitch!"];
Catagory = @"pitch";
break;
case 3:
CatagorySelected.text = [NSString stringWithFormat:@"Rhythm!"];
Catagory = @"rhythm";
break;
default:
break;
}

*************** 更新 2***************

我按照您的建议将其更改为 Limit 1,现在得到 thread1: signal SIGABRT

if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *query_stmt = "SELECT * FROM Questions WHERE Done='no' AND Catagory='%@' LIMIT 1", Catagory;

int check = (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL));
NSLog(@"%i",check);


if (sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{


questionID = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
questionString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
rightAnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
wrong1AnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)];
wrong2AnswerString = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 4)];
audioInteger = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 5)];


sqlite3_finalize(statement);
}
sqlite3_close(database);
}

*************** 更新 *************看起来语句将其保留为 '%@' 而不是存储在类别中的内容,知道为什么吗?

下面是一个屏幕截图的链接,显示了 SIGABRT 崩溃之前的变量。 (不会让我直接发布图片,因为我是这个网站的新手)

http://puu.sh/hkOxf/8c05b9489b.png

最佳答案

我唯一的猜测是您的“类别”变量无效。除此之外,SQL 语句看起来还不错。我不记得 SQLite 是否区分大小写,但将 Done 和 Questions 之类的东西放在正确的大小写中不会有什么坏处。

此外,出于性能原因,您始终可以在 SQL 语句的末尾添加“Limit 1”以获得第一行。或者只是将它从 WHILE 更改为 IF,因此它只运行一次,但是,我建议使用 LIMIT 语法。

NSString *query_stmt = [NSString stringWithFormat:"SELECT * FROM Questions WHERE Done='no' AND Catagory='%@' LIMIT 1", Catagory];

关于ios - 从 iPhone 应用程序中的数据库读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com