如何打印数据库中的表? (Android的,SQLite的) [英] How do I print database as a table? (Android, SQLite)

查看:371
本文介绍了如何打印数据库中的表? (Android的,SQLite的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地创建了一个数据库,在这几个表,并成功入选的所有表与rawQuery。所有的桌子都差不多,我的意思是,他们有不同的值相同的列。
现在我需要打印新活动的所有表。我想通过在TableLayout添加行做到这一点。每个新表将被印在一个新行。

I've successfully created a database with several tables in it and successfully selected all the tables with rawQuery. All the tables are similar, I mean they have the same columns with different values. Now I need to print all the tables in a new Activity. I want to do it by adding rows in TableLayout. Each new table will be printed in a new row.

请纠正我还是表现出这样做的另一种方式。我这样做,是这样的:

Please, correct me or show another way of doing it. I do it like this:

//添加新TABLES

//ADDING NEW 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

//SELECTING

SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
     Cursor c = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table'",null);

//试图创建新行时迎接新标题

//TRYING TO CREATE NEW ROWS WHEN MEET NEW 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列-1场插槽。

But DDMS says it can't get field slot from row 1 col -1.

顺便说一句,我只拿到了2列每个表,但CursorWindow说,有5列。

By the way, I've got only 2 columns in each table, but CursorWindow says there are 5 columns.

请帮助!

推荐答案

在拉光标只包含一列,表名,这是当您尝试访问不列,为什么你得到错误列存在于该游标。

The cursor you pulled contains only one column, the table names, which is why you're getting the column error when you try to access columns that don't exist in that cursor.

您需要建立一个循环使用从游标表名来查询每个表的表中的内容,并使用表内容光标(S)填写表格。

You need to set up a loop using the table names from that cursor to query for the table contents of each table, and use the table contents cursor(s) to fill your table.

取决于你如何做到这一点,你可能需要使用MergeCursor。

Depending on how you do it you might need to use MergeCursor.

修改

返回的前两个表是系统表,所以你应该跳过他们,在光标第三项中启动。

The first two tables returned are system tables, so you should skip over them and start at the third entry in the cursor.

c.moveToFirst();
c.moveToPosition(3);  // may be two, not sure if the cursor starts at 0 or 1
while (c.isAfterLast == false) {
    String tblName = c.getString(1);
    Cursor table = db.rawQuery("SELECT * FROM " + tblName,null);
    table.moveToFirst();
    if (table.getString(table.getColumnIndex("Title")) != null) {
        //  Your code to create table
    }
    c.moveToNext();
}

这篇关于如何打印数据库中的表? (Android的,SQLite的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆