应用程序的数据库中有很多表。在应用程序使用期间,这些表由用户及其名称创建。
我想将所有表导出,每个表都导出到具有表名的单个 .CSV 文件中。
例如,我有3个分别名为1、2和3的表。我要导出并创建文件1.csv,2.csv和3.csv。
我真的很感谢您的帮助,因为我无法在网络上找到任何答案。
我正在使用一些很受欢迎的类CSVReader和CSVWriter。
在我的导出方法中。它没有显示任何错误,但是没有导出文件:
public void exportTables (View view) {
File dbFile = getDatabasePath("database.db");
Banco dbhelper = new Banco(getApplicationContext());
File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, tableName +".csv");
try {
file.createNewFile();
CSVWriter csvWriter = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table' AND name NOT LIKE 'android_metadata' AND name NOT LIKE 'sqlite_sequence'", null);
csvWriter.writeNext(curCSV.getColumnNames());
while(curCSV.moveToNext()) {
// Table columns
String arrStr[] ={curCSV.getString(0),curCSV.getString(1)};
csvWriter.writeNext(arrStr);
}
csvWriter.close();
curCSV.close();
}
catch(Exception sqlEx) {
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}
Toast.makeText(this, "FILE EXPORTED WITH SUCCESS!", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
这实际上是一个两步过程:
1-列出表名
您的第一个查询
SELECT *
FROM sqlite_master
WHERE type='table'
AND name NOT LIKE 'android_metadata'
AND name NOT LIKE 'sqlite_sequence'
返回表列表,如下所示
type | name | tbl_name | rootpage | sql |
------+--------+----------+----------+------------------+
table | table1 | table1 | 2 | CREATE TABLE ... |
table | table2 | table2 | 2 | CREATE TABLE ... |
table | table3 | table3 | 2 | CREATE TABLE ... |
您可以简单地返回名称而不是所有列
SELECT name
FROM sqlite_master
WHERE type='table'
AND name NOT LIKE 'android_metadata'
AND name NOT LIKE 'sqlite_sequence'
您需要阅读每一行以获得表名
2-然后,需要为每个表创建一个文件并查询该表,例如:
SELECT * FROM table1
然后将它们粘贴到您的文件中
代码:
我没有测试该代码,但是您应该能够使它工作:
第一个方法将列出表名
private static List<String> listTables(SQLiteDatabase db) {
List<String> tableNames = new ArrayList<>();
Cursor tableListCursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'android_metadata' AND name NOT LIKE 'sqlite_sequence'", null);
while(tableListCursor.moveToNext()) {
// get the name of the table
tableNames.add(tableListCursor.getString(0));
}
tableListCursor.close();
return tableNames;
}
第二种方法会将表转储到文件
private static void dumpTableToCsvFile(SQLiteDatabase db, String tableName) {
try {
// Query the data in the table
Cursor cursor = db.rawQuery("SELECT * FROM " + tableName, null);
File file = new File(exportDir, tableName +".csv");
file.createNewFile();
CSVWriter csvWriter = new CSVWriter(new FileWriter(file));
// Write the table data to the file
csvWriter.writeNext(cursor.getColumnNames());
while(cursor.moveToNext()) {
String arrStr[] = { cursor.getString(0), cursor.getString(1) };
csvWriter.writeNext(arrStr);
}
// close cursor and writer
cursor.close();
csvWriter.close();
} catch (IOException e) {
// handle exception
}
}
您可以通过以下方式使用它们:
SQLiteDatabase db = dbhelper.getReadableDatabase();
List<String> tableNames = listTables(db);
for(String tableName : tableNames) {
dumpTableToCsvFile(db, tableName);
}