EDIT: I found the cause to be the error of not having a column in my database called Col_Name, however I did code the column into the database and yet it tells me it isnt there.
Error message: Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (table Expense_Table has no column named Name (code 1): , while compiling: INSERT INTO Expense_Table(Am,Name) VALUES (?,?))
I have a database with 2 Columns. I save inputs into the 2 columns then load them into a listview. I have successfully done 1 column and it works perfectly. When I add the 2nd column and I load the app, my Toast message tells me my database is empty. Any help on why this happens?
My MainActivity(Only relevant parts):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView)findViewById(R.id.listView);
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimary));
}
//Code for Ads
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
nameList = new ArrayList<>();
expenseList = new ArrayList<>();
Row = new ArrayList<>();
adapter = new CustomAdapter(this, Row);
listView.setAdapter(adapter);
//Get Database to populate Listview
myDb = new DBHelper(this);
Cursor data = myDb.getListContent();
if(data.getCount() == 0){
Toast.makeText(this, "Data NOT loaded", Toast.LENGTH_SHORT).show();
} else{
while(data.moveToNext()){
String dbAmount = data.getString(1);
String dbName = data.getString(2);
ExRow exRow = new ExRow(dbAmount, dbName);
expenseList.add(dbAmount);
Row.add(exRow);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Data loaded", Toast.LENGTH_SHORT).show();
}
}
And here is my Database class for retrieving and saving data:
private static final String DATABASE_NAME = "Expenses.db";
public static final String TABLE_NAME = "Expense_Table";
public static final String Col_ID = "id";
public static final String Col_AMOUNT = "Am";
public static final String Col_NAME = "Name";
//public static final String Col_Date = "date";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
//Set up database here
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table " + TABLE_NAME +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT, AM TEXT, Name TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public boolean saveData(String Am, String Name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col_AMOUNT, Am);
contentValues.put(Col_NAME, Name);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
public Cursor getListContent(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
There is more to my MainActivity that adds the inputs into the database. I figured it's too simple to paste the code. It's simply saveData(String, String);