My code is as Follows:
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
@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;
}}
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.
Modify your
createDataBase()
method in yourDatabaseHandler
like the code below:createDataBase() :
checkDataBase() :
copyDataBase() :
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_FOLDERI hope this helps.
P.S: DB_NAME and DB_PATH are both Strings.
UPDATE :
Whenever you are trying to use an instance of your
DatabaseHelper
Class, just call this methodcreateDatabase()
like this :It will copy the existing Database from Asset Folder if the Database has not been copied to the Databases directory.
I hope this helps.