我已經成功創建了一個包含多個表的數據庫,並使用rawQuery成功選擇了所有表。所有表格都是相似的,我的意思是它們具有不同值的相同列。 現在我需要在新的活動中打印所有表格。我想通過在TableLayout中添加行來實現。每個新表將被打印在一個新的行中。如何將數據庫打印爲表格? (Android,SQLite)
請糾正我或顯示其他方式。我不喜歡這樣寫道:
//添加新TABLES
EditText title = (EditText)findViewById(R.id.Title);
String Title = title.getText().toString();
EditText d = (EditText)findViewById(R.id.director);
String Director = d.getText().toString();
SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + Title + " (Title VARCHAR, Director VARCHAR);");
db.execSQL("INSERT INTO " + Title + "(Title) VALUES('" + Title + "');");
db.execSQL("INSERT INTO " + Title + " (Director) VALUES('" + Director + "');");
db.close();
startActivity(new Intent(Add.this, Browse.class));
// SELECTING
SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table'",null);
//試圖創建新行時迎接新的TITLE
c.moveToFirst();
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
while (c.moveToNext())
{
if (c.getString(c.getColumnIndex("Title")) != null)
{
String Title = c.getString(c.getColumnIndex("Title"));
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
TextView b = new TextView(this);
b.setText(Title);
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
c.moveToNext();
}
但是DDMS表示它不能從第1行col -1中獲得字段空位。順便說一下,每個表中只有2列,但CursorWindow表示有5列。
請幫忙!
爲什麼不使用像ORMLite一個很好的框架,消除了需要編寫SQL ? :) –
我需要使用SQLite,因爲我的老師問我。我會閱讀關於ORMLite的內容,因爲這很有趣,謝謝,但我必須使用SQLite完成它。 –