これは私の SQLOpenHelper のコードです。
public class ContactsDB extends SQLiteOpenHelper{
/** Database name */
private static String DBNAME = "sqlspinnersmsdemo";
/** Version number of the database */
private static int VERSION = 1;
/** Field 1 of the table contacts, which is the primary key */
public static final String KEY_ROW_ID = "_id";
/** Field 2 of the table contacts, stores the contact name */
public static final String KEY_NAME = "name";
/** Field 3 of the table contacts, stores the phone number of the contact */
public static final String KEY_PHONE = "phone";
/** A constant, stores the the table name */
private static final String DATABASE_TABLE = "contacts";
/** An instance variable for SQLiteDatabase */
private SQLiteDatabase mDB;
/** Constructor */
public ContactsDB(Context context) {
super(context, DBNAME, null, VERSION);
this.mDB = getWritableDatabase();
}
/** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called
* provided the database does not exists
* */
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table contacts (_id integer primary key autoincrement , "
+ " name text not null , phone text not null ) " ;
db.execSQL(sql);
}
/** Inserts a new contact to the table contacts */
public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;
}
/** Updates a contact */
public int update(ContentValues contentValues,String contactID){
int cnt = mDB.update(DATABASE_TABLE, contentValues, "_id=" + contactID, null);
return cnt;
}
/** Deletes a contact from the table */
public int del(String contactID){
int cnt = mDB.delete(DATABASE_TABLE, "_id="+contactID, null);
return cnt;
}
/** Returns all the contacts in the table */
public Cursor getAllContacts(){
return mDB.query(DATABASE_TABLE, new String[] { KEY_ROW_ID, KEY_NAME , KEY_PHONE } , null, null, null, null, KEY_NAME + " asc ");
}
/** Returns a contact by passing its id */
public Cursor getContactByID(String contactID){
return mDB.query(DATABASE_TABLE, new String[] { KEY_ROW_ID, KEY_NAME , KEY_PHONE } , "_ID="+contactID, null, null, null, KEY_NAME + " asc ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
そして、データベースがアセットから読み取られ、初めて作成されないように変更したいのですが、どうすればこれを処理できますか? 私の提案するコードはこれですが、どこに置くべきかわかりません!
public ContactDB(Context context, String databaseName) {
super(context, databaseName, null, 1);
this.context = context;
String packageName = context.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
DB_NAME = databaseName;
openDataBase();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return database;
}
あなたが私を助けてくれるなら、私はうれしいです!