更改SQLOpenHelper为了读取从资产 [英] Change the SQLOpenHelper In Order To Read From The Asset
本文介绍了更改SQLOpenHelper为了读取从资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的SQLOpenHelper的code,
This is the code of my 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) {
}
}
和我想改变它的方式让我的数据库将被读取FRM资产并第time.How我可以处理这难道不是创造出来的?
我建议的code是这个,但我知道don'n放在哪里!
and I want to change it in a way so my database is going to be read frm the assets and not created for the first time.How can I handle this? My suggested code is this but I don'n know where to put it!
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;
}
如果你帮我,我会很高兴!
I'll be glad if you help me!
推荐答案
这是一个工作code选择所需要的类:
This is a working code for the needed class:
public class ContactsDB extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/com.example.test/databases/";
public static String DB_NAME = "database";
public static SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseManager(Context context)throws IOException{
super(context,DB_NAME,null,DATABASE_VERSION);
this.mContext=context;
boolean dbexist=checkDataBase();
if(dbexist){
openDataBase();
}else{
System.out.println("Database does not exist");
createDataBase();
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
SQLiteDatabase db_read= null;
if(dbExist){
// do nothing - database already exist
}else{
db_read=this.getReadableDatabase();
db_read.close();
try {
copyDataBase();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}
public boolean checkDataBase() {
boolean checkdb=false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile=new File(myPath);
checkdb=dbfile.exists();
}catch (SQLiteException e){
throw new Error("Database does not exist");
}
return checkdb;
}
public void copyDataBase() throws IOException {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while((length=myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}
创建ContactsDB类的一个实例来得到这个工作。 ;)
Create an instance of ContactsDB class to get this working. ;)
这篇关于更改SQLOpenHelper为了读取从资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文