i'm trying to use a SQLite database, it's a database who is already filled, after some research i found i needed to copy this database to use it, so i picked up the code and tested it but the copy is incomplete, all column aren't copied and the data are just not there. When i open the original file with DB Browser for SQLite there is no problem.
here the code of framework databasehelper:
package com.example.chiffrage;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DataBaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
private static String DB_NAME = "ChiffrageBDD.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 10);
this.myContext = context;
this.DB_PATH = context.getApplicationInfo().dataDir + "/";
Log.e("Path 1", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
// SQLiteDatabase checkDB = null;
// try {
// String myPath = 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;
File databasePath = myContext.getDatabasePath(DB_NAME);
return databasePath.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[2068];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
public List getAllMetal(){
List returnList = new ArrayList();
String queryString = "PRAGMA table_info(Metal)";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString, null);
if (cursor.moveToFirst()){
do {
returnList.add(cursor.getString(1));
} while (cursor.moveToNext());
} else {
//failure
}
cursor.close();
db.close();
return returnList;
}
}
The response of getAllMetal is "[ID]" i should have a table name "Metal" with "id" And "nom" nom is the name of the metal. there is no data and i used PRAGMA to verify the column and as you see it there is missing "nom" column.
here some resources to where i got code:
[Framework SQLiteOpenHelper] https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper#SQLiteOpenHelper(android.content.Context,%20java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int)
[Copy Database] Getting "Failed to open database" error when copying a sqlite database from assets In Android 4.2
ty for your time.