Android SQL查询语法

时间:2013-12-10 01:10:14

标签: android sqlite

我试图在Android上的SQLite数据库上运行SQL查询,但是当我运行查询时它崩溃了。

在我的DBhelper中我有一个函数getFlex,它接受从微调器和edittext字符串传递的字符串变量。

传递的信息代表数据库中的列 airportIata是文字 指定是文本 体重是真的

这是函数

public Cursor getFlex(String iata, String designation, String weight) { 
    String airportiata = iata;
    String runway = designation;
    String enteredWeight = weight;

    String sql = "SELECT airportIata, runwayDesignation, flex, weight, v1, vr, v2, minV1, minVr, minV2, engOut FROM flexes WHERE airportIata = ? AND runwayDesignation = ? AND weight >= ? LIMIT 1";

    Cursor c =  db.rawQuery(sql, new String[]{airportiata, runway, enteredWeight});

    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

这里是我在MainActivity中使用按钮单击

调用此函数的地方
protected void calculate(View v) {
    // TODO Auto-generated method stub
    airportiata = (((airportspinner) airportSp.getSelectedItem()).getairportiata().toString());
    designation = (runwaySp.getSelectedItem().toString());
    EditText weightTV = (EditText) findViewById(R.id.weightET);

    weightText = weightTV.getText().toString();
    String message = "";

    Cursor flexCur = myDbHelper.getFlex(airportiata, designation, weightText);

    if (flexCur.moveToFirst())
    {
        do {
            // Process the data:
            int flex = flexCur.getInt(2);
            float weight = flexCur.getInt(3);
            int v1 = flexCur.getInt(4);
            int vr = flexCur.getInt(5);
            int v2 = flexCur.getInt(6);
            int minV1 = flexCur.getInt(7);
            int minVr = flexCur.getInt(8);
            int minV2 = flexCur.getInt(9);
            int engOut = flexCur.getInt(10);

            // Append data to the message:
            message += "flex = " + flex
                       +", weight =" + weight
                       +", v1=" + v1
                       +", vr=" + vr
                       +", v2=" + v2
                       +", minV1=" + minV1
                       +", minVr=" + minVr
                       +", minV2=" + minV2
                       +", engOut=" + engOut
                       +"\n";
        } while(flexCur.moveToNext());
    }

    TextView display = (TextView) findViewById(R.id.displayTV);
    display.setText(airportiata + " " + designation + " " + weightText);

    TextView fromDB = (TextView) findViewById(R.id.formDB);
    fromDB.setText(message);
}

这是我的LogCat:

12-10 18:02:01.380: E/AndroidRuntime(702): FATAL EXCEPTION: main
12-10 18:02:01.380: E/AndroidRuntime(702): Process: com.waleed.sqlite1, PID: 702
12-10 18:02:01.380: E/AndroidRuntime(702): java.lang.NullPointerException
12-10 18:02:01.380: E/AndroidRuntime(702):  at com.waleed.sqlite1.DatabaseHelper.getFlex(DatabaseHelper.java:209)
12-10 18:02:01.380: E/AndroidRuntime(702):  at com.waleed.sqlite1.MainActivity.calculate(MainActivity.java:71)
12-10 18:02:01.380: E/AndroidRuntime(702):  at com.waleed.sqlite1.MainActivity$1.onClick(MainActivity.java:56)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.view.View.performClick(View.java:4424)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.view.View$PerformClick.run(View.java:18383)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.os.Handler.handleCallback(Handler.java:733)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.os.Handler.dispatchMessage(Handler.java:95)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.os.Looper.loop(Looper.java:137)
12-10 18:02:01.380: E/AndroidRuntime(702):  at android.app.ActivityThread.main(ActivityThread.java:4998)
12-10 18:02:01.380: E/AndroidRuntime(702):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 18:02:01.380: E/AndroidRuntime(702):  at java.lang.reflect.Method.invoke(Method.java:515)
12-10 18:02:01.380: E/AndroidRuntime(702):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-10 18:02:01.380: E/AndroidRuntime(702):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-10 18:02:01.380: E/AndroidRuntime(702):  at dalvik.system.NativeStart.main(Native Method)

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我发现了我的错误

我没有调用db = this.getReadableDatabase();在函数中的Cursor之前。

非常感谢:)