SQL android,创建多个表的更多信息

时间:2012-03-24 11:52:50

标签: android sqlite datatable android-listview

这是我的代码 `我创建了一个表,但我想创建两个表,当我点击“show”按钮时,我希望能够从两个表中选择内容并显示它们......这是我的代码...我遇到了问题创建两个表并显示它们:

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 个答案:

答案 0 :(得分:1)

一个简单的答案就是不,你不能这样做。由于Create Table语法不允许同时进行两次DML操作。

但是alternet的方式如下,

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

这是可能的。道德是每个Create Table语法完成后必须有;(分号)。