db.query及其方法

时间:2018-02-09 16:45:15

标签: android android-sqlite

我尝试使用游标在最后一条记录中读取第二列。 首先,查询和方法的代码布局是否可行?如果没有可以改进的地方?

其次,我缺乏检测错误的经验,并希望得到纠正代码的建议。

来自班级:

SQLiteDatabase db = this.getWritableDatabase();
try {
                Cursor cursor = this.getSurveyData();
                if(cursor.moveToLast())
                    {cursor.moveToLast();
                        String name = cursor.getString(2);
                        Log.v(TAG,name);
                    }
                cursor.close();
            }catch (SQLiteException e )
                                        {
                                            Log.v(TAG, "Exception " + e.getMessage());
                                        }

查询:

    public Cursor getSurveyData(){
      SQLiteDatabase db = this.getWritableDatabase();
    String[] projection = {
             COLUMN_DATO1
                          };
         // String sortOrder = "ID1" + " DESC";

      return db.query(
              table1,
              projection,
              null,null,null,null,null,null
                     );
  }    

调试错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.android.parkinsonaider, PID: 4092
              java.lang.IllegalStateException: Couldn't read row 6, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                  at android.database.CursorWindow.nativeGetString(Native Method)
                  at android.database.CursorWindow.getString(CursorWindow.java:438)
                  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                  at com.example.android.parkinsonaider.DatabaseHelper.insertData1(DatabaseHelper.java:153)
                  at com.example.android.parkinsonaider.InfoActivity$1.onClick(InfoActivity.java:91)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

与目标虚拟机断开连接,地址:' localhost:8603',传输:' socket'

谢谢大家,我重写了一下,现在正在工作。接下来是比较日期,看看是否要追加或插入数据库。那是另一个故事。

String dd1="",dbdato="";
            SQLiteDatabase db = this.getWritableDatabase();
            long millis1=System.currentTimeMillis();
            final Date dat1=new java.sql.Date(millis1);
            dd1 = dat1.toString();

            Cursor cursor = db.query(table1,new String[]{COLUMN_DATO1},null,null,null,null,null);
            if (cursor.moveToLast()) {
            cursor.moveToLast();
            dbdato = cursor.getString(cursor.getColumnIndex(COLUMN_DATO1));
            }

1 个答案:

答案 0 :(得分:0)

您的投影只包含一列,您需要添加所有想要填充的内容:

 String[] projection = {
         COLUMN_DATO1,
         COLUMN_DATO2
                      };