Home>

In android studio, I enter a user name in EditText and make a program that determines whether the entered user name exists in the database. I am trying to check the text with TextWatcher and compare it with the value of the database each time, but an error occurs.

Error message

If i try to enter text in et.user, the following error occurs:

11-20 21: 46: 45.465 10998-10998/com.example.keita.databasesample2 E/SQLiteLog: (1) no such column: k
11-20 21: 46: 45.467 10998-10998/com.example.keita.databasesample2 D/AndroidRuntime: Shutting down VM
11-20 21: 46: 45.471 10998-10998/com.example.keita.databasesample2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.keita.databasesample2, PID: 10998
    android.database.sqlite.SQLiteException: no such column: k (code 1):, while compiling: SELECT * FROM namelist WHERE name = k
Applicable source code
@Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        this.bt_save = findViewById (R.id.btSave);
        this.lv_user = findViewById (R.id.lvUser);
        et_user = findViewById (R.id.etUser);
        et_user.addTextChangedListener (this);
        et_password = findViewById (R.id.etPassword);

        Listener listener = new Listener ();
        this.bt_save.setOnClickListener (listener);
        this.bt_save.setEnabled (false);
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
    }
    @Override
    public void beforeTextChanged (CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged (CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged (Editable s) {
        DatabaseHelper helper = new DatabaseHelper (MainActivity.this);
        SQLiteDatabase db = helper.getWritableDatabase ();
        try {
            String sql = "SELECT * FROM namelist WHERE name =" + s.toString ();
            Cursor cursor = db.rawQuery (sql, null);
            // String name = "";
            while (cursor.moveToNext ()) {
                // int idxNote = cursor.getColumnIndex ("name");
                // name = cursor.getString (idxNote);
            }
            // EditText etNote = findViewById (R.id.etNote);
            //etNote.setText(note);
        }
        finally {
            db.close ();
        }
    }

After commenting out and checking the location where the error occurred, the app seems to stop at the SELECT statement.

Supplemental information (FW/tool version etc.)

Android studio3.1.4

  • Answer # 1

      

    no such column: k (code 1):, while compiling: SELECT * FROM namelist WHERE name = k

    I am angry that there is no

    k column.

    In SQL, the hint is that strings must be enclosed in single quotes.

Related articles