0

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);

Corpse
  • 107
  • 1
  • 11

2 Answers2

0

I found the answer. It was because I added the column later after already creating the database. I change the name of the database and it worked.

Corpse
  • 107
  • 1
  • 11
-1

I think you should have to use do while loop to iterate data from your database.

if (cursor.moveToFirst()) {
        do {
           String dbAmount = cursor.getString(1);
           String dbName = cursor.getString(2);
            // Adding your data to holder
            ExRow exRow = new ExRow(dbAmount, dbName);
        expenseList.add(dbAmount);
        Row.add(exRow);
        } while (cursor.moveToNext());
    }

also there is no need to notify your list all the time so remove that code.

happy coding. :-) i hope it works for you.

  • Thanks, but it didnt solve the problem :/ If i set dbName = "randomString" and comment out the contentvalue.put(Col_Name, Name), then it works and loads the database and just puts "randomString" into the textview. But when I try to use the inputs, it doesnt work. – Corpse Apr 02 '17 at 22:00
  • What you have mistaken is you are setting up your adapter before adding any value in the array list. so you just try to populate your arraylist first then try to adding it to your adapter. – Developer Maniax Apr 02 '17 at 22:14
  • I found cause of the problem, but no solution. See edited question if you still wanna help. – Corpse Apr 02 '17 at 23:10
  • @DeveloperManiax The original while loop actually is better. – CL. Apr 03 '17 at 07:15