How To Insert Data Into The Database File Which Is In The Assets Folder
Solution 1:
You should not create database in the assets folder as we can only read data from assets folder.
Infact you should create the database in the default internal folder i.e data/data/[package-name]/databases OR you also can create your database in the sdcard and can perform read-write operation but of course it will not run if there is no sdcard.
The following is the code for creating databse in sdcard:-
private SQLiteDatabase db;
privatestatic Context cntxt;
publicstaticFilefilename=null;
publicstaticStringDATABASE_FILE_PATH_EXTERNAL=null;
publicDBHelper(Context context) {
super(context, DATABASE_NAME, null,1);
cntxt = context;
try{
try{
filename = Environment.getExternalStorageDirectory();
}catch(Exception e){
Toast.makeText(DbHelper.cntxt, "Please Insert SD card To create Database", Toast.LENGTH_LONG).show();
Log.e("Log",e.getMessage(),e.fillInStackTrace());
}
DATABASE_FILE_PATH_EXTERNAL = filename.getAbsolutePath()+File.separator+DATABASE_NAME;
// db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH_EXTERNAL, null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
}catch(Exception e){
Log.e("Log",e.getMessage(),e.fillInStackTrace());
}
}
@Overridepublicsynchronized SQLiteDatabase getWritableDatabase() {
// TODO Auto-generated method stubtry{
db = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH_EXTERNAL, null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
try{
onCreate(db);
}catch(Exception e){
Log.e("Log",e.getMessage(),e.fillInStackTrace());
}
return db;
}catch(Exception e){
Log.e("Log",e.getMessage(),e.fillInStackTrace());
if(db!=null)
db.close();
}
return db;
}// End of getWritableDatabase()
**Inserting and Retrieving data from database:-**
publicvoidinsertIntoTable(String[] userName,int[] score)
{
SQLiteDatabasedb=this.getWritableDatabase();
ContentValuescv=newContentValues();
try {
db.beginTransaction();
for (inti=0; i < beatID.length; i++) {
cv.put("UserName",userName[i]);
cv.put("Score",score[i]);
db.insert("TableName", "UserName",
cv);
}
db.setTransactionSuccessful();
} catch (Exception ex) {
} finally {
db.endTransaction();
db.close();
}
}
publicvoidgetUserNamePswd(){
Cursorc=null;
SQLiteDatabasedb=null;
try{
db = this.getReadableDatabase();
c = db.rawQuery("Select UserName,Score from TableName",null);
c.moveToFirst();
String[] username = newString[c.getCount()];
int[] score = newint[c.getCount()];
intcounter=0;
c.moveToFirst();
while(!c.isAfterLast()){
username[counter] = c.getString(0);
score[counter] = c.getInt(1);
c.moveToNext();
counter++;
}
}catch(Exception e){
Log.e("Log", e.getMessage(), e.fillInStackTrace());
returnnull;
}finally{
c.close();
db.close();
}
}
Solution 2:
As the /asset
directory is read only (because android .apk file is read-only) so you can't write anything in asset folder file, you have to first copy that sqlite database file into your application's internal storage /data/data/<package_name>/databases
directory then you can modify your database file..
Copy your pre-populated sqlite database file into
data/data/<package_name>/databases directory
,Open that database file (from /databases/ directory) in your sqlite database helper class..
Perform insert, update or select operation on that database..
Solution 3:
You need to copy database file from asset and paste in /data/data/''/databases/ folder
privatestaticStringDB_NAME="QuotesData.db";
privatestaticStringDB_PATH="/data/data/[PACKAGE NAME]/databases/";
privatevoidcopyDataBase()throws IOException {
InputStreammyInput= context.getAssets().open(DB_NAME);
StringoutFileName= DB_PATH + DB_NAME;
OutputStreammyOutput=newFileOutputStream(outFileName);
byte[] buffer = newbyte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
In Above code : Replace [PACKAGE NAME ] with your package name.
Post a Comment for "How To Insert Data Into The Database File Which Is In The Assets Folder"