如何在Android中编写SQL查询?

时间:2015-02-25 09:48:09

标签: android mysql

请让我知道这是一种正确的查询方式。我想获取一行的所有值来编辑页面。我应该在下面的查询中做出哪些更改?欢迎提出所有建议。

db = helper.getReadableDatabase();
        Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, new String[] {SQLiteHelper.UID,
                        SQLiteHelper.TASK_NAME , SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
                SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME}, SQLiteHelper.UID + "=?",
                new String[] {String.valueOf(id)}, null, null, null, null); 

我正在获得null游标的值。这是由于不正确的查询还是其他原因?

光标操作方法:

public Task getTask(String id) {

    db = helper.getReadableDatabase();
            Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, new String[] {SQLiteHelper.UID,
                            SQLiteHelper.TASK_NAME , SQLiteHelper.CONTACTS, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATETIME, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
                    SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_TIME}, SQLiteHelper.UID + " LIKE ?",
                    new String[] {String.valueOf(id)}, null, null, null);





            if (cursor != null );
            cursor.moveToFirst();


               task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
                        cursor.getString(6), cursor.getString(7), cursor.getString(8));

                Log.d("Pana" , "The value of Task is" + task);



            db.close();
            // return user
            return task;

        }

logcat的:

02-25 15:31:12.343    1317-1317/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ms.t.tms, PID: 1317
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.EditScreen}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
            at com.ms.t.tms.SQLiteDataBaseAdapter.getTask(SQLiteDataBaseAdapter.java:120)
            at com.ms.t.tms.EditScreen.onCreate(EditScreen.java:36)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

3 个答案:

答案 0 :(得分:1)

检查cursor.moveToFirst()返回true或false,然后只获取值:like

if (cursor != null ){
       if( cursor.moveToFirst()){
           task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
                    cursor.getString(6), cursor.getString(7), cursor.getString(8));

            Log.d("Pana" , "The value of Task is" + task);
          }
 }

答案 1 :(得分:0)

代码看起来没问题。请在代码后面的代码后面尝试并检查:

if (cursor != null)
    cursor.moveToFirst();

答案 2 :(得分:0)

if (cursor != null );

绝对不会做任何事情!

分号为“如果为null,则为空语句”。 最好这样做:

if(cursor!=null) 
{
  `enter code here`//do something here!
}