Sqlite:创建一个表并仅填充一次信息
我有一个 appcelerator titan 项目(你不需要熟悉该平台来帮助我),我用它来创建一个 iOS 应用程序。我想要一个数据库,其中的表第一次填充了几行,然后在第一次之后就不再保留。
我知道如果表不存在,您可以创建一个表。有类似插入数据的东西吗?
谢谢!
I have an appcelerator titanium project (you don't need to be familiar with the platform to help me) that I am using to create an iOS app. I want to have a database with a table that is filled with several rows for the first time, and then left alone after the first time.
I know you can create a table if it doesn't exist. Is there something similar for Inserting data?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用一些特定于 Titanium 的代码扩展 MPelletier 的响应,您可以在项目中执行以下操作:
记录变量将指示表中是否有数据,然后您可以采取相应的操作。
Titanium 有几个不错的类似 ORM 的实用程序:TiStore 和 Joli 是我使用过的两个。两者都受到 ActiveRecord 的启发,有助于减少与数据库相关的代码。如果您想了解更多信息,请访问 Github!
Expanding on MPelletier's response with some Titanium-specific code, you could do the following in your project:
The records variable will indicate whether or not you have data in your table and then you can act accordingly.
There are a couple of nice ORM-ish utilities out there for Titanium: TiStore and Joli are the two I've used. Both are inspired by ActiveRecord and can be helpful in reducing your DB-related code. They're on Github if you want to know more about them!
如果您愿意,可以使用 INSERT 或 REPLACE,但您也可以只检查表中的行数:
然后决定在其中输入它们。
There's INSERT OR REPLACE if you want, but you might as well just check against the number of rows on the table:
Then decide to enter them on that.
您可以创建数据库并通过 SQLLiteManager 或任何您想要的工具在其中插入数据,然后转储它。
取出您转储的文件,将其放入您的 Titanium 项目文件夹中(Resources 文件夹中的某个位置)。
然后这行代码将获取 .sqlite 文件的内容并将其插入到 iOS 数据库中:
只是不要忘记 SQL 文件中的 CREATE IF NOT EXISTS 语句。
或者像 MPelletier 所说的那样使用 REPLACE 而不是 INSERT 。
You can create your DB and insert data in it via SQLLiteManager or whatever tool you want and then dump it.
Take the file you dumped, put it in your Titanium project folder (somewhere in the Resources folder).
Then this line of code will take the content of the .sqlite file and insert it in the iOS db:
Just don't forget the CREATE IF NOT EXISTS statement in your SQL file.
Or use REPLACE instead of INSERT as MPelletier said.