0

I have created this class for the names of database tables :

public class TableDatas {
    public TableDatas() {
    }
    public static abstract class TableInfo implements BaseColumns//columns of tables
    {
        public static final String first_Name = "first_name" ;
        public static final String last_Name = "last_name" ;
        public static final String sex  = "sex" ;
        public static final String birth_date = "birth_date" ;
        public static final String db_name = "UserInfo" ;
        public static final String tbl_name = "UserData" ;

    }

}

in the db helper class i created a cursor function :

    public Cursor getInfo(DatabaseOperat dop) {
            SQLiteDatabase sq = dop.getReadableDatabase();
            String columns[] = {TableInfo.first_Name, TableInfo.last_Name, TableInfo.sex, TableInfo.birth_date};
            Cursor CR = sq.query(TableInfo.tbl_name, columns, null, null, null, null, null);
            return CR;
        }

And in the mainActivity i did this to save the data of the columns in array lists :

 DatabaseOperat DOP = new DatabaseOperat(CTX);
        Cursor CR = DOP.getInfo(DOP);
        DOP.getWritableDatabase();
        CR.moveToFirst();
        while (!CR.isAfterLast()) {


            Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
            Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
            Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
            Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
            // Adding contact to list

        }
            CR.close();
            Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();

The error in getting is the following :

android.database.sqlite.SQLiteException: ...no such column: first_name (code 1): , while compiling: SELECT first_name, last_name, sex, birth_date FROM UserData .Any idea where the error is ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – Phantômaxx Dec 28 '16 at 11:04

1 Answers1

0

Change this code :

DatabaseOperat DOP = new DatabaseOperat(CTX);
    Cursor CR = DOP.getInfo(DOP);
    DOP.getWritableDatabase();
    CR.moveToFirst();
    while (!CR.isAfterLast()) {


        Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
        Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
        Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
        Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
        // Adding contact to list

    }
        CR.close();
        Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();

To :

DatabaseOperat DOP = new DatabaseOperat(CTX);
    DOP.getWritableDatabase();        
    Cursor CR = DOP.getInfo(DOP);
    CR.moveToFirst();
    while (!CR.isAfterLast()) {


        Fnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.first_Name)));
        Lnames.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.last_Name)));
        Birthdates.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.sex)));
        Sex.add(CR.getString(CR.getColumnIndex(TableDatas.TableInfo.birth_date)));
        // Adding contact to list

    }
        CR.close();
        Toast.makeText(getApplicationContext(), "Cursor Success", Toast.LENGTH_LONG).show();

First call getWritableDatabase() and than getInfo()

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50