调用sqlite数据库查询时出错

时间:2017-07-05 20:35:10

标签: android 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);
    }
}

2 个答案:

答案 0 :(得分:1)

对我来说,当SQL字符串不需要时,你正在传递一个whereArg ... 尝试

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

代替。

答案 1 :(得分:0)

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

代替上面的代码,替换为

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

这是因为你的whereArgs是空的。