1

我正在为 Android 开发一个简单的购物应用程序。该应用程序需要两个 SQLite 数据库表,但我没有同时创建它们(最初只为 MainActivity 创建了一个)。在网上搜索后,我了解到我们必须使用 SQLiteOpenHelper 类同时为数据库创建多个表。这种说法是真的吗?

如果不是,请检查下面的代码并为我的错误提出解决方案 -

该应用程序有两个活动,MainActivity.javaItemsActivity.java,一个 DatabaseAdapter( DBAdapter.java ) 类。DatabaseAdapter 类有两个表,MainActivity 类使用名为TABLE_NAME的第一个表,ItemsActivity 类使用 DBAdapter 类中名为TABLE_NAME_ITEMS的第二个表。

ItemsActivity.java 是我将项目插入到 DBAdapter 中名为 TABLE_NAME_ITEMS 的第二个表中的类。这是ItemsActivity.java

public class ItemsActivity extends Activity implements OnClickListener{
    DBAdapter dbAdapterItems;
    String listId;
    //Cursor cItems;
    //MyItemsAdapter adapter;
    int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.items_activity);

        dbAdapterItems = new DBAdapter(this);
        dbAdapterItems.openDatabase();

        Intent itemsActivity = getIntent();
        Bundle b  = itemsActivity.getExtras();
        listId = b.getString("LIST_ID");


        Button addItem = (Button) findViewById(R.id.addItemButton);
    /*  ListView itemslist = (ListView)findViewById(R.id.itemsListView);

        cItems = dbAdapter.getAllItemRecords(listId);
        adapter = new MyItemsAdapter();
        itemslist.setAdapter(adapter);

        **/
        addItem.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        final Dialog d = new Dialog(ItemsActivity.this);
        d.setTitle("Add Item");
        d.setContentView(R.layout.customdialog);
        d.show();

        final EditText itemNameEt = (EditText) d.findViewById(R.id.dialogEditText);
        Button addButton = (Button) d.findViewById(R.id.dialogAddButton);
        Button cancelButton = (Button) d.findViewById(R.id.dialogCancelButton);         

        addButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String itemName = itemNameEt.getText().toString();
                dbAdapterItems.insertItemsRecord(itemName,listId);
                Toast.makeText(ItemsActivity.this, "Item name: "+itemName+" added", Toast.LENGTH_LONG).show();
                d.dismiss();
            }
        });
        cancelButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                d.dismiss();
                Toast.makeText(ItemsActivity.this, listId, Toast.LENGTH_LONG).show();
            }
        });

    }
/** 
    class MyItemsAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return cItems.getCount();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            // TODO Auto-generated method stub
            if(view==null){
                LayoutInflater inflater = getLayoutInflater();
                view = inflater.inflate(R.layout.custom_items_row, parent, false);
            }

            TextView itemsRowTv = (TextView) view.findViewById(R.id.customitemsrowTV);

            cItems.moveToPosition(position);
            String itemName = cItems.getString(1);
            itemsRowTv.setText(itemName);

            return view;
        }

    }
**/
}

这是DBAdapter.java

public class DBAdapter {

    String DATABASE_NAME = "SeenuDB";
    String TABLE_NAME = "listTable"; //First Table Name
    int DATABASE_VERSION = 1;

    String TABLE_NAME_ITEMS = "itemsTable"; //Second Table Name
    String COLUMN_ITEMS_ONE = "rowitemsid";
    String COLUMN_ITEMS_TWO = "itemname";
    String COLUMN_ITEMS_THREE = "listid";

    public static final String COLUMN_ONE = "rowid";
    public static final String COLUMN_TWO = "listname";

    SQLiteDatabase db;
    Context context;
    DBHelper dbHelper;

    String CREATE_TABLE = "create table if not exists listTable(rowid integer primary key autoincrement,listname text not null)";
    String CREATE_TABLE_ITEMS = "create table if not exists " +TABLE_NAME_ITEMS+ "(rowitemsid integer primary key autoincrement,itemname text not null,listid text not null);";

    public DBAdapter(Context c) {
        // TODO Auto-generated constructor stub
        this.context = c;
        dbHelper = new DBHelper(context);
    }

    class DBHelper extends SQLiteOpenHelper{

        public DBHelper(Context context) {
            // TODO Auto-generated constructor stub
            super(context,TABLE_NAME,null,DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_TABLE);
            db.execSQL(CREATE_TABLE_ITEMS);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_ITEMS);
            onCreate(db);
        }

    }

    DBAdapter openDatabase(){
        db = dbHelper.getWritableDatabase();
        return this;
    }

    void closeDatabase(){
        dbHelper.close();
    }


    long insertRecord(String listname){
        ContentValues con = new ContentValues();
        con.put(COLUMN_TWO, listname);
        return db.insert(TABLE_NAME, null, con);
    }


    Cursor getAllRecords(){
        String[] columns = {COLUMN_ONE,COLUMN_TWO};
        return db.query(TABLE_NAME, columns, null, null, null, null, null); 
    }

    void deleteAllRecords(){
        db.delete(TABLE_NAME, null, null);
    }

    void deleteOneRecord(String rowid){
        db.delete(TABLE_NAME, rowid+"="+COLUMN_ONE, null);
    }



    long insertItemsRecord(String itemname,String listid){
        ContentValues con = new ContentValues();
        con.put(COLUMN_ITEMS_TWO, itemname);
        con.put(COLUMN_ITEMS_THREE, listid);
        return db.insert(TABLE_NAME_ITEMS, null, con);
    }

    Cursor getAllItemRecords(String listid){
        String[] columns = {COLUMN_ITEMS_ONE,COLUMN_ITEMS_TWO,COLUMN_ITEMS_THREE};
        return db.query(TABLE_NAME_ITEMS, columns, listid+"="+COLUMN_ITEMS_THREE, null, null, null, null);
    }
}

使用第一个表我能够执行所有 CURD 操作,但是在使用第二个表执行插入操作时,我在 LogCat 中收到错误 no such tableitesTable。

希望读者理解我的问题,请为我提供解决方案..

4

1 回答 1

2

我们必须使用 SQLiteOpenHelper 类同时为数据库创建多个表

您可以随时更改数据库方案。只需增加DATABASE_VERSION触发onUpgrade回调,您就可以在其中执行与架构更改相关的所有事情。

在您的代码中,您可以设置DATABASE_VERSION为 2,它应该可以正常工作。

int DATABASE_VERSION = 2;

但它会重新创建整个数据库,您将丢失所有现有数据。您可以以更合适的方式处理这种情况:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion == 1 && newVersion == 2) {
        db.execSQL(CREATE_TABLE_ITEMS);
    }
}
于 2013-01-12T17:20:29.643 回答