2012-03-24 62 views
0

這是我的代碼 `我創建了一個表,但我想創建兩個,當我點擊「顯示」按鈕時,我希望能夠從兩個表中選擇內容並告訴他們......這是我的代碼...我有創建兩個表,並顯示他們的問題:SQL android,創建多個表更多信息

public class Entername extends Activity { 

     private Button showButton; 
     private Button insertButton; 
     private TextView nameEditText; 
     private TextView addTextView; 
     private Button doneButton; 
     public DatabaseHelper dbHelper = new DatabaseHelper(Entername.this,"pubgolfdatabase",2); 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.entername); 
      addTextView = (TextView)findViewById(R.id.textView1); 
      doneButton= (Button)findViewById(R.id.doneButton); 
      insertButton = (Button)findViewById(R.id.addButton); 
      nameEditText = (EditText)findViewById(R.id.name); 
      showButton =(Button)findViewById(R.id.button1); 
      showButton.setOnClickListener(new showButtonListener()); 
      insertButton.setOnClickListener(new InsertButtonListener()); 
      doneButton.setOnClickListener(new DoneButtonListener()); 

      /** create the database if it dosen't exist **/ 
      SQLiteDatabase db = dbHelper.getWritableDatabase(); 
      try 
      { 
       db.execSQL("create table user_name(ID integer, name varchar(90));"); 

      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 

     class InsertButtonListener implements OnClickListener, android.view.View.OnClickListener 
     { 
      public void onClick(View v) 
      { 

       if("".equals(nameEditText.getText().toString())) 
       { 
        Toast toast = Toast.makeText(Entername.this, "Sorry, you must input both the name and the address!", Toast.LENGTH_LONG); 
        toast.show(); 
       } 
       else 
       { 
        long flag = 0; 
        int id = 1; 
        SQLiteDatabase db = dbHelper.getWritableDatabase(); 
        Cursor cursor = db.query("user_name", new String[]{"count(*) ID"}, null, null, null, null, null); 
        while(cursor.moveToNext()) 
        { 
         int idFromDatabase = cursor.getInt(cursor.getColumnIndex("ID")); 
         if(idFromDatabase != 0) 
         { 
          id = 1 + idFromDatabase; 
         } 
        } 
        ContentValues values = new ContentValues(); 
        values.put("ID", id); 
        values.put("name", nameEditText.getText().toString().trim()); 

        flag = db.insert("user_name", null, values); 
        if(flag != -1) 
        { 
         Toast toast = Toast.makeText(Entername.this, "You have successful inserted this record into database! ", Toast.LENGTH_LONG); 
         toast.show(); 
         db.close(); 

         //clear fields    //clearing edittexts 
          nameEditText.setText(""); 


         return; 
        } 
        else 
        { 
         Toast toast = Toast.makeText(Entername.this, "An error occured when insert this record into database!", Toast.LENGTH_LONG); 
         toast.show(); 
         db.close(); 

         //clear fields 
         //clearing edittexts 
          nameEditText.setText(""); 
         return; 
        } 
       } 
      } 

      public void onClick(DialogInterface dialog, int which) 
      { 
       // TODO Auto-generated method stub 

      } 
     } 


     class DoneButtonListener implements OnClickListener, android.view.View.OnClickListener 
     { 
      public void onClick(View v) 
      { 

       Intent myIntent = new Intent(v.getContext(), Pickholespubs.class); 
       startActivityForResult(myIntent, 0); 

      } 

      public void onClick(DialogInterface dialog, int which) 
      { 
       // TODO Auto-generated method stub 
      } 
     } 
     class showButtonListener implements OnClickListener, android.view.View.OnClickListener 
     { 
      public void onClick(View v) 
      { 
       String display = ""; 
       SQLiteDatabase db = dbHelper.getWritableDatabase(); 
       /** the result will be loaded in cursor **/ 
       Cursor cursor = db.query("user_name", new String[]{"ID","name"}, null, null, null, null, null); 
       /** check if the table is empty **/ 
       if (!cursor.moveToNext()) 
       { 
        addTextView.setText("No data to display, please make sure you have already inserted data!"); 
        db.close(); 
        return; 
       } 
       cursor.moveToPrevious(); 
       /** if the table is not empty, read the result into a string named display **/ 
       while(cursor.moveToNext()) 
       { 
        int ID = cursor.getInt(cursor.getColumnIndex("ID")); 
        String name = cursor.getString(cursor.getColumnIndex("name")); 

        display = display + "\n"+"Player"+ID+", Name: "+name; 
       } 
       /** display the result on the phone **/ 
       addTextView.setText(display); 
       db.close(); 
      } 

      public void onClick(DialogInterface dialog, int which) 
      { 
       // TODO Auto-generated method stub 
      } 
     } 
}` 
+1

執行兩個查詢是否是否定的? – 2012-03-24 11:54:52

+0

什麼不適合db.execSQL(「create table table_name1 ...」); db.execSQL(「create table table_name2 ...」); ? – 2012-03-24 11:58:13

+0

我使用這個:'/ **創建數據庫,如果它不存在**/SQLiteDatabase db = dbHelper.getWritableDatabase();嘗試{db.execSQL(CREATE_TABLE_1); db.execSQL(CREATE_TABLE_2); catch(Exception e){e.printStackTrace(); }}'但是得到它沒有創建第二個表 – user1179083 2012-03-24 12:14:40

回答

1

一個簡單的答案是不,你不能做到這一點。由於Create Table語法不允許同時運行兩個DML操作。

但alternet方法是像下面這樣

Create Table table1 (column list); Create Table table2 (column list); 

這是可能的。道德是每個創建表語法完成後必須有一個;(分號)。

+0

我已經使用過,但它是說我的第二個表沒有被創建 – user1179083 2012-03-24 12:11:44

+0

我使用這個: '/ **創建數據庫,如果它不存在**/ SQLiteDatabase db = dbHelper.getWritableDatabase(); \t \t嘗試db.execSQL(CREATE_TABLE_1); \t \t \t db.execSQL(CREATE_TABLE_2); \t \t \t \t \t} \t \t趕上(例外五) \t \t { \t \t \t e.printStackTrace(); \t \t} }' 但我得到它沒有創建第二個表 – user1179083 2012-03-24 12:12:38

+0

好的,我猜你可能需要在創建第一個表 – Krish 2012-03-24 12:12:41