2012-05-13 29 views
1

我已經成功創建了一個包含多個表的數據庫,並使用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列。

請幫忙!

+0

爲什麼不使用像ORMLite一個很好的框架,消除了需要編寫SQL ? :) –

+0

我需要使用SQLite,因爲我的老師問我。我會閱讀關於ORMLite的內容,因爲這很有趣,謝謝,但我必須使用SQLite完成它。 –

回答

3

您拖動的遊標只包含一列,即表名,這就是爲什麼當您嘗試訪問該遊標中不存在的列時出現列錯誤的原因。

您需要使用該遊標中的表名設置循環來查詢每個表的表內容,並使用表內容遊標來填充表。

根據您的操作方式,您可能需要使用MergeCursor。

編輯返回

前兩個表是系統表,所以你應該跳過他們,在光標所在的第三項開始。

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(); 
} 
0

表沒有得到很好的android系統中清除難度比預期的那麼我犯了一個新招數來填充下一個水平文本視圖佈局正常的列表視圖這裏是如何只要按照我的領導,這是用來檢索隊的表

主要活動::

public class ViewAllScores extends ListActivity { 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
setContentView(R.layout.table_total_scores_by_exam); 

     generateTable(); 

    }private void generateTable() { 
     // TODO Auto-generated method stub 
     setContentView(R.layout.table_total_scores_by_exam); 
     mDbHelper = new DBhelp(this); 
     mDbHelper.open(); 
     String ExamID = "1"; 
     mNotesCursor = mDbHelper.getAllTeamsScoresByExam(ExamID); 

     startManagingCursor(mNotesCursor); 

     String[] from = new String[] { mDbHelper.KEY_TEAMS_TEAMNAME, 
       mDbHelper.KEY_SCORES_TOTALSCORE, 
       mDbHelper.KEY_SCORES_TIMEELAPSED }; 

     int[] to = new int[] { R.id.tblTablesTeamsByExamRowsTeamName, 
       R.id.tblTablesTeamsByExamRowsTeamScore, 
       R.id.tblTablesTeamsByExamRowsElapsedTime }; 
     SimpleCursorAdapter notes = new SimpleCursorAdapter(this, 
       R.layout.table_teams_by_exams_rows, mNotesCursor, from, to); 

     setListAdapter(notes); 

    } 

數據庫活動:

public Cursor getAllTeamsScoresByExam(String examID) { 
     // TODO Auto-generated method stub 
     String where = KEY_SCORES_SCOREDEXAMID + "=?"; 

     String[] whereArgs = new String[] { examID }; 

     Cursor cursor = ourDatabase.query(VIEW_TEAMS_SCORES_BY_EXAM, 
       new String[] { KEY_SCORES_SCOREDEXAMID, KEY_SCORES_TIMEELAPSED, 
         KEY_SCORES_TOTALSCORE ,KEY_SCORES_TEAMTRANSID, 
         KEY_TEAMS_TEAMNAME }, where, whereArgs, null, null, 
       KEY_SCORES_TOTALSCORE+" DESC"); 

     return cursor; 
    } 
從數據庫中的分數

R.layout.table_total_scores_by_exam:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="99" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="33" 
      android:background="@drawable/cell_shape" 
      android:gravity="center" 
      android:text="Team Name" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="33" 
      android:background="@drawable/cell_shape" 
      android:gravity="center" 
      android:text="Total Score" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="33" 
      android:background="@drawable/cell_shape" 
      android:gravity="center" 
      android:text="Elapsed Time" 
      android:textSize="30sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

R.layout.table_teams_by_exams_rows:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="99" > 

    <TextView 
     android:id="@+id/tblTablesTeamsByExamRowsTeamName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="33" 
     android:background="@drawable/cell_shape" 
     android:gravity="center" 
     android:text="Team Name" 
     android:textSize="22sp" 
     /> 

    <TextView 
     android:id="@+id/tblTablesTeamsByExamRowsTeamScore" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="33" 
     android:background="@drawable/cell_shape" 
     android:gravity="center" 
     android:text="Total Score" 
     android:textSize="22sp" 
     /> 

    <TextView 
     android:id="@+id/tblTablesTeamsByExamRowsElapsedTime" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="33" 
     android:background="@drawable/cell_shape" 
     android:gravity="center" 
     android:text="Elapsed Time" 
     android:textSize="22sp" 
     /> 

</LinearLayout> 

希望這一招可以幫助