I am using cupboard to easily write and read from a SQL Lite DB, that I populate from a CSV file the first time the app is loaded. This works great on my android studio emulator, but I don't see it creating the file (KMDB.db) on my phone, or reading the data.
It's possible the issue is reading from the CSV so it never acutally populates the DB. I am just using Android studio to create an APK and placing it in a directory on my phone, so this is another potential issue.
Here is my base DB code
package com.example.shweber.lanternkeeper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static nl.qbusict.cupboard.CupboardFactory.cupboard;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by SHWEBER on 1/16/2016.
*/
public class EZDB extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "KMDB.db";
private static final int DATABASE_VERSION = 1;
public EZDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
static {
// register our models
//cupboard().register(Survivor.class);
cupboard().register(Disorders.class);
cupboard().register(Farts.class);
cupboard().register(Abilandimpair.class);
}
@Override
public void onCreate(SQLiteDatabase db) {
// this will ensure that all tables are created
cupboard().withDatabase(db).createTables();
// add indexes and other database tweaks in this method if you want
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// this will upgrade tables, adding columns and new tables.
// Note that existing columns will not be converted
cupboard().withDatabase(db).upgradeTables();
// do migration work if you have an alteration to make to your schema here
}
Here is my code where I create the db and read from the CSV and then put the info into the DB
//Database setup for all tables
EZDB ezdb = new EZDB(this);
db = ezdb.getWritableDatabase();
//read values from DB into classes
getFartsfromDB();
getAbilandimpairfromDB();
getDisordersfromDB();
arrayNameFarts.addAll(getAllFartsNames(arrayFarts));
arrayNameDisorders.addAll(getAllDisordersNames(arrayDisorders));
arrayNameAbilandimpair.addAll(getAllAbilandimpairNames(arrayAbilandimpair));
//Reading items from CSVs and adding to the DB, will only do it it DB has not been updated this way yet
//*********************************************
if(Objects.equals(cupboard().getTable(Farts.class), "Farts")) {
System.out.println("DB already created");
}else{
arrayFarts.addAll(readcsv.retrieveFarts(this)); //pulls items from CSV not needed after initial creation
cupboard().withDatabase(db).put(readcsv.retrieveFarts(this));//writes item to the DB not needed after initial creation
//*************************************
//Reading and adding the Disorders items to the DB
arrayDisorders.addAll(readcsv.retrieveDisorders(this));//pulls items from CSV not needed after initial creation
cupboard().withDatabase(db).put(readcsv.retrieveDisorders(this));//writes item to the DB not needed after initial creation
//*************************************
//Reading and adding the Abilandimpair items to the DB
arrayAbilandimpair.addAll(readcsv.retrieveAbilandimpair(this));//pulls items from CSV not needed after initial creation
cupboard().withDatabase(db).put(readcsv.retrieveAbilandimpair(this));//writes item to the DB not needed after initial creation
System.out.println("DB now created");
}
My code where I read the CSV
public class ReadCSV {
//method to read a CSV file in the assets directory called farts.csv
public ArrayList<Farts> retrieveFarts(Context context) {
AssetManager assetManager =context.getAssets();
ArrayList<Farts> arrayfarts = new ArrayList<Farts>();
InputStream csvStream = null;
try {
csvStream = assetManager.open("farts.csv");
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvreader = new CSVReader(csvStreamReader);
String[] line;
int temp=0;
while ((line=csvreader.readNext())!=null){
Farts fdata = new Farts();
fdata.name=line[0];
fdata.description=line[1];
arrayfarts.add(fdata);
temp++;
}
} catch (IOException e) {e.printStackTrace(); }
return arrayfarts;
}
//method to read a CSV file in the assets directory called disorders.csv
public ArrayList<Disorders> retrieveDisorders(Context context) {
AssetManager assetManager =context.getAssets();
ArrayList<Disorders> arraydisorders = new ArrayList<Disorders>();
InputStream csvStream = null;
try {
csvStream = assetManager.open("disorders.csv");
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvreader = new CSVReader(csvStreamReader);
String[] line;
int temp=0;
while ((line=csvreader.readNext())!=null){
Disorders ddata = new Disorders();
ddata.name=line[0];
ddata.description=line[1];
arraydisorders.add(ddata);
temp++;
}
} catch (IOException e) {e.printStackTrace(); }
return arraydisorders;
}
//method to read a CSV file in the assets directory called abilandimpair.csv
public ArrayList<Abilandimpair> retrieveAbilandimpair(Context context) {
AssetManager assetManager =context.getAssets();
ArrayList<Abilandimpair> arrayabilandimpair = new ArrayList<Abilandimpair>();
InputStream csvStream = null;
try {
csvStream = assetManager.open("abilandimpair.csv");
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvreader = new CSVReader(csvStreamReader);
String[] line;
int temp=0;
while ((line=csvreader.readNext())!=null){
Abilandimpair adata = new Abilandimpair();
adata.name=line[0];
adata.description=line[1];
arrayabilandimpair.add(adata);
temp++;
}
} catch (IOException e) {e.printStackTrace(); }
return arrayabilandimpair;
}
I suppose I am not sure where I need to place my CSVs on the phone so they are read properly. I would prefer to just package a copy of the DB with the application in the APK.
Aucun commentaire:
Enregistrer un commentaire