【发布时间】:2012-05-13 17:19:30
【问题描述】:
我已经成功地创建了一个包含多个表的数据库,并成功地使用 rawQuery 选择了所有表。所有表都是相似的,我的意思是它们具有不同值的相同列。 现在我需要打印一个新活动中的所有表格。我想通过在 TableLayout 中添加行来做到这一点。每个新表都将打印在一个新行中。
请纠正我或显示另一种方法。我是这样做的:
//添加新表
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));
//选择中
SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table'",null);
//遇到新标题时尝试创建新行
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 来完成它。
标签: android sqlite tablelayout