android - 在 sqlite/android 中创建两个表并显示它们的内容

标签 android sqlite database-table

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

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

最佳答案

这样试试,

@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);

关于android - 在 sqlite/android 中创建两个表并显示它们的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076590/

相关文章:

php - 如果行存在则不插入

android - 换行符\n 在 textView Android 中未正确显示

android onCheckedChanged 用于 radio 组

android - 从首选项 Activity 启动 Activity 导致权限拒绝异常

android - 无法在 Jenkins 上下载 Android 构建工具

sql - 从两个表中查询SQLite-选择要返回的值

php - 尝试自动将外键值插入到我的 mysql 表中

sql-server - SQL Server - 表元数据

sql - 添加前检查扩展属性描述是否已存在

android - 试图将按钮移动到屏幕的右侧。我的权利不是电脑的权利