我想在我现有的Sqlite数据库中为我的Android应用程序添加另一个表。我创建了一个表“dailytips”,并希望添加另一个表“healthytips”。我尝试了不同的方法,但没有成功,也做了谷歌搜索,但每个代码都与我的不同。请有人帮助我。
这是我的代码:
package com.example.health;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "healthDB";
public static final String TABLE_DAILY = "dailytips";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TIPS = "tips";
public static final String TABLE_HEALTH="healthytips";
public static final String COLUMN_H_ID = "_id";
public static final String COLUMN_H_TIPS = "tips";
//create table TABLE_DAILY (ID integer primary key, tips text not null);
private static final String CREATE_TABLE_DAILYS =
"create table " + TABLE_DAILY + " ("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TIPS + " text not null);";
//create table TABLE_HEALTHY (ID integer primary key, tips text not null);
private static final String CREATE_TABLE_HEALTH=
"create table " + TABLE_HEALTH + " ("
+ COLUMN_H_ID + " integer primary key autoincrement, "
+ COLUMN_H_TIPS + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String tips){
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_TIPS, tips);
return sqLiteDatabase.insert(TABLE_DAILY, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(TABLE_DAILY, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{COLUMN_ID, COLUMN_TIPS};
Cursor cursor = sqLiteDatabase.query(TABLE_DAILY, columns,
null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_DAILYS);
db.execSQL(CREATE_TABLE_HEALTH);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}}
答案 0 :(得分:0)
在onCreate()方法中,
db.execSQL(CREATE_DATABASE_TABLE_1);
db.execSQL(CREATE_DATABASE_TABLE_2);
在您的程序中更新此内容。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE_1");
db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE_2");
// Create tables again
onCreate(db);
}
注意:在运行应用程序之前需要清除数据库。
答案 1 :(得分:0)
我的解决方案是创建一个从SqliteDatabaseHelper派生的数据库帮助器,并在onCreate方法中将所有SQL语句添加到其中,包括new和old以及onUpgrade方法,因此下次是否创建新数据库(可能在首先运行)或升级您的数据库,您将得到所有新的旧表。仅使用此类创建或升级db,其他工作将在其他更具体的类中执行。
DatabaseHelper - 添加代码以创建/升级所有表 Table1Helper - 管理table1的方法 Table2Helper - 管理table2等的方法
希望这有助于某人。