Tôi đang cố gắng xuất dữ liệu SQLite sang thẻ SD trong android dưới dạng tệp CSV trên một thư mục.Xuất cơ sở dữ liệu SQLite sang tệp csv trong android
Vì vậy, tôi đã thử phương pháp dưới đây và dường như nó chỉ hiển thị văn bản này in ra:
BẢNG ĐẦU CỦA CƠ SỞ DỮ LIỆU
NGÀY, ITEM, AMOUNT, TỆ
Trong DBHelper.java của tôi tôi đã xác định các chức năng như sau:
public boolean exportDatabase() {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
/**First of all we check if the external storage of the device is available for writing.
* Remember that the external storage is not necessarily the sd card. Very often it is
* the device storage.
*/
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
return false;
}
else {
//We use the Download directory for saving our .csv file.
File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file;
PrintWriter printWriter = null;
try
{
file = new File(exportDir, "MyCSVFile.csv");
file.createNewFile();
printWriter = new PrintWriter(new FileWriter(file));
/**This is our database connector class that reads the data from the database.
* The code of this class is omitted for brevity.
*/
SQLiteDatabase db = this.getReadableDatabase(); //open the database for reading
/**Let's read the first table of the database.
* getFirstTable() is a method in our DBCOurDatabaseConnector class which retrieves a Cursor
* containing all records of the table (all fields).
* The code of this class is omitted for brevity.
*/
Cursor curCSV = db.rawQuery("select * from contacts", null);
//Write the name of the table and the name of the columns (comma separated values) in the .csv file.
printWriter.println("FIRST TABLE OF THE DATABASE");
printWriter.println("DATE,ITEM,AMOUNT,CURRENCY");
while(curCSV.moveToNext())
{
Long date = curCSV.getLong(curCSV.getColumnIndex("date"));
String title = curCSV.getString(curCSV.getColumnIndex("title"));
Float amount = curCSV.getFloat(curCSV.getColumnIndex("amount"));
String description = curCSV.getString(curCSV.getColumnIndex("description"));
/**Create the line to write in the .csv file.
* We need a String where values are comma separated.
* The field date (Long) is formatted in a readable text. The amount field
* is converted into String.
*/
String record = df.format(new Date(date)) + "," + title + "," + amount + "," + description;
printWriter.println(record); //write the record in the .csv file
}
curCSV.close();
db.close();
}
catch(Exception exc) {
//if there are any exceptions, return false
return false;
}
finally {
if(printWriter != null) printWriter.close();
}
//If there are no errors, return true.
return true;
}
}
}
Và cột của tôi là:
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TITLE = "title";
public static final String CONTACTS_COLUMN_AMOUNT = "amount";
public static final String CONTACTS_COLUMN_DESC = "description";
Hãy cho tôi biết nếu bạn cần thêm mã.
bạn muốn gì? bạn nhận được bất kỳ lỗi nào. – RajSharma
'// Viết tên của bảng và tên của các cột (giá trị được phân cách bằng dấu phẩy) trong tệp .csv.' Sau đó, nó không còn là tệp CSV hợp lệ nữa, nếu bạn thêm tên bảng. Dòng đầu tiên trong tệp CSV hợp lệ hoặc là hàng tiêu đề có tên cột hoặc chỉ là hàng dữ liệu đầu tiên. –
Đối với những gì liên quan đến vấn đề của bạn ... có vẻ như không có dữ liệu trong bảng. –