2014-01-08 93 views
0

我想將單行轉換爲.csv文件。我該怎麼做?我在互聯網上看到的大部分都是整個sqlite數據庫被轉換/導出爲.csv文件。但我的要求只是一個單一的記錄?你有什麼想法,我會如何實現這一目標?非常感謝幫助。謝謝!Android將sqlite單記錄轉換爲csv

更新:

public class CSVCreationActivity extends Activity { 
TextView empidtxt,empnametxt,empsaltxt; 
EditText empidet,empnameet,empsalet; 
Button insetbt,viewbt; 
SQLiteDatabase myDatabase=null; 
String DataBase_Name="employeedata"; 
String Table_Name="employeedetails"; 
Cursor c1,c2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

empidtxt=(TextView)findViewById(R.id.tv1); 
empnametxt=(TextView)findViewById(R.id.tv2); 
empsaltxt=(TextView)findViewById(R.id.tv3); 

empidet=(EditText)findViewById(R.id.et1); 
empnameet=(EditText)findViewById(R.id.et2); 
empsalet=(EditText)findViewById(R.id.et3); 

insetbt=(Button)findViewById(R.id.bt1); 
viewbt=(Button)findViewById(R.id.bt2); 

try { 
    myDatabase=this.openOrCreateDatabase(DataBase_Name, MODE_PRIVATE, null); 
    System.out.println("databse has been created....."); 
    myDatabase.execSQL("create table if not exists " + Table_Name + "(empid integer(10),empname varchar(50),empsal integer(10))"); 
    System.out.println("table has been created....."); 
    c1 = myDatabase.rawQuery("select * from " + Table_Name, null); 
    c1.moveToFirst(); 
    int count1 = c1.getCount(); 
    System.out.println("columns --->" + count1); 
    if (count1 == 0) { 
     myDatabase.execSQL("insert into "+Table_Name+ "(empid,empname,empsal)" +"values(101,'asha',20000)"); 
     System.out.println("data base has been inserted....."); 
    } 

    c2 = myDatabase.rawQuery("select * from " + Table_Name, null); 
    c2.moveToFirst(); 
    int count2 = c2.getCount(); 
    System.out.println("columns --->" + count2); 
    final int column1 = c2.getColumnIndex("empid"); 
    final int column2 = c2.getColumnIndex("empname"); 
    final int column3 = c2.getColumnIndex("empsal"); 

    insetbt.setOnClickListener(new View.OnClickListener() {    
     @Override 
     public void onClick(View v) { 
      if (c2 != null) { 
       do { 
        int id = c2.getInt(column1); 
        String name = c2.getString(column2); 
        int salary = c2.getInt(column3); 

        System.out.println("empID --> "+id); 
        System.out.println("empNAME --> "+name); 
        System.out.println("empsalalry --> "+salary); 
       } while(c2.moveToNext()); 
      }         
     } 
    }); 

    viewbt.setOnClickListener(new View.OnClickListener() {    
     @Override 
     public void onClick(View v) { 
      try { 
       new ExportDatabaseCSVTask().execute(""); 
      } catch(Exception ex) { 
       Log.e("Error in MainActivity",ex.toString()); 
      } 
     } 
    }); 
} 
catch(SQLException ex) { ex.printStackTrace(); } 
/*finally { 
    if (myDB != null) { myDB.close(); } 
}*/ 
} 


public class ExportDatabaseCSVTask extends AsyncTask<String, Void, Boolean> { 
private final ProgressDialog dialog = new ProgressDialog(CSVCreationActivity.this); 

@Override 
protected void onPreExecute() { 
    this.dialog.setMessage("Exporting database..."); 
    this.dialog.show(); 
} 

protected Boolean doInBackground(final String... args) { 
    File dbFile = getDatabasePath("myDatabase.db"); 
    System.out.println(dbFile); // displays the data base path in your logcat 
    File exportDir = new File(Environment.getExternalStorageDirectory(), ""); 

    if (!exportDir.exists()) { exportDir.mkdirs(); } 

    File file = new File(exportDir, "myfile.csv"); 
    try { 
     file.createNewFile(); 
     CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); 
     Cursor curCSV = myDatabase.rawQuery("select * from " + Table_Name,null); 
     csvWrite.writeNext(curCSV.getColumnNames()); 
     while(curCSV.moveToNext()) { 
      String arrStr[] ={curCSV.getString(0),curCSV.getString(1),curCSV.getString(2)}; 
      // curCSV.getString(3),curCSV.getString(4)}; 
      csvWrite.writeNext(arrStr); 
     } 
     csvWrite.close(); 
     curCSV.close(); 
     return true; 
    } catch(SQLException sqlEx) { 
     Log.e("MainActivity", sqlEx.getMessage(), sqlEx); 
     return false; 
    } catch (IOException e) { 
     Log.e("MainActivity", e.getMessage(), e); 
     return false; 
    } 
} 

protected void onPostExecute(final Boolean success) { 
    if (this.dialog.isShowing()) { this.dialog.dismiss(); } 
    if (success) { 
     Toast.makeText(CSVCreationActivity.this, "Export successful!", Toast.LENGTH_SHORT).show(); 
    } else { 
     Toast.makeText(CSVCreationActivity.this, "Export failed", Toast.LENGTH_SHORT).show(); 
    } 
} 
} 

CSVWriter和CSVReader可以下載here

+0

提供一些代碼 – Rohit

+0

@Rohit好再堅持一秒鐘。 –

+0

首先閱讀你的行,並創建此鏈接的​​csv文件的幫助http://stackoverflow.com/questions/4632501/android-generate-csv-file-from-table-values – Rohit

回答

1

更好的辦法從數據庫中獲取特定的記錄,然後將其轉換爲csv文件...

你只需要修改您的查詢來達到此目的

替換此行

Cursor curCSV = myDatabase.rawQuery("select * from " + Table_Name,null); 

與烏爾條件查詢是指使用其中運營商或其他任何東西......

+0

讓我試試這個。 –