I have a small database app. It requires a user to enter a code 1A1 for example to search the Db.
If I hard code the sql command it will find that row.
Cursor c=db.rawQuery("SELECT * from emdcode WHERE code = '1A1'",null);
On my layout I have a text box for user input
<EditText
android:id="@+id/codelookup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/button3"
android:ems="10" >
<requestFocus />
</EditText>
My question> How do I write the SQL to search for the user input?
I have done this in Java
private void SrchCodeActionPerformed(java.awt.event.ActionEvent evt)
{
String sql = "SELECT * FROM emdcodes WHERE Codes like ?";
try
{
ps = conn.prepareStatement(sql);
ps.setString(1, EMDCODELOOKUP.getText() + "%");
rs = ps.executeQuery();
ResultsTable.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
So I assumned SQLite is similar
I have tried
Cursor c=db.rawQuery("SELECT * from emdcode WHERE code = codelookup",null);
Cursor c=db.rawQuery("SELECT * from emdcode WHERE code = ?",null);
Any Suggestions or guidemnce would be great. Thank you
update
When using Cursor c=db.rawQuery("SELECT * from emdcode WHERE code = ?", new String[]{"codelookup"}); I get a null result set. This is the logcat file:
07-31 00:10:34.735: E/Trace(1765): error opening trace file: No such file or directory (2)
07-31 00:10:38.645: E/AndroidRuntime(1765): FATAL EXCEPTION: main 07-31 00:10:38.645: E/AndroidRuntime(1765): java.lang.IllegalStateException: Could not execute method of the activity 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.view.View$1.onClick(View.java:3599) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.view.View.performClick(View.java:4204) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.view.View$PerformClick.run(View.java:17355) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.os.Handler.handleCallback(Handler.java:725) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.os.Handler.dispatchMessage(Handler.java:92) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.os.Looper.loop(Looper.java:137) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-31 00:10:38.645: E/AndroidRuntime(1765): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 00:10:38.645: E/AndroidRuntime(1765): at java.lang.reflect.Method.invoke(Method.java:511) 07-31 00:10:38.645: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-31 00:10:38.645: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-31 00:10:38.645: E/AndroidRuntime(1765): at dalvik.system.NativeStart.main(Native Method) 07-31 00:10:38.645: E/AndroidRuntime(1765): Caused by: java.lang.reflect.InvocationTargetException 07-31 00:10:38.645: E/AndroidRuntime(1765): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 00:10:38.645: E/AndroidRuntime(1765): at java.lang.reflect.Method.invoke(Method.java:511) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.view.View$1.onClick(View.java:3594) 07-31 00:10:38.645: E/AndroidRuntime(1765): ... 11 more 07-31 00:10:38.645: E/AndroidRuntime(1765): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 07-31 00:10:38.645: E/AndroidRuntime(1765): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 07-31 00:10:38.645: E/AndroidRuntime(1765): at com.example.studentdatabase.MainActivity.showdata(MainActivity.java:47) 07-31 00:10:38.645: E/AndroidRuntime(1765): ... 14 more 07-31 00:11:55.095: E/Trace(1839): error opening trace file: No such file or directory (2) 07-31 00:20:08.084: E/Trace(2020): error opening trace file: No such file or directory (2) 07-31 00:20:16.915: E/AndroidRuntime(2020): FATAL EXCEPTION: main 07-31 00:20:16.915: E/AndroidRuntime(2020): java.lang.IllegalStateException: Could not execute method of the activity 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.view.View$1.onClick(View.java:3599) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.view.View.performClick(View.java:4204) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.view.View$PerformClick.run(View.java:17355) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.os.Handler.handleCallback(Handler.java:725) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.os.Handler.dispatchMessage(Handler.java:92) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.os.Looper.loop(Looper.java:137) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-31 00:20:16.915: E/AndroidRuntime(2020): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 00:20:16.915: E/AndroidRuntime(2020): at java.lang.reflect.Method.invoke(Method.java:511) 07-31 00:20:16.915: E/AndroidRuntime(2020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-31 00:20:16.915: E/AndroidRuntime(2020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-31 00:20:16.915: E/AndroidRuntime(2020): at dalvik.system.NativeStart.main(Native Method) 07-31 00:20:16.915: E/AndroidRuntime(2020): Caused by: java.lang.reflect.InvocationTargetException 07-31 00:20:16.915: E/AndroidRuntime(2020): at java.lang.reflect.Method.invokeNative(Native Method) 07-31 00:20:16.915: E/AndroidRuntime(2020): at java.lang.reflect.Method.invoke(Method.java:511) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.view.View$1.onClick(View.java:3594) 07-31 00:20:16.915: E/AndroidRuntime(2020): ... 11 more 07-31 00:20:16.915: E/AndroidRuntime(2020): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 07-31 00:20:16.915: E/AndroidRuntime(2020): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 07-31 00:20:16.915: E/AndroidRuntime(2020): at com.example.studentdatabase.MainActivity.showdata(MainActivity.java:47) 07-31 00:20:16.915: E/AndroidRuntime(2020): ... 14 more 07-31 00:32:07.675: E/Trace(2296): error opening trace file: No such file or directory (2) 07-31 00:33:22.405: E/Trace(2377): error opening trace file: No such file or directory (2)