如何从资源文件夹数据库复制到数据库文件夹 [英] How to copy Database from Asset Folder to Databases Folder
问题描述
我的code如下:
My code is as Follows:
dbhelper.java
dbhelper.java
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion) return;
db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME +";");
onCreate(db);
}
@Override
public void onCreate(SQLiteDatabase db) {
//here is the database definition
db.execSQL("CREATE TABLE dhivehienglish " +
"(mv TEXT, en TEXT);");
//insert pre-configured records
db.execSQL("INSERT INTO dhivehienglish (mv, en) VALUES('ކާރު','car');");
db.execSQL("INSERT INTO dhivehienglish (mv, en) VALUES('ޖަހާ','hit');");
db.execSQL("INSERT INTO dhivehienglish (mv, en) VALUES('އިނުން','sit');");
db.execSQL("INSERT INTO dhivehienglish (mv, en) VALUES('ކެއުން','eat');");
}
}
MainActivity.java
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carian_kamus);
//listing data processes
//-initiate the database connector
db = dbhelper.getReadableDatabase();
txtmelayu=(EditText)findViewById(R.id.txtmelayu);
btncari=(Button)findViewById(R.id.btncari);
btncari.setOnClickListener(this);
lblmakna=(TextView)findViewById(R.id.lblmakna);
}//end onCreate
public void onClick(View v){
//fetch kata melayu dr textbox
String carimelayu=txtmelayu.getText().toString();
//kena letak dalam onclick
//-run SELECT command to fetch data from table
if (v.getId()==R.id.btncari){
Cursor cmelayu=db.rawQuery("SELECT * FROM dhivehienglish " +
"WHERE mv='"+carimelayu+"';", null);
//-fetch record
if(cmelayu.getCount()!=0){
cmelayu.moveToFirst();//go to first row
String en=cmelayu.getString(1).toString();
lblmakna.setText(en);
}
else{
//display some notice here saying no data found
lblmakna.setText("Not found!");}}
}//end onCLick
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_carian_kamus, menu);
return true;
}
//when menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//call about screen, if user hit "Tentang kami" menu
if (item.getItemId()==R.id.minsert){
Intent ins= new Intent (this, InsertActivity.class);
startActivity(ins);
}
return true;
}}
我怎样才能将现有数据库从我的资产文件夹,并把它作为位于我的应用程序的沙箱中的本地数据库?任何帮助是非常AP preciated。
How can i move my existing Database from my Assets Folder and use it as a native database located within my Application's Sandbox ? Any Help is highly appreciated.
推荐答案
修改你的的CreateDatabase()
方法,在你的数据库处理器
像下面的code:
Modify your createDataBase()
method in your DatabaseHandler
like the code below:
的CreateDatabase():
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
System.out.println("createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
checkDataBase():
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
copyDataBase():
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
其中, DB_PATH
= /数据/数据/ YOUR_PACKAGE_NAME /数据库/
,您可以在DatabseHandler的构建初始化像这样: DB_PATH = context.getApplicationInfo()DATADIR +/数据库/;
DB_NAME
= YOUR_DATABASE_NAME_THAT_IS_STORED_IN_YOUR_ASSETS_FOLDER
Where DB_PATH
= /data/data/YOUR_PACKAGE_NAME/Databases/
, You can initialize it in your DatabseHandler's Construct like this : DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
DB_NAME
= YOUR_DATABASE_NAME_THAT_IS_STORED_IN_YOUR_ASSETS_FOLDER
我希望这有助于。
P.S:DB_NAME和DB_PATH都是字符串
P.S: DB_NAME and DB_PATH are both Strings.
更新:
当你要使用你的 DatabaseHelper
类的一个实例,只需要调用这个方法的CreateDatabase()
是这样的:
UPDATE :
Whenever you are trying to use an instance of your DatabaseHelper
Class, just call this method createDatabase()
like this :
DatabaseHandler db = new DatabaseHandler(context);
try
{
db.createDataBase();
}
catch (IOException io)
{
throw new Error("Unable to create database");
}
这将复制从资源文件夹现有的数据库,如果数据库还没有被复制到数据库目录。
我希望这有助于。
It will copy the existing Database from Asset Folder if the Database has not been copied to the Databases directory.
I hope this helps.
这篇关于如何从资源文件夹数据库复制到数据库文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!