将 SQLite 数据库从特定行提取到文本字段中

发布于 2024-10-30 16:55:54 字数 275 浏览 1 评论 0原文

我正在尝试连接到 SQLite 数据库,并使用一种方法指定数据库中的特定行(数据库中的第一列是“ID”并且是主键),然后从该行中的其他一些列中提取信息并将它们显示在文本字段中。

这将用于我正在制作的简单问答游戏;稍后我将制作一个随机方法来随机选择行。

我已经为这个问题苦苦挣扎了几个星期,并且已经阅读了大量教程,但所有这些教程都涉及在表视图中显示数据,我想简单地将其显示在基于视图的应用程序中的文本字段上。我现在相当困惑,因此非常感谢从加载数据库到在文本字段中显示数据的任何帮助!

谢谢!

I am trying to connect to a SQLite database and have a method that specifies a specific row from the database (the first column in the database is “ID” and is a primary key) then extract the information from a few other columns in that row and display them in text fields.

This will be used for a simple Trivia game I am making; I will later make a random method that will choose the row at random.

I have been struggling with this problem for several weeks and I have been through loads of tutorials but all of them deal with displaying the data in a table view, I want to display it simply on text fields in a View based app. I am fairly confused at this point so any help starting from loading the database to displaying the data in the text fields would be GREATLY APPRECIATED!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-11-06 16:55:54

链接到 libsqlite3.dylib(并导入 )以访问 SQLite 的强大功能。有许多轻量级 Objective-C 前端,我建议您选择一个。在此示例中,我使用 fmdb (https://github.com/ccgus/fmdb) 从之前创建的数据库中读取人员姓名:

NSString* docsdir = [NSSearchPathForDirectoriesInDomains( 
    NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"];
FMDatabase* db = [FMDatabase databaseWithPath:dbpath];
if (![db open]) {
    NSLog(@"Ooops");
    return;
}
FMResultSet *rs = [db executeQuery:@"select * from people"];
while ([rs next]) {
    NSLog(@"%@ %@", 
        [rs stringForColumn:@"firstname"], 
        [rs stringForColumn:@"lastname"]);
}
[db close];
/* output:
Snidely Whiplash
Dudley Doright
*/

这说明了与数据库的通信;了解 SQL 取决于您(并且是另一个主题)。您可以在应用程序包中包含先前构建的 SQLite 文件,但不能在那里写入;解决方案是在开始使用它之前,将其从应用程序包复制到另一个位置,例如 Documents 目录。

最后,要将字符串放入文本字​​段 (UITextField),请设置其 text 属性。例如,我可以使用这些结果来设置文本字段值,而不是上面显示的 while 循环(我在其中记录数据库结果):

myTextField.text = [rs stringForColumn:@"firstname"];
myOtherTextField.text = [rs stringForColumn:@"lastname"];

Link to libsqlite3.dylib (and import <sqlite3.h>) to access the power of SQLite. There are a number of lightweight Objective-C front ends and I suggest you pick one. In this example, I use fmdb (https://github.com/ccgus/fmdb) to read the names of people out of a previously created database:

NSString* docsdir = [NSSearchPathForDirectoriesInDomains( 
    NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"];
FMDatabase* db = [FMDatabase databaseWithPath:dbpath];
if (![db open]) {
    NSLog(@"Ooops");
    return;
}
FMResultSet *rs = [db executeQuery:@"select * from people"];
while ([rs next]) {
    NSLog(@"%@ %@", 
        [rs stringForColumn:@"firstname"], 
        [rs stringForColumn:@"lastname"]);
}
[db close];
/* output:
Snidely Whiplash
Dudley Doright
*/

That illustrates talking to the database; knowing SQL is up to you (and is a different topic). You can include a previously constructed SQLite file in your app bundle, but you can't write to it there; the solution is to copy it from your app bundle into another location, such as the Documents directory, before you start working with it.

Finally, to put strings into text fields (UITextField), set their text property. So for example instead of the while loop shown above, where I log the database results, I could use those results to set text field values:

myTextField.text = [rs stringForColumn:@"firstname"];
myOtherTextField.text = [rs stringForColumn:@"lastname"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文