SQL语句中的用户文本导致应用程序崩溃

时间:2015-10-16 14:39:32

标签: android sqlite android-edittext

我有一个edittext,一个按钮和一个sqlite表。按下按钮时,将运行包含输入文本的select语句。如果用户只输入数字,该应用程序可正常运行,但如果包含字母则会崩溃。

这是表格的架构:

    public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String STOP = "entryid";
    public static final String LINE = "title";
    public static final String SCHEDULE = "schedule";
}

表已初始化。 onClick方法是:

public void lookUp(View v) {
        mEdit = (EditText) findViewById(R.id.text);
    String string = mEdit.getText().toString();

    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    String[] projection = {
            FeedReaderContract.FeedEntry.STOP,
            FeedReaderContract.FeedEntry.LINE,
            FeedReaderContract.FeedEntry.SCHEDULE,
    };

    String sortOrder =
            FeedReaderContract.FeedEntry.SCHEDULE + " ASC";

    Cursor cursor = db.query(
            FeedReaderContract.FeedEntry.TABLE_NAME,
            projection,
            FeedReaderContract.FeedEntry.LINE+" = " + string ,
            null,
            null,
            null,
            sortOrder
    );

当我在edittext中有字母时,为什么应用程序会崩溃?列是字符串类型,因此字符串不应该崩溃。

这是logcat错误:

(1) no such column: line1
Shutting down VM
FATAL EXCEPTION: main
Process: com.mycompany.antibes, PID: 2767
java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:4020)
    at android.view.View.performClick(View.java:4780)
    at android.view.View$PerformClick.run(View.java:19866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.view.View$1.onClick(View.java:4015)
    at android.view.View.performClick(View.java:4780) 
    at android.view.View$PerformClick.run(View.java:19866) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 Caused by: android.database.sqlite.SQLiteException: no such column: line1 (code 1): , while compiling: SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
    at com.mycompany.antibes.MainActivity.lookUp(MainActivity.java:61)
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at android.view.View$1.onClick(View.java:4015) 
    at android.view.View.performClick(View.java:4780) 
    at android.view.View$PerformClick.run(View.java:19866) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

1 个答案:

答案 0 :(得分:0)

你的sql是

SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC

这意味着它将标题与列进行比较而不是值。

尝试在字符串周围添加单引号。

Cursor cursor = db.query(
        FeedReaderContract.FeedEntry.TABLE_NAME, 
        projection,
        FeedReaderContract.FeedEntry.LINE+" = \'" + string + "\'" ,
        null, 
        null, 
        null, 
        sortOrder
);

所以你会得到

SELECT entryid, title, schedule FROM entry WHERE title = 'line1' ORDER BY schedule ASC

这似乎是你正在尝试做的事情。