按名称获取单行查询

时间:2014-03-24 03:05:04

标签: android sqlite

我在按名称获取单行查询时遇到问题。这是我在 main.java

中的代码
 String companies=company.getText().toString();

 Cursor port=myDb.getRowLasttran(companies);

在String公司,我有价值“Ace Development Bank Ltd”

在我的 Dbaadapter.java 中,我有这个

public Cursor getRowLasttran(String company) {

    String where = KEY_COMPANIES + "=" + company;
Cursor c =  db.query( DATABASE_TABLE_LASTTRAN, ALL_KEYS_lasttran, where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

在我的logcat中,我有这个错误

`03-23 23:00:40.651: E/SQLiteLog(813): (1) near "Development": syntax error
03-23 23:00:40.671: D/AndroidRuntime(813): Shutting down VM
03-23 23:00:40.671: W/dalvikvm(813): threadid=1: thread exiting with uncaught exception (group=0xb3a6eba8)
03-23 23:00:40.971: E/AndroidRuntime(813): FATAL EXCEPTION: main
03-23 23:00:40.971: E/AndroidRuntime(813): Process: com.example.merostock, PID: 813
03-23 23:00:40.971: E/AndroidRuntime(813): java.lang.IllegalStateException: Could not execute method of the activity
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.view.View$1.onClick(View.java:3823)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.view.View.performClick(View.java:4438)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.view.View$PerformClick.run(View.java:18422)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.os.Handler.handleCallback(Handler.java:733)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.os.Looper.loop(Looper.java:136)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.app.ActivityThread.main(ActivityThread.java:5017)
03-23 23:00:40.971: E/AndroidRuntime(813):  at java.lang.reflect.Method.invokeNative(Native Method)
03-23 23:00:40.971: E/AndroidRuntime(813):  at java.lang.reflect.Method.invoke(Method.java:515)
03-23 23:00:40.971: E/AndroidRuntime(813):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-23 23:00:40.971: E/AndroidRuntime(813):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-23 23:00:40.971: E/AndroidRuntime(813):  at dalvik.system.NativeStart.main(Native Method)
03-23 23:00:40.971: E/AndroidRuntime(813): Caused by: java.lang.reflect.InvocationTargetException
03-23 23:00:40.971: E/AndroidRuntime(813):  at java.lang.reflect.Method.invokeNative(Native Method)
03-23 23:00:40.971: E/AndroidRuntime(813):  at java.lang.reflect.Method.invoke(Method.java:515)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.view.View$1.onClick(View.java:3818)
03-23 23:00:40.971: E/AndroidRuntime(813):  ... 11 more
03-23 23:00:40.971: E/AndroidRuntime(813): Caused by: android.database.sqlite.SQLiteException: near "Development": syntax error (code 1): , while compiling: SELECT _id, companies, todayclose, yesterdayclose FROM lasttran WHERE companies=Ace Development Bank Ltd
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-23 23:00:40.971: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
03-23 23:00:40.971: E/AndroidRuntime(813):  at com.example.merostock.DBAdapter.getRowLasttran(DBAdapter.java:381)
03-23 23:00:40.971: E/AndroidRuntime(813):  at com.example.merostock.AddPortfolio.addportfolio(AddPortfolio.java:65)
03-23 23:00:40.971: E/AndroidRuntime(813):  ... 14 more

`

2 个答案:

答案 0 :(得分:2)

为避免格式化此类问题(以及SQL注入攻击),您应该使用参数:

String where = KEY_COMPANIES + " = ?";
String[] whereArgs = new String[] { company };
db.query(..., where, whereArgs, ...);

您还必须确保数据库中确实存在具有该值的记录。

答案 1 :(得分:-1)

你应该试试这个,对你有所帮助:

public Cursor retriveWard(String companies) {
        Cursor c = null;

        try{

            c = db.rawQuery(
                    "select * where KEY_COMPANIES =" + companies,
                    null);
        }catch (Exception e){
            e.printStackTrace();
        }finally{

        }
        return c;
    }