Hi I'm trying to create a ListView that displays the data from a sql file and I always got an error "Error copying database" or i can't open the database
here is my DBHelper
public class DBHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
@SuppressLint("SdCardPath")
private static String DB_PATH = "/data/data/com.mobapp.dblist/databases/";
private static final String DB_NAME = "infodb";
public static final String TBL_GLOSSARY = "main_conference_items";
public static final String KEY_ID = "_id";
public static final String KEY_DAY_ID = "day_id";
public static final String KEY_TITLE = "title";
public static final String KEY_TIME = "time";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(!dbExist)
{this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
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;
}
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[1024];
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.NO_LOCALIZED_COLLATORS | 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) {
}
public Cursor getConferenceItems() {
Cursor localCursor =
this.myDataBase.query(TBL_GLOSSARY, new String[] {
KEY_ID,
KEY_DAY_ID,
KEY_TITLE,
KEY_TIME},
null,
null, null, null, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
and here is my fragment to display my listview
public class TabP extends Fragment {
DBHelper dbHelper;
private SimpleCursorAdapter dataAdapter;
public static final String TAG = "Tab_Programme";
// The desired columns to be bound
String[] columns = new String[] {
DataBaseHelper.KEY_ID,
DataBaseHelper.KEY_DAY_ID,
DataBaseHelper.KEY_TITLE,
DataBaseHelper.KEY_TIME
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.row_id,
R.id.list_day,
R.id.title,
R.id.desc,
};
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_programme,container,false);
initialazeDatabase();
Cursor cursor = dbHelper.getConferenceItems();
dataAdapter = new SimpleCursorAdapter( getActivity(), R.layout.text_list_item_layout, cursor, columns, to, 0);
ListView listView = (ListView) v.findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String countryCode =
cursor.getString(cursor.getColumnIndexOrThrow("title"));
Toast.makeText(getActivity().getApplicationContext(),
countryCode, Toast.LENGTH_SHORT).show();
}
});
return v;
}
public void initialazeDatabase()
{
dbHelper = new DBHelper(getActivity());
try {
dbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
}
Can anyone help me please? I've been trying this since yesterday. Thank you so much.
I found my mistake. The code is working and my mistake was the package name. I did copy paste that's why i get the wrong package. Now it's working.