热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何以编程方式填充数据库-Howtoprogrammaticallyfilladatabase

IcurrentlyhaveaniPhoneappthatreadsdatafromanexternalXMLfileatstart-up,andthenwrite

I currently have an iPhone app that reads data from an external XML file at start-up, and then writes this data to the database (it only reads/writes data that the user's app has not seen before, though)

我目前有一个iPhone应用程序,它在启动时从外部XML文件中读取数据,然后将数据写入数据库(它只读取/写入用户应用程序以前从未见过的数据)

My concern is that there is going to be a back catalogue of data of several years, and that the first time the user runs the app it will have to read this in and be atrociously slow.

我担心的是,将会有一份几年的数据备份目录,而且用户第一次运行这个应用程序时,必须阅读它,而且速度非常慢。

Our proposed solution is to include this data "pre-built" into the applications database, so that it doesn't have to load in the archival data on first-load - that is already in the app when they purchase it.

我们提出的解决方案是将这些“预构建”的数据包含到应用程序数据库中,这样它就不必在第一次加载时加载应用程序中已经存在的存档数据。

My question is whether there is a way to automatically populate this data with data from, say, an XML file or something. The database is in SQLite. I would populate it by hand, but obviously this will take a very long time, so I was just wondering if anybody had a more...programmatic solution...

我的问题是,是否有一种方法可以用XML文件或其他东西的数据自动填充数据。数据库在SQLite中。我用手填充它,但显然这需要很长时间,所以我想知道是否有人有更多…编程解决方案…

3 个解决方案

#1


1  

I'm going to flesh out Jason's answer, I've marked my post as a community wiki so I shouldn't get any points for this.

我将充实杰森的回答,我已经把我的帖子标记为一个社区维基,所以我不应该为此得到任何分数。

He's not talking about a dummy app - write the app as you normally would, but check to see if the database exists in your main bundle before you call the code that populates the plist. You run that in the simulator, pull out the generated sqllite database, and add it to your project - if you only need to read from it, you can read it from the main bundle directory. If you need to do further writes then copy it into the writable documents area, and use it from there. So basically for the main user, the code to populate the DB would never be called...

他不是在说一个虚拟的应用程序——像你平常那样编写应用程序,而是在你调用填充plist的代码之前检查一下数据库是否存在于你的主包中。您可以在模拟器中运行它,取出生成的sqllite数据库,并将其添加到项目中——如果您只需要从它中读取,您可以从主bundle目录中读取它。如果您需要进行进一步的写操作,那么将它复制到可写文档区域,并从那里使用它。基本上对于主用户来说,填充DB的代码永远不会被调用。

The only downside is you also end up including the plist files you are reading from, even though you only need the database. You could make a different build target that was a copy of the main one with the only difference being that it held the plist files, while the main target you built for the app store did not.

唯一的缺点是,即使只需要数据库,也会包含正在读取的plist文件。您可以创建一个不同的构建目标,它是主目标的副本,惟一的区别是它保存了plist文件,而您为应用程序商店构建的主目标没有。

#2


1  

Not to take Jason's answering thunder, I can't comment yet so it has to be here.

不要接受杰森的回答,我还不能评论,所以必须在这里。

The nice thing is that you can access the filesystem of the simulator right on your Mac. I am away from mine at the moment or I could tell you exactly where to look, but I just find it by putting the name of the db file into searchlight and just running with that.

的好处是,你可以访问的文件系统模拟器在你的Mac。此刻我离开我或者我可以确切地告诉你在哪里看,但我发现它把db文件的名称为探照灯和运行。

You also do not need to wright any code to populate the db since you can use the command line tool to do the initial setup if that is more convenient.

您也不需要编写任何代码来填充db,因为如果更方便的话,您可以使用命令行工具进行初始设置。

You will need to copy it over though since resources are stored in the read only signed portion of the app bundle.

您将需要复制它,因为资源存储在应用程序包的只读签名部分。

#3


0  

I had the same problem of you using sqlite, on massive insert it's really slow. So the best way it's provide directly a filled sqlite database.

我也有同样的问题,你使用sqlite,在大型插入时,非常慢。因此,最好的方法是直接提供一个填充的sqlite数据库。

You have another way, instead of INSERT INTO, to populate a sqlite db. You can produce a csv file for each table and load into the tables using your computer and the sqlite shell:

您可以使用另一种方式来填充sqlite db,而不是插入。您可以为每个表生成一个csv文件,并使用您的计算机和sqlite shell将其加载到表中:

Just 2 simple commands:

只是两个简单的命令:

.separator SEPARATOR
.import FILE TABLE 

Example:

例子:

adslol:~ user$ sqlite3 
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .separator ;
sqlite> .import myData.csv nameOfMyTable
sqlite> .quit

I hope it's what you was looking for :)

我希望这就是你想要的

If you need a good client for sqlite3 try SQLite Manager, it's a Firefox add-ons.

如果你需要一个好的sqlite3客户端,可以试试SQLite Manager,它是一个Firefox插件。


推荐阅读
author-avatar
大美女慧慧慧
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有