SQL查询-没有这样的列

时间:2018-10-02 18:49:20

标签: java android sql sqlite

我在Android Studio中获得了这个DataBaseOpenHelper类:

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

一切似乎都可以进行到这里,我可以使用getTableAsString方法打印整个表而没有问题。

现在,我需要另一个活动中的新方法来搜索此cf_table,并将COL2(姓氏)作为输入。

公共类ThirdActivity扩展了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;

}

}

我使用了getTableAsString方法的相同实现。 我收到错误“没有这样的列:COL2”。我不明白为什么有该表,并且可以按我所说的打印它,但是我无法在本活动中完成我需要的特定任务(在查询中说明:为该表提取CF(主键))具有COL2 =输入的行)。该错误显然在rawQuery的行中。

我已经清理并重建了项目,已经关闭了Android Studio并重新启动了模拟器。我在Mac上,几个月前我遇到了相同的错误,该错误是通过“重建并重新启动”过程解决的。

0 个答案:

没有答案