简体   繁体   中英

android sqlite query search with “LIKE” error

I have been stuck on this one for the past couple of hours (sadly).

I am building an application with SQLite where the application can do all the flexible SQL commands. I am currently stuck on how to query from columns other than the ID.

my code for retrieving information from the second column of the table is like so:

      public String nameSearch(String n) {
    String[] columns = { KEY_ID, KEY_NAME, KEY_DESCRIPTION };
    Cursor c = myDatabase.query(true, DATABASE_TABLE, columns, KEY_NAME + "LIKE '%"+n+"%'", null, null, null, null, null);
    while(c != null){
        c.moveToFirst();
        String data = c.getString(1);
        return data;
    }
    return null;
}

My code on the onClick command and this is in a seperate class to explain the context:

        case R.id.nameSearchButton:
        String n = etName.getText().toString();
        try {
            SQLControls controller = new SQLControls(this);
            controller.nameSearch(n);
            String gotName = controller.nameSearch(n).toString();
            controller.close();
            tvName.setText(gotName);
            Log.d(TAG, " got " + gotName);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, " error at " + e);
        }
        break;

I know it's more or less pretty simple, but I think there's one thing that I am not doing or not seeing that's kicking my behind. For additional info, the logCat information that displays is as such whenever you hit the button click:

04-16 23:49:14.616: W/KeyCharacterMap(596): No keyboard for id 0
04-16 23:49:14.616: W/KeyCharacterMap(596): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-16 23:49:17.680: W/System.err(596): java.lang.NullPointerException
04-16 23:49:17.685: W/System.err(596):  at com.example.sqltest.SQLControls.nameSearch(SQLControls.java:175)
04-16 23:49:17.685: W/System.err(596):  at com.example.sqltest.CustomSearch.onClick(CustomSearch.java:72)
04-16 23:49:17.685: W/System.err(596):  at android.view.View.performClick(View.java:2485)
04-16 23:49:17.685: W/System.err(596):  at android.view.View$PerformClick.run(View.java:9080)
04-16 23:49:17.685: W/System.err(596):  at android.os.Handler.handleCallback(Handler.java:587)
04-16 23:49:17.685: W/System.err(596):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 23:49:17.685: W/System.err(596):  at android.os.Looper.loop(Looper.java:130)
04-16 23:49:17.685: W/System.err(596):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 23:49:17.685: W/System.err(596):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 23:49:17.685: W/System.err(596):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 23:49:17.685: W/System.err(596):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 23:49:17.685: W/System.err(596):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 23:49:17.685: W/System.err(596):  at dalvik.system.NativeStart.main(Native Method)
04-16 23:49:17.685: E/Custom Search(596):  error at java.lang.NullPointerException

Thanks a lot for your help. Everyone who has answered my questions thus far has been of significant assistance, even for things so simple and minute.

You miss a space before LIKE , and c is never null you have to check if c.moveToFirst is true

Cursor c = myDatabase.query(true, DATABASE_TABLE, columns, KEY_NAME + " LIKE '%"+n+"%'", null, null, null, null, null);
if (c.moveToFirst())
{
    String data = c.getString(1);
    return data;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM