将 SQLite 数据库从特定行提取到文本字段中
我正在尝试连接到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接到
libsqlite3.dylib
(并导入
)以访问 SQLite 的强大功能。有许多轻量级 Objective-C 前端,我建议您选择一个。在此示例中,我使用fmdb
(https://github.com/ccgus/fmdb) 从之前创建的数据库中读取人员姓名:这说明了与数据库的通信;了解 SQL 取决于您(并且是另一个主题)。您可以在应用程序包中包含先前构建的 SQLite 文件,但不能在那里写入;解决方案是在开始使用它之前,将其从应用程序包复制到另一个位置,例如 Documents 目录。
最后,要将字符串放入文本字段 (UITextField),请设置其
text
属性。例如,我可以使用这些结果来设置文本字段值,而不是上面显示的while
循环(我在其中记录数据库结果):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 usefmdb
(https://github.com/ccgus/fmdb) to read the names of people out of a previously created database: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 thewhile
loop shown above, where I log the database results, I could use those results to set text field values: