如何将会议室表格导出为CSV格式? [英] How does one export a Room table to a CSV format?

查看:126
本文介绍了如何将会议室表格导出为CSV格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个应用程序,该应用程序使用多个实体将数据存储在Room数据库中.我需要的是获取表的内容,将数据转换为CSV格式,然后通过电子邮件发送该格式.我已经敲定了最后一部分,因为它只涉及通过ACTION_SEND通过意图发送文件.很简单.

I have an app right now that stores data in a Room database using several entities. What I need is to take the contents of a table, convert the data into a CSV format, and then to send that format via email. I have the very last part buttoned down, as it simply involves sending the file through an intent with ACTION_SEND. That's easy.

我了解可以获取实际数据库的路径并处理该文件.但是,Room的设计者竭尽全力将SQLite放入黑匣子,这似乎是个坏主意.

I understand that I can get the path to the actual database and manipulate that file. However, the designers of Room went to such great lengths to put SQLite into a black box that this seems like a bad idea.

我知道我也可以在其中一个DAO中编写查询方法,该方法可以返回CSV格式的字符串,然后可以将其转换为File对象.但是,这是很多工作,如果有人为我这样做,我也不会浪费我的时间.

I know that I can also write a query method in one of the DAOs that returns a string in CSV format that can then be turned into a File object. However, that is a lot of work, and I'm not going to waste my time if someone else has done so for me.

因此,这是我的问题:如何将Room数据库表导出为CSV格式的字符串?如果这样做有多种方法,那么有没有首选的方法?

So, here's my question: How do I export a Room database table into a CSV formatted string? If there are multiple ways of doing so, is there a preferred way?

奖金问题:是否有任何首选的方式来备份整个数据库?

Bonus question: Is there any preferred way to back up the entire database?

推荐答案

我了解可以获取实际数据库的路径并处理该文件.但是,Google竭尽全力掩盖了SQLite的细节,这似乎是个坏主意.

I understand that I can get the path to the actual database and manipulate that file. However, Google has gone to such lengths to obscure the SQLite details that this seems like a bad idea.

Google在您的RoomDatabase ,如果您有理由想更直接地使用数据库(例如,执行不在您的DAO中的任意SQL).

Google offers getOpenHelper() on your RoomDatabase, if you have a reason to want to work with the database more directly (e.g., execute arbitrary SQL that is not in your DAO).

我知道我也可以在其中一个DAO中编写查询方法,该方法可以返回CSV格式的字符串,然后可以将其转换为File对象

I know that I can also write a query method in one of the DAOs that returns a string in CSV format that can then be turned into a File object

恕我直言,创建CSV文件不是DAO的责任,不仅仅是填充小部件是DAO的责任.

IMHO, creating a CSV file is not the responsibility of a DAO, any more than populating widgets is the responsibility of a DAO.

如何将Room数据库表导出为CSV格式的字符串?

How do I export a Room database table into a CSV formatted string?

创建某种Report类,可以查询DAO,生成CSV并将其写入所需的位置.

Create some sort of Report class that can query the DAO, generate the CSV, and write it to your desired location.

或者,找到一些可以使用Cursor并从中生成CSV的现有代码.使用getOpenHelper()获取SupportSQLiteDatabase,可以在其上使用所需的SQL query()生成Cursor.

Or, find some existing code that can take a Cursor and generate a CSV from it. Use getOpenHelper() to get a SupportSQLiteDatabase, on which you can query() with your desired SQL to generate the Cursor.

有没有首选的方法来备份整个数据库?

Is there any preferred way to back up the entire database?

恕我直言,close() RoomDatabase,然后复制文件(

IMHO, close() the RoomDatabase, then copy the files (using getDatabasePath() and Java file I/O).

这篇关于如何将会议室表格导出为CSV格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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