0

this is my code `I have created one table, but i want to create two and when i hit the "show" button, i want to be able to select contents from both tables and show them...this is my code...am having problems creating two tables and showing them:

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  
            }
        }
}`
3
  • 1
    Executing two queries is a no-no ? Commented Mar 24, 2012 at 11:54
  • What does not fit db.execSQL("create table table_name1..."); db.execSQL("create table table_name2..."); ? Commented Mar 24, 2012 at 11:58
  • i use this: /** create the database if it dosen't exist **/ SQLiteDatabase db = dbHelper.getWritableDatabase(); try{ db.execSQL(CREATE_TABLE_1); db.execSQL(CREATE_TABLE_2); } catch(Exception e) { e.printStackTrace(); } } but am getting that it's not creating the second table Commented Mar 24, 2012 at 12:14

1 Answer 1

1

A Simple Answer would be No you can not do it. As Create Table Syntax doesn't allow two DML operations at a same time.

But the alternet way is like as follows,

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

This could be possible. Moral is there must be a ; (semicolon) after each Create Table syntax is completed).

Sign up to request clarification or add additional context in comments.

6 Comments

i have used that but it's saying that my second table is not being created
i use this: /** create the database if it dosen't exist **/ SQLiteDatabase db = dbHelper.getWritableDatabase(); try{ db.execSQL(CREATE_TABLE_1); db.execSQL(CREATE_TABLE_2); } catch(Exception e) { e.printStackTrace(); } } but am getting that it's not creating the second table
ok i guess you might need to commit after creating first table
means after the execute of first create statement, write commit, to save the first table's creation and then create another one.
ok..so i'ld just write after the db.execSQL(Create_Table_1); COMMIT; db.execSQL(Create_Table_2);?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.