2017-11-18 206 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

相关问题