2017-07-05 31 views
0

從代碼運行時出現以下異常。該消息談論哪個論點?調用sqlite數據庫查詢時出錯

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters. 
at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212) 
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166) 
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) 
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345) 
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1284) 
at su.joel.punchintime.ShowRawDB.displayDatabaseInfo(ShowRawDB.java:53) 
at su.joel.punchintime.ShowRawDB.onCreate(ShowRawDB.java:19) 

在這一行:Cursor cursor = db.rawQuery(queryString, whereArgs);

活動:

package su.joel.punchintime; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import java.util.ArrayList; 
import java.util.List; 
public class ShowRawDB extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_raw_db); 
     displayDatabaseInfo(); 
    } 
    private void displayDatabaseInfo() { 
     DatabaseHelper DBHelper = new DatabaseHelper(this); 
     SQLiteDatabase db = DBHelper.getReadableDatabase(); 
     String[] projection = { 
       DBHelper.COL_ID, 
       DBHelper.COL_DATE, 
       DBHelper.COL_PUNCHIN 
     }; 
     String selection = "null"; 
     String[] selectionArgs = { "null" }; 
     String sortOrder = 
       DBHelper.COL_DATE + " DESC"; 
     String queryString = 
       "SELECT * FROM punchlog"; 
     String[] whereArgs = new String[] {""}; 
     Cursor cursor = db.rawQuery(queryString, whereArgs); 
     List itemIds = new ArrayList<>(); 
     while(cursor.moveToNext()) { 
      long itemId = cursor.getLong(
        cursor.getColumnIndexOrThrow(DBHelper.COL_ID)); 
      itemIds.add(itemId); 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, itemIds); 
     ListView lst = (ListView) findViewById(R.id.listDB); 
     lst.setAdapter(adapter); 
     cursor.close(); 
    } 
} 

和數據庫幫手:

package su.joel.punchintime; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public final class DatabaseHelper extends SQLiteOpenHelper { 
     public static final String TABLE_NAME = "punchlog"; 
     public static final String COL_ID = "_id"; 
     public static final String COL_DATE = "date"; 
     public static final String COL_PUNCHIN = "punchin"; 
     public static final String COL_PUNCHOUT = "punchout"; 
     public static final String COL_ONDUTY = "onduty"; 
     public static final String COL_FORGOTIN = "forgotin"; 
     public static final String COL_FORGOTOUT = "forgotout"; 
     public static final String COL_LATEIN = "latein"; 
     public static final String COL_LATEOUT = "lateout"; 
    String SQL_CREATE_ENTRIES = 
      "CREATE TABLE " + TABLE_NAME + " (" + 
        COL_ID + " INTEGER PRIMARY KEY," + 
        COL_DATE + " TEXT," + 
        COL_PUNCHIN + " TEXT," + 
        COL_PUNCHOUT + " TEXT," + 
        COL_ONDUTY + " INTEGER," + 
        COL_FORGOTIN + " INTEGER," + 
        COL_FORGOTOUT + " INTEGER," + 
        COL_LATEIN + " INTEGER," + 
        COL_LATEOUT + " INTEGER);"; 
    String SQL_DELETE_ENTRIES = 
      "DROP TABLE IF EXISTS " + TABLE_NAME; 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "PunchInTime.db"; 
    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL(SQL_DELETE_ENTRIES); 
     System.out.println(SQL_CREATE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
} 

回答

1

在我看來,你是在傳遞一個單一whereArg SQL字符串預計沒有... 嘗試

String[] whereArgs = new String[] {};

改爲。

0
Cursor cursor = db.rawQuery(queryString, whereArgs); 

,而不是上面的代碼,用

Cursor cursor = db.rawQuery(queryString, null); 

更換這是因爲你的whereArgs是空的。