dimanche 15 mars 2015

sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database, I get this while opening the app for First time not later

This is my Dbhelperclass, i call dbHeplper.createDataBase() from mainactivity.


public class DatabaseHelper extends SQLiteOpenHelper {



//public static String DB_PATH = "/data/data/com.peopleapps.myApp/databases/";
public static String DB_PATH;
public static String DB_NAME = "myDB.sqlite";
public static final int DB_VERSION = 1;

public static final String TB_USER = "Locations";

private SQLiteDatabase myDB;
private Context context;

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
Log.e("DB Path",""+DB_PATH);
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
Log.e("DB Path1",""+DB_PATH);
}

}



@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

}

@Override
public synchronized void close(){
if(myDB!=null){
myDB.close();
}
super.close();
}

public Cursor getAllUsers(String sql){
List<String> listUsers = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Log.e("Database",""+db);
Cursor c=null;

try {
c = db.rawQuery(sql , null);
if(c == null) return null;

String name;
c.moveToFirst();
do {
name = c.getString(0);
listUsers.add(name);
} while (c.moveToNext());
//c.close();
} catch (Exception e) {
Log.e("tle99", e.getMessage());
}


//db.close();

return c;
}


/***
* Open database
* @throws SQLException
*/
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.e("MyDB",""+myDB);
}

/***
* Copy database from source code assets to device
* @throws IOException
*/
public void copyDataBase() throws IOException{
try {
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;

OutputStream myOutput = new FileOutputStream(outputFileName);
Log.e("myOutPut",""+myOutput);
byte[] buffer = new byte[1024];
int length;

while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}

}

/***
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();

if (dbExist) {
copyDataBase();
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("tle99 - create", e.getMessage());
}
}
}

// ---------------------------------------------
// PRIVATE METHODS
// ---------------------------------------------

/***
* Check if the database is exist on device or not
* @return
*/
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}


public Cursor fetchLVC(String sql) {
Cursor mCursor = myDB.rawQuery(sql+" " + TB_USER , null);

if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}


}


It throws the following errors E/SQLiteLog(19621): (28) failed to open "/data/data/http://ift.tt/1GGPCkb" with flag (131074) and mode_t (0) due to error (2) E/SQLiteLog(19621): (28) failed to open "/data/data/http://ift.tt/1GGPCkb" with flag (131072) and mode_t (0) due to error (2) E/SQLiteLog(19621): (14) cannot open file at line 32503 of [9491ba7d73] E/SQLiteLog(19621): (14) os_unix.c:32503: (2) open(/data/data/http://ift.tt/1GGPCkb) - E/SQLiteDatabase(19621): Failed to open database '/data/data/http://ift.tt/1GGPCkb'. E/SQLiteDatabase(19621): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database E/SQLiteDatabase(19621): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) E/SQLiteDatabase(19621): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:318) E/SQLiteDatabase(19621): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:228)


Aucun commentaire:

Enregistrer un commentaire