lundi 9 mars 2015

error while opening sqlite database from myown path in android

I have created a sampleDb.sqlite and copied that db in the asset folder. When I try to open it, it gives me the following error in logcat:



03-09 07:20:50.991: E/SQLiteLog(11307): (14) cannot open file at line 30191 of [00bb9c9ce4]
03-09 07:20:50.991: E/SQLiteLog(11307): (14) os_unix.c:30191: (2) open(//data/data/com.example.dietanimation/databases/sampleDb.sqlite) -
03-09 07:20:51.181: E/SQLiteDatabase(11307): Failed to open database 'data/data/com.example.dietanimation/databases/sampleDb.sqlite'.
03-09 07:20:51.181: E/SQLiteDatabase(11307): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at com.example.dietanimation.DBhelper.isDatabaseAvailable(DBhelper.java:71)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at com.example.dietanimation.DBhelper.<init>(DBhelper.java:34)
03-09 07:20:51.181: E/SQLiteDatabase(11307): at com.example.dietanimation.MainActivity.onCreate(MainActivity.java:67)


and following is my code



public class DBhelper extends SQLiteOpenHelper
{
static String DBName="sampleDb.sqlite";
String Table_Name="diet_tbl";
public final static int version=1;
public static String DB_PATH=null;
public SQLiteDatabase myDatabase=null;
private Context myContext=null;
SQLiteDatabase dbHelper;

public DBhelper(Context context) {
super(context, DBName, null, version);
// TODO Auto-generated constructor stub
DB_PATH="data"+File.separator+"data"+File.separator+context.getPackageName()+File.separator+"databases"+File.separator;
this.myContext=context;
Log.d("Debug"," Point 2");
boolean checkDb= isDatabaseAvailable();

if(checkDb){

}else{
this.getReadableDatabase();
try{
Log.d("Debug"," Point 5");
copyTheDatabase();
}catch(IOException e){
Log.d("Debug"," Point 6");
throw new Error("Error Coping database");
}
}
}
private void copyTheDatabase() throws IOException {
// TODO Auto-generated method stub
InputStream myInput=myContext.getAssets().open("sampleDb.sqlite");
String outFileName=DB_PATH+DBName;
Log.d("Debug"," Point 7 with File");
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();
}
private boolean isDatabaseAvailable() {
// TODO Auto-generated method stub

SQLiteDatabase checkDB=null;

try{
String myPath=DB_PATH+DBName;
checkDB=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){
Log.d("Debug"," Point 3");
}
if(checkDB!=null){
checkDB.close();
}
Log.d("Debug"," Point 4");
return checkDB!=null ?true:false;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
please help me asap.

Aucun commentaire:

Enregistrer un commentaire