我似乎无法将多个表插入一个数据库。另外,我已经按照这个例子Multiple Table SQLite DB Adapter(s) in Android?。
我不确定要在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();
}
我真的需要帮助。任何帮助将不胜感激。
更新
我已在代码中添加,但您可以查看上面的链接。它链接到我遵循的例子。
答案 0 :(得分: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()来创建表。
答案 1 :(得分: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();