java sqlite3 csv,将SQLite数据库导出到android中的csv文件

我试图将SQLite数据导出到android中的SD卡作为目录上的CSV文件.

所以我在下面尝试过这种方法,显然它只显示了这个文字:

数据库的第一个表

日期,项目,金额,币种

在我的DBHelper.java中,我已经定义了如下函数:

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;

}

}

}

我的专栏是:

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";

如果您需要更多代码,请与我们联系.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值