在 Android 设备上批量插入 [英] Bulk Insertion on Android device

查看:22
本文介绍了在 Android 设备上批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下次升级时将大约 700 条记录批量插入到 Android 数据库中.执行此操作的最有效方法是什么?从各种帖子中,我知道如果我使用 Insert 语句,我应该将它们包装在一个事务中.还有一个关于使用您自己的数据库的帖子,但我需要这些数据进入我的应用程序的标准 Android 数据库.请注意,每台设备只能执行一次.

I want to bulk insert about 700 records into the Android database on my next upgrade. What's the most efficient way to do this? From various posts, I know that if I use Insert statements, I should wrap them in a transaction. There's also a post about using your own database, but I need this data to go into my app's standard Android database. Note that this would only be done once per device.

一些想法:

  1. 将一堆SQL语句放在一个文件中,一行一行读取,然后执行SQL.

  1. Put a bunch of SQL statements in a file, read them in a line at a time, and exec the SQL.

将数据放入 CSV 文件、JSON、YAML、XML 或其他文件中.一次读取一行并执行db.insert().

Put the data in a CSV file, or JSON, or YAML, or XML, or whatever. Read a line at a time and do db.insert().

弄清楚如何进行导入和对整个文件进行一次导入.

Figure out how to do an import and do a single import of the entire file.

制作一个包含所有记录的 sqlite 数据库,将其复制到 Android 设备上,然后以某种方式合并两个数据库.

Make a sqlite database containing all the records, copy that onto the Android device, and somehow merge the two databases.

将所有 SQL 语句作为一个大字符串放在 res/values 中的单个文件中.然后一次读取一行并执行 SQL.

Put all the SQL statements in a single file in res/values as one big string. Then read them a line at a time and exec the SQL.

最好的方法是什么?还有其他方法可以加载数据吗?3 和 4 甚至可能吗?

What's the best way? Are there other ways to load data? Are 3 and 4 even possible?

推荐答案

我不认为有任何可行的方法可以完成您列表中的 #3 或 #4.

I don't believe there is any feasible way to accomplish #3 or #4 on your list.

在您列出的其他解决方案中,有两个数据文件包含直接 SQL,另一个具有非 SQL 格式的数据.

Of the other solutions you list two that have the datafile contain direct SQL, and the other has the data in a non-SQL format.

这三个都可以正常工作,但后一种从格式化文件中获取数据并自己构建 SQL 的建议似乎是最干净的.如果以后添加真正的批量更新功能,您的数据文件仍然可用,或者至少可以轻松处理成可用的形式.此外,数据文件的创建更加直接且不易出错.最后,拥有原始"数据将允许导入其他数据存储格式.

All three would work just fine, but the latter suggestion of grabbing the data from a formatted file and building the SQL yourself seems the cleanest. If true batch update capability is added at a later date your datafile is still usable, or at least easily processable into a usable form. Also, creation of the datafile is more straightforward and less error prone. Finally, having the "raw" data would allow import into other data-store formats.

在任何情况下,您都应该(如您所提到的)将插入组包装到事务中,以避免创建每行事务日志.

In any case, you should (as you mentioned) wrap the groups of inserts into transactions to avoid the per-row transaction journal creation.

这篇关于在 Android 设备上批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆