2014-04-18 38 views
5

美好的一天。我有一個應用程序使用包含五個數據庫表的數據庫,我想要做的是能夠將這些表中的兩個導出爲CSV文件併發送給他們。在進行初步研究後,我看到OpenCSV Library是將SQLite表導出爲CSV的流行選擇。然而,經過進一步的研究,我發現通過給writeAll(ResultSet rSet, Boolean includeHeaders)函數提供一個ResultSet對象可以實現導出,但是沒有明確顯示如何獲得ResultSet。我試圖尋找關於如何在DatabaseHelper類中使用ResultSet的指南,但我空了。Android - 從SQLite數據庫獲取ResultSet,然後用於OpenCSV

任何人都可以教我如何在我的DatabaseHelper類中實現一個函數,它將返回查詢結果的ResultSet?這裏是我DatabaseHelper類:

public class OrderListDBAdapter { 

    private DatabaseHelper mDBHelper; 
    private SQLiteDatabase mDB; 
    private final Context context; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

     DatabaseHelper(Context context) { 
      super(context, "itemList.db", null, 1); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     } 
    } 


    public OrderListDBAdapter(Context context){ 
     this.context = context; 
    } 

    public OrderListDBAdapter open() throws SQLException{ 
     this.mDBHelper = new DatabaseHelper(this.context); 
     //this.mDB = this.mDBHelper.getWritableDatabase(DBAdapter.key); 
     this.mDB = this.mDBHelper.getWritableDatabase(); 
     return this;  
    } 

    public void close(){ 
     this.mDBHelper.close(); 
    } 

    //TODO - crud queries 
    public long insertOrder(OrderListClass order){ 
     Log.d("Angelo", "Hi, insert Order entered"); 
     ContentValues values = new ContentValues(); 

     values.put("orderDate", order.getOrderDate()); 
     values.put("customer", order.getCustomer()); 
     values.put("agent", order.getAgent()); 
     values.put("itemName", order.getItemName()); 
     values.put("price", order.getPrice()); 
     values.put("discount", order.getDiscount()); 
     values.put("quantity", order.getQuantity()); 
     values.put("requestedDiscount", order.getRequestedDiscount()); 

     return this.mDB.insert("OrderList", null, values); 
    } 

    public boolean deleteOrder(String productname) { 
     Log.d("Angelo", "Hi, delete Order entered"); 
     return mDB.delete("OrderList", "itemName =" + productname, null) > 0; 
    } 

    public int updateOrder(OrderListClass order) { 
     Log.d("Angelo", "Hi, update Order entered"); 

     ContentValues values = new ContentValues(); 
     values.put("orderDate", order.getOrderDate()); 
     values.put("customer", order.getCustomer()); 
     values.put("agent", order.getAgent()); 
     values.put("itemName", order.getItemName()); 
     values.put("price", order.getPrice()); 
     values.put("discount", order.getDiscount()); 
     values.put("quantity", order.getQuantity()); 
     values.put("requestedDiscount", order.getRequestedDiscount()); 

     // updating row 
     return mDB.update("OrderList", values, "itemName" + " = ?", new String[] { order.getItemName() }); 
    } 

    public ArrayList<OrderSummaryListViewItem> getAllOrders(String date, String customerName){ 
     ArrayList<OrderSummaryListViewItem> orders = new ArrayList<OrderSummaryListViewItem>(); 

     String query = "SELECT itemName, price, discount, quantity, requestedDiscount FROM OrderList WHERE orderDate = '" 
       + date + "' AND customer = '" + customerName + "'"; 

     Cursor cursor = mDB.rawQuery(query, null); 
     if (cursor.moveToFirst()) { 
      do { 

       String quantity = cursor.getString(cursor.getColumnIndex("quantity")); 
       String itemName = cursor.getString(cursor.getColumnIndex("itemName")); 
       String discount = cursor.getString(cursor.getColumnIndex("discount")); 
       String price = cursor.getString(cursor.getColumnIndex("price")); 
       String discRequest = cursor.getString(cursor.getColumnIndex("requestedDiscount")); 

       OrderSummaryListViewItem entry = new OrderSummaryListViewItem(quantity, itemName, discount, price, discRequest); 

       orders.add(entry); 
      } while (cursor.moveToNext()); 
     } 

     cursor.close(); 

     return orders; 
    } 


} 

我有一種感覺,它類似於執行查詢,但是,而不是得到一個遊標,我應該得到一個結果和結果集返回到我的活動。

我也可以選擇將sqlite數據庫導出到csv文件。

回答

1

現在我完全一樣。

我覺得java.sql.ResultSetandroid.database.sqlite.SQLiteDatabase只是不兼容。

  • ConnectionStatementResultSet來自Java(註冊包java.sql)。

  • SQLiteDatabase來自Android和是不同的。它將返回Cursor而不是ResultSet

您可以嘗試不使用Android類,但使用Java類進行數據庫訪問,但我不確定這是否是個好主意。

我用CursorCSVWriter#writeNext(String[])代替。 This is my implementation

相關問題