Error Copying Database
I have no problem copying database using helper. but since a re format my pc and install the eclipse this day I just can't copy the datbase anymore. maybe the sd card of emulator c
Solution 1:
Focus on below line, don't forget to put .sqlite
extension:
privatestaticStringDB_NAME="wfbos.sqlite";
It worked for me.
Solution 2:
He you can use my DbHelper..It is copied database from asset folder...
package com.Blog;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
publicclassDBAdapterextendsSQLiteOpenHelper
{
privatestaticStringDB_NAME="wfbos.sqlite";
privateStringDB_PATH="";
privatestatic DBAdapter adb;
private SQLiteDatabase db;
private Context myContext;
/*public DBAdapter(Context context, String name, CursorFactory factory,int version)
{
super(context, DB_NAME, factory, 1);
this.myContext = context;
DB_PATH = "/data/data/"+myContext.getPackageName()+"/databases/";
}*/publicDBAdapter(Context context)
{
super(context, DB_NAME, null,1);
this.myContext = context;
DB_PATH = "/data/data/"+myContext.getPackageName()+"/databases/";
}
publicstaticsynchronized DBAdapter getAdapterInstance(Context context)
{
if(adb == null)
{
adb = newDBAdapter(context);
}
return adb;
}
publicvoidcreatedatabase()
{
booleandbExist= checkDataBase();
if(dbExist)
{
}
else
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (IOException e)
{
Log.v("log",e.toString());
thrownewError("Error copying database");
}
}
}
privatevoidcopyDataBase()throws IOException
{
// Open your local db as the input streamInputStreammyInput= myContext.getAssets().open(DB_NAME);
// Path to the just created empty dbStringoutFileName= DB_PATH + DB_NAME;
// Open the empty db as the output streamOutputStreammyOutput=newFileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfilebyte[] buffer = newbyte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
Log.v("log", "copy finish");
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
privatebooleancheckDataBase()
{
SQLiteDatabasecheckDB=null;
try
{
StringmyPath= DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e)
{
}
if (checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
public SQLiteDatabase openDataBase()throws SQLException
{
// Open the databaseStringmyPath= DB_PATH + DB_NAME;
db= SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
return db;
}
@OverridepublicvoidonCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
@Overridepublicsynchronizedvoidclose() {
if(db != null)
db.close();
super.close();
}
}
Solution 3:
You have somehow messed up the onCreate
method in your DBHelper
. This didn't happen because you always had the database created before. now that you have to do a fresh install on a new device (your case new emulator) if fails because your Create Table queries are probably wrong and the tables aren't created properly.
So check those, i am pretty sure there lies your problem.
Edit:
Also i think you are missing a whitespace between *
and first
like:
SELECT*fromfirst
Post a Comment for "Error Copying Database"