【问题标题】:Android - Export DB in a Text FileAndroid - 在文本文件中导出数据库
【发布时间】:2015-04-27 00:54:06
【问题描述】:

我有一个将数据存储在数据库中的应用程序。现在,我想要一个按钮来将此数据库导出到文本文件中。

这是我的导出按钮的代码:

//BUTTON ACTION
public void Save(View view) {

    //Saving the data into the data base
    SQLiteDatabase db = DataBase.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("name", name.getText().toString());
    values.put("phone", phone.getText().toString());
    values.put("email", email.getText().toString());
    values.put("job", job.getText().toString());
    values.put("others", others.getText().toString());

    long result= db.insert("table_customer", null, values);

    Cursor cursor = db.query ("table_customer", new String[]{"name", "phone", "email", "job", "others"}, null,null,null,null,null);

        if (cursor.moveToFirst()) {

            do {
                HashMap<String, String> customer = new HashMap<String, String>();
                cliente.put("_id_customer", cursor.getString(cursor.getColumnIndex("_id_customer")));
                cliente.put("name", cursor.getString(cursor.getColumnIndex("name")));
                cliente.put("phone", cursor.getString(cursor.getColumnIndex("phone")));
                cliente.put("email", cursor.getString(cursor.getColumnIndex("email")));
                cliente.put("job", cursor.getString(cursor.getColumnIndex("job")));
                cliente.put("others", cursor.getString(cursor.getColumnIndex("others")));
            }

            while (cursor.moveToNext());

        }

        cursor.close();

    if (resultado != -1) {
        Toast.makeText(this, "Customer saved and file exported", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(this, "Error 2", Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

  • 修复了代码格式并从问题中删除了不相关的部分

标签: java android database export


【解决方案1】:

您可以做的是使用全选查询依次查询每个表,并为每个表使用 StringBuilder 手动构建 XML 或 CSV(逗号分隔值)文件。

例如,如果 Persons 表具有以下列 {ID, FNAME, LNAME, AGE},那么对于包含两行数据的表,我可以为 XML 生成以下内容

<persons>
  <person>
    <ID>1</ID>
    <FNAME>Isaac</FNAME>
    <LNAME>Meacock</LNAME>
    <AGE>27</AGE>
  </person>
  <person>
    <ID>2</ID>
    <FNAMENorah</FNAME>
    <LNAME>Bone</LNAME>
    <AGE>52</AGE>
  </person>
<persons>

对于 CSV 或以下内容

1, Isaac, Meacock, 27
2, Norah, Bone, 53

那么只需用 Java 保存标准文本文件即可。

【讨论】:

    【解决方案2】:

    您需要使用db 对象的方法查询数据库。然后您需要迭代这些结果并将它们写入您的文件。在SQLiteDatabase 文档中查看可用的方法。

    编辑 1:

    试试这样的:

    Cursor cursor = db.query ("table_customer", new String[]{"name","phone", "email", "job", "others"},null,null,null,null,null);
    
    //cursor is now at the first result returned by this query
    do{
        int colIdx = cursor.getColumnIndex("name");
        String name = cursor.getString(colIdx);
        //do something with name
    }while(cursor.moveToNext()); //loop will terminate when all the rows are exhausted
    

    编辑 2: 这是一个link to an article,它详细说明了如何使用查询和游标功能。

    【讨论】:

    • 亲爱的雅各布。谢谢你的时间。好吧,很抱歉,如果我问得太多了……我想我知道该怎么做……但我不知道该怎么做……我对此真的很陌生,相信所有我正在努力使这一切发生。你能写一个示范吗?非常感谢!
    • 我认为您可能可以这样做。 Cursor cursor = db.query ("table_customer", new String[]{"name", "phone", "email", "job", "others"}, null,null,null,null,null); //cursor is now at the first result returned by this query do{ int colIdx = cursor.getColumnIndex("name"); String name = cursor.getString(colIdx); //do something with name }while(cursor.moveToNext());
    • 亲爱的@Jacob,我想我终于要开始做一些事情了,只是因为你的耐心......嗯......我上传了我的代码,因为这里没有更多的空间可以写......做你认为它正确吗?以及如何在文本文件中编写所有查询?再次感谢
    • @Vhox,它看起来是正确的,唯一的问题可能是查询中未选择“_id_customer”字段,因此可能无法正常工作。然而,这似乎是正确的。在android中编写文本文件,see here.
    • 是的,没有错误发生。但是现在,保存到文本文件中的命令是什么?我尝试使用 FileOutPuStream 创建文本文件。它有效,但我不知道如何在其中编写数据库......你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2023-03-30
    • 2019-06-07
    • 1970-01-01
    相关资源
    最近更新 更多