0

我似乎无法将多个表插入一个数据库。另外,我在 Android 中遵循了这个示例多表 SQLite DB 适配器?.

我不确定要在 Cars.java、Cycles.java 和 Boats.java 类中编码什么。我也不确定在这 3 个类中声明哪个 DBAdapter。我已经设法为 3 个表创建数据库。但是没有创建表。

这是 CarsDBAdapter 类,类似于 BoatsDBAdapter 和 CyclesDBAdapter 类。

public class CarsDBAdapter
{
    public static final String ROW_ID = "_id";
    public static final String NAME = "name";
    public static final String MODEL = "model";
    public static final String YEAR = "year";

    private static final String DATABASE_TABLE = "cars";

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);          
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {           
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {           
        }
    }// end DatabaseHelper class

    public CarsDBAdapter(Context ctx)
    {
        this.mCtx = ctx;
    }

    public CarsDBAdapter open() throws SQLException
    {
        this.mDbHelper = new DatabaseHelper(this.mCtx);
        this.mDb = this.mDbHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        this.mDbHelper.close();
    }

    /*public long insertCars(String name, String model, String year)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NAME, name);
        initialValues.put(MODEL, model);
        initialValues.put(YEAR, year);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }*/

    public long create(String name, String model, String year)
    {
        ContentValues initialValues = new ContentValues();

        initialValues.put(NAME, name);
        initialValues.put(MODEL, model);
        initialValues.put(YEAR, year);
        return this.mDb.insert(DATABASE_TABLE, null, initialValues); 
    }

    public boolean deleteCar(long rowId) 
    {          
        return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$     
    }

     public Cursor getAllCars() 
     {          
         return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID, NAME, MODEL, YEAR }, null, null, null, null, null);       
     }

     public Cursor getCar(long rowId) throws SQLException
     {
         Cursor mCursor = this.mDb.query(true, DATABASE_TABLE, new String[] {ROW_ID, NAME, MODEL, YEAR}, ROW_ID + "=" + rowId,
                 null, null, null, null, null);

         if(mCursor != null)
         {
             mCursor.moveToFirst();
         }

         return mCursor;
     }

     public boolean updateCar(long rowId, String name, String model, String year)
     {
         ContentValues args = new ContentValues();

         args.put(NAME, name);
         args.put(MODEL, model);
         args.put(YEAR, year);
         return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0;  
     }

}//end CarsDBAdapter class

这是我创建 3 个表的 DBAdapter:

public class DBAdapter
{
    public static final String DATABASE_NAME = "stuffIOwn";
    public static final int DATABASE_VERSION = 2;

    private static final String CREATE_TABLE_CARS = "create table cars(_id integer primary key autoincrement, " //$NON-NLS-1$
            +CarsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$
            +CarsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$
            +CarsDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$

    private static final String CREATE_TABLE_BOATS = "create table boats(_id integer primary key autoincrement, " //$NON-NLS-1$
            +BoatsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$
            +BoatsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$
            +BoatsDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$

    private static final String CREATE_TABLE_CYCLES = "create table cycles(_id integer primary key autoincrement, " //$NON-NLS-1$
            +CyclesDBAdapter.NAME+ " TEXT," //$NON-NLS-1$
            +CyclesDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$
            +CyclesDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$

    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)     
    {         
        this.context = ctx;         
        this.DBHelper = new DatabaseHelper(this.context);     
    }

    private static class DatabaseHelper extends SQLiteOpenHelper      
    {         
        DatabaseHelper(Context context)          
        {             
            super(context, DATABASE_NAME, null, DATABASE_VERSION);         
            }          
        @Override         
        public void onCreate(SQLiteDatabase db)          
        {             
            db.execSQL(CREATE_TABLE_CARS);             
            db.execSQL(CREATE_TABLE_BOATS);             
            db.execSQL(CREATE_TABLE_CYCLES);                   
        }          
        @Override         
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)          
        {                            
            // Adding any table mods to this guy here         
        }     
    }//end DatabaseHelper class

    public DBAdapter open() throws SQLException      
    {         
    this.db = this.DBHelper.getWritableDatabase();         
    return this;     
    }      
    /**      
     * close the db 
     *return type: void      
     */    
    public void close()      
    {        
        this.DBHelper.close();     
    }

这是 Cars.java 类,它类似于 Cycles.java 和 Boats.java 类。

public class Cars extends Activity
{
    final Context context = this;

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cars);

        CarsDBAdapter carsDB = new CarsDBAdapter(this);

        //DBAdapter dbA = new DBAdapter(this);

        /*carsDB.open();
        long id;
        id = carsDB.insertCars("Mercedes", "MERCDS", "2000");
        id = carsDB.insertCars("BMW", "BMWTO", "1999");
        carsDB.close();*/

        carsDB.open();

        Cursor c = carsDB.getAllCars();
        if (c.moveToFirst())
        {
            do
            {
                DisplayContact(c);
            } while (c.moveToNext());
        }

        carsDB.close();

        Button btnMenu = (Button) findViewById(R.id.btnMenu);
        btnMenu.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Intent menuIntent = new Intent(context, VehiclesMenu.class);
                    startActivity(menuIntent);

                }
            });
    }//end onCreate()

    public void DisplayContact(Cursor c)
    {
        Toast.makeText(
                this,
                "id: " + c.getString(0) + "\n" + "Name: " + c.getString(1)
                        + "\n" + "Model:  " + c.getString(2) + "\n" + "Year: " + c.getString(3),  Toast.LENGTH_LONG)
                .show();
    }

我真的需要帮助。任何帮助将不胜感激。

更新

我已经添加了代码,但您可以查看上面的链接。它链接到我遵循的示例。

4

2 回答 2

0

如果您想在一个 dbHelper 中创建多个表,请在 onCreate 方法中执行此操作。

   @Override
    public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE Abc(_id INTEGER ,Category text ,Category_Name,Date text);");

        db.execSQL("CREATE TABLE Xyz(_id INTEGER ,Name text not null);");
    }

您可以编写任意数量的 db.execSql() 来创建表。

于 2012-08-10T04:25:02.317 回答
0

尝试这个:

package com.example.sample;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static String sqlTable1 = "CREATE TABLE TEST (ID INTEGER PRIMARY KEY,NAME TEXT);";
    private static String sqlTable2 = "CREATE TABLE TEST2 (ID INTEGER PRIMARY KEY,NAME TEXT);";
    private Context context;

    public DatabaseHelper(Context context) {
        super(context, "MYDB", null, 1);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
        db.execSQL(sqlTable1);
        db.execSQL(sqlTable2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void initialInsert() {
        String sql = "INSERT INTO TEST(NAME) VALUES('YOUR NAME')";
        String sql2 = "INSERT INTO TEST2(NAME) VALUES('YOUR NAME')";
        getWritableDatabase().execSQL(sql);
        getWritableDatabase().execSQL(sql2);
    }
}

从您的活动中创建助手类的对象,例如:

DatabaseHelper helper = new DatabaseHelper(this);
//FOR INSERTING INITIAL VALUES      
helper.initialInsert();
于 2012-08-10T05:24:24.443 回答