2017-11-18 372 views
0

我想從我的數據庫從某些領域的報告到PDF。 pdf正在完美生成,但它只是從數據庫中讀取第一個條目而不是所有條目。如何讓光標從SQLite數據庫中讀取下一行?

這是我對PDF的代碼:

public void exportDataBaseIntoPDF() { 
    Document doc = new Document(PageSize.A4); 
    try { 
     path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MSS Jobsheets"; 
     date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date()); 

     File dir = new File(path); 
     if (!dir.exists()) 
      dir.mkdirs(); 

     Log.d("PDFCreator", "PDF Path: " + path); 

     filePDF = new File(dir, "Report" + "(" + date + ")" + ".pdf"); 
     FileOutputStream fOut = null; 
     try { 
      fOut = new FileOutputStream(filePDF); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     PdfWriter.getInstance(doc, fOut); 
     doc.open(); 

     DBHelper db = new DBHelper(this); 
     Cursor cursor = db.getDataItem(); 
     int count = cursor.getCount(); 

     cursor.moveToFirst(); 

     for (int j = 0; j < count; j++) { 
      PdfPTable table = new PdfPTable(2); 
      table.addCell(cursor.getString(cursor.getColumnIndex("name"))); 
      table.addCell(cursor.getString(cursor.getColumnIndex("gender"))); 


      cursor.moveToNext(); 

      doc.add(table); 
      doc.close(); 
     } 

    } catch (DocumentException e) { 
     e.printStackTrace(); 
    } 
} 

這是從我的DBHelper類拉低數據:

public Cursor getDataItem() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String q = "SELECT * FROM " + PERSON_TABLE_NAME; 
    Cursor mCursor = db.rawQuery(q, null); 

    return mCursor; 
} 
} 

但就像我說的,它只是從數據庫讀取第一行並添加數據但不移動到數據庫中的下一行以將數據添加到PDF中。

有人可以幫我解決這個問題嗎? 在此先感謝!

+0

外面你有數據庫中的多行? –

+0

是的,我確實有。基本上它是一個用戶填寫的表單,然後在一天結束時用戶可以從某些領域獲取報告,然後發送電子郵件。 – Newbie

+0

嘗試關閉doc for side循環。寫這行doc.close();外側爲循環 – Munir

回答

1

實際問題與遊標無關,遊標讀取下一行但您在第一次寫入數據後關閉文檔。嘗試關閉for循環之外的文檔對象。

,就把這行到for loop

doc.close(); 
+0

謝謝!它的工作現在。我無法相信我是如何錯過的!我一直在努力奮鬥這個年齡! – Newbie

+0

@Newbie高興地幫助你..! – Munir