2012-04-09 13 views
1

私は1つのテーブルを作成しましたが、2つ作成したいのですが、 "show"ボタンを押すと両方のテーブルからコンテンツを選択して表示できますこれは...問題2つのテーブルを作成し、それらを示す私のコード持っています...:sqlite/androidで2つのテーブルを作成してその内容を表示

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 
      } 
     } 
} 
+0

'dbHelper.onCreate'に2番目のテーブルを作成しない理由を説明できますか?そこに複数のテーブルを作成するのを止めるものは何もありません。 – CjS

+0

ああ、私は1ページのテーブルに作成して以来、私はそれを行うことができたか分からなかった。 – user1179083

+0

DBHelperコールバック(onCreate、onUpgradeなど)でDB管理をすべて//行うことができます。 – CjS

答えて

5

@Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_DETAILS + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," 
       + KEY_NUMBER + " TEXT," + KEY_MAIL + " TEXT" + ")"; 
     db.execSQL(CREATE_CONTACTS_TABLE); 
     String CREATE_ONLINE_TABLE = "CREATE TABLE " + TABLE_ONLINE_DETAILS + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," 
       + KEY_MAIL + " TEXT," + KEY_PHONE + " TEXT"+ ")"; 
     db.execSQL(CREATE_ONLINE_TABLE); 

    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_ONLINE_DETAILS); 
     onCreate(db); 
     } 

、次のように試してみてください、あなたは

のようなあなたのJavaクラスのメソッドを呼び出すことができます
db.add(String, String, String); 
関連する問題