1

I got this DataBaseOpenHelper class in Android Studio:

public class DatabaseOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "cf_db.db";
public static final String TABLE_NAME = "cf_table";
public static final String CF = "CF";
public static final String COL1 = "Name";
public static final String COL2 = "Surname";
public static final String COL3 = "Sex";
public static final String COL4 = "Birthday";
public static final String COL5 = "PlaceOfBirth";


public DatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();


}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME +
            "(" + CF + " TEXT PRIMARY KEY, " + COL1 + " TEXT NOT NULL, " +
            COL2 + " TEXT NOT NULL," + COL3 + " TEXT NOT NULL," + COL4 +
            " TEXT NOT NULL," + COL5 + " TEXT NOT NULL);");

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public boolean insertData(String cf, String name, String surname, String sex, String year,
                          String month, String day, String place) {
    String date = day + "/" + month + "/" + year;
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(CF, cf);
    contentValues.put(COL1, name);
    contentValues.put(COL2, surname);
    contentValues.put(COL3, sex);
    contentValues.put(COL4, date);
    contentValues.put(COL5, place);
    long id = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);

    sqLiteDatabase.close();

    return (id != -1);
}


public String getTableAsString(SQLiteDatabase db, String tableName) {
    Log.d("ciao", "getTableAsString called");
    String tableString = String.format("Table %s:\n", tableName);
    Cursor allRows = db.rawQuery("SELECT * FROM " + tableName, null);
    if (allRows.moveToFirst()) {
        String[] columnNames = allRows.getColumnNames();
        do {
            for (String name : columnNames) {
                tableString += String.format("%s: %s\n", name,
                        allRows.getString(allRows.getColumnIndex(name)));
            }
            tableString += "\n";

        } while (allRows.moveToNext());
    }
    return tableString;
}

Everything seems to work till here, I can print the whole table with no issues with the getTableAsString method.

Now I need a new method in another Activity that search through this cf_table, having COL2 (Surname) as an input.

public class ThirdActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);
    EditText editText_search;
    Button button_ok;

   editText_search = findViewById(R.id.editText4);
   String searchtext = editText_search.getText().toString();
   button_ok = findViewById(R.id.button_ok);

   button_ok.setOnClickListener(
           new View.OnClickListener() {
               @Override
               public void onClick(View view) {

                   Log.d("ciao", searchtext); //doesn't print this

                   CFsearch(searchtext);


               }
           }
   );

}

public String CFsearch(String search) {

    DatabaseOpenHelper db = new DatabaseOpenHelper(getApplicationContext());
    String tag = "print";
    SQLiteDatabase sqldb = db.getDB(db);
    String result = String.format("Table %s:\n", "cf_table");
    Cursor cursor = sqldb.rawQuery("SELECT CF FROM cf_table " +
            "WHERE Surname = ?", new String[] {search});
    if (cursor.moveToFirst()) {
        String[] columnNames = cursor.getColumnNames();
        do {
            for (String name : columnNames) {
                result += String.format("%s: %s\n", name,
                        cursor.getString(cursor.getColumnIndex(name)));
            }
            result += "\n";

        } while (cursor.moveToNext());
    }
    Log.d("debug", result);
    return result;

}

}

I used the same implementation of the getTableAsString method. I got the error "no such column: COL2". I don't understand why the table is there and I can print it as I said, but I can't do it in this Activity with the specific task I need (explained in the query: extract the CF(primary key) for the rows with COL2 = input). The error is obviously in the rawQuery's row.

I already did Clean & Rebuild the project, I already shut Android Studio off and Rebooted the Emulator. I'm on a Mac and I had the same error months ago which I solved with the Rebuild and Restart "procedure".

oiac412245
  • 13
  • 5

0 Answers0