Android:SQLite到csv导出

时间:2018-06-04 14:02:34

标签: android sqlite export-to-csv opencsv

我无法将SQLite数据导出到csv file.i也无法看到任何LOGCAT。在Manifest文件中授予必要的权限。你能检查并清楚你的专业知识。

还会加载opencsv和其他支持文件。 清单文件。附加清单文件,您的审查的主要活动数据,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.bar.example.myapplication">

  <uses-permission android:name="android.permission.CAMERA" />

  <uses-feature android:name="android.hardware.camera" />
  <uses-feature android:name="android.hardware.camera.autofocus" />

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.INTERNET" />

  <uses-feature android:name="android.hardware.camera" />

  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

  <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    <activity android:name=".CourseSearchActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".ScanActivity" />
  </application>

</manifest>

Mainactivity。

btnexport.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    SQLiteDatabase db = openOrCreateDatabase("OCC", MODE_PRIVATE, null);
    File exportDir = new File(getExternalFilesDir(null), "");
    if (!exportDir.exists()) {
      exportDir.mkdirs();
    }

    File file = new File(exportDir, "coke.csv");
    try {
      file.createNewFile();
      com.bar.example.myapplication.CSVWriter csvWrite = new com.bar.example.myapplication.CSVWriter(new FileWriter(file));

      Cursor curCSV = db.rawQuery("SELECT * FROM DATAALL", null);
      csvWrite.writeNext(curCSV.getColumnNames());
      while (curCSV.moveToNext()) {
        //Which column you want to export
        String arrStr[] = {
          curCSV.getString(curCSV.getColumnIndex("_id")),
          curCSV.getString(curCSV.getColumnIndex("Material_Barcode")),
          curCSV.getString(curCSV.getColumnIndex("Material_Number")),
          curCSV.getString(curCSV.getColumnIndex("Material_Description")),
          curCSV.getString(curCSV.getColumnIndex("Scanning_Date")),
          curCSV.getString(curCSV.getColumnIndex("Production_Date")),
          curCSV.getString(curCSV.getColumnIndex("Expiry_Ratio")),
          curCSV.getString(curCSV.getColumnIndex("Product_Durablity")),
          curCSV.getString(curCSV.getColumnIndex("Qty")),
        };
        csvWrite.writeNext(arrStr);
      }
      csvWrite.close();
      curCSV.close();
      Toast.makeText(getApplicationContext(), "Downloaded Successfully", Toast.LENGTH_SHORT).show();

    } catch (Exception sqlEx) {
      Log.e("ResultActivity", sqlEx.getMessage(), sqlEx);
    }

  }

});

0 个答案:

没有答案