0

我有一个将一些数据(9 列)存储到 SQLite 数据库的应用程序。我遵循了vogella的示例,并用其他一些示例对其进行了调整,直到我认为它应该没问题。现在,我为使我的应用程序运行而苦苦挣扎了两天,但发现自己陷入了 SQLite 错误,最近又遇到了这个错误:无法从具有 1 行 1 列的 CursorWindow 读取第 0 行第 4 列。当我尝试在向 DB 添加任何内容之前首先打开活动 ure .java 时出现错误(但我认为之前的尝试中已经有一些数据)。

这是我的 VnosiDB.java 的代码:

    package com.sandpit.jazstudent;

public class VnosiDB {
    private long _id;
    private int odT;
    private int deltaT;
    private String niz;
    private String delo;
    private int post;
    private int dod;
    private String opis;
    private int vnos;

    public long getId() {
        return _id;
    }

    public void setId(long _id) {
        this._id = _id;
    }

    public int getOdt() {
        return odT;
    }

    public void setOdt(int odT) {
        this.odT = odT;
    }

    public int getDeltat() {
        return deltaT;
    }

    public void setDeltat(int deltaT) {
        this.deltaT = deltaT;
    }

    public String getNiz() {
        return niz;
    }

    public void setNiz(String niz) {
        this.niz = niz;
    }

    public String getDelo() {
        return delo;
    }

    public void setDelo(String delo) {
        this.delo = delo;
    }

    public int getPost() {
        return post;
    }

    public void setPost(int post) {
        this.post = post;
    }

    public int getDod() {
        return dod;
    }

    public void setDod(int dod) {
        this.dod = dod;
    }

    public String getOpis() {
        return opis;
    }

    public void setOpis(String opis) {
        this.opis = opis;
    }

    public int getVnos() {
        return vnos;
    }

    public void setVnos(int vnos) {
        this.vnos = vnos;
    }

    // Will be used by the ArrayAdapter in the ListView
    @Override
    public String toString() {
        return delo;
    }

我的 DatabaseManidzer.java:

    package com.sandpit.jazstudent;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseManidzer extends SQLiteOpenHelper {

    public static final String TABLE_VNOSI = "vnosi";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_ODT = "odT";
    public static final String COLUMN_DELTAT = "deltaT";
    public static final String COLUMN_NIZ = "niz";
    public static final String COLUMN_DELO = "delo";
    public static final String COLUMN_POST = "post";
    public static final String COLUMN_DOD = "dod";
    public static final String COLUMN_OPIS = "opis";
    public static final String COLUMN_VNOS = "vnos";

    private static final String DATABASE_NAME = "dbvnosi.db";
    private static final int DATABASE_VERSION = 1;

    // database creation sql statement
    private static final String DATABASE_CREATE = "CREATE TABLE vnosi (_id INTEGER PRIMARY KEY, odT NUMERIC, deltaT NUMERIC, niz TEXT, delo TEXT, post NUMERIC, dod NUMERIC, opis TEXT, vnos NUMERIC);";

    public DatabaseManidzer(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database){
        try{
        database.execSQL(DATABASE_CREATE);
        } catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        Log.w(DatabaseManidzer.class.getName(),"Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_VNOSI);
        onCreate(db);

    }

}

我的 VnosiDataSource.java:

    package com.sandpit.jazstudent;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class VnosiDataSource {

    // Database fields
    private SQLiteDatabase database;
    private DatabaseManidzer dbHelper;
    private String[] allColumns = { DatabaseManidzer.COLUMN_ID,
            DatabaseManidzer.COLUMN_ODT, DatabaseManidzer.COLUMN_DELTAT,
            DatabaseManidzer.COLUMN_NIZ, DatabaseManidzer.COLUMN_DELO,
            DatabaseManidzer.COLUMN_POST, DatabaseManidzer.COLUMN_DOD,
            DatabaseManidzer.COLUMN_OPIS, DatabaseManidzer.COLUMN_VNOS };

    public VnosiDataSource(Context context) {
        dbHelper = new DatabaseManidzer(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public VnosiDB createVnos(int odt, int deltat, String niz, String delo,
            int post, int dod, String opis, int vnos) {
        ContentValues values = new ContentValues();
        values.put(DatabaseManidzer.COLUMN_ODT, odt);
        values.put(DatabaseManidzer.COLUMN_DELTAT, deltat);
        values.put(DatabaseManidzer.COLUMN_NIZ, niz);
        values.put(DatabaseManidzer.COLUMN_DELO, delo);
        values.put(DatabaseManidzer.COLUMN_POST, post);
        values.put(DatabaseManidzer.COLUMN_DOD, dod);
        values.put(DatabaseManidzer.COLUMN_OPIS, opis);
        values.put(DatabaseManidzer.COLUMN_VNOS, vnos);
        long insertId = database.insert(DatabaseManidzer.TABLE_VNOSI, null,
                values);
        Cursor cursor = database.query(DatabaseManidzer.TABLE_VNOSI,
                allColumns, DatabaseManidzer.COLUMN_ID + " = " + insertId,
                null, null, null, null);
        cursor.moveToFirst();
        VnosiDB newVnos = cursorToVnos(cursor);
        cursor.close();
        return newVnos;
    }

    public void deleteVnos(VnosiDB vnos) {
        long id = vnos.getId();
        System.out.println("Comment deleted with id: " + id);
        database.delete(DatabaseManidzer.TABLE_VNOSI,
                DatabaseManidzer.COLUMN_ID + " = " + id, null);
    }

    public List<VnosiDB> getAllDela() {

        List<VnosiDB> dela = new ArrayList<VnosiDB>();

        Cursor cursor = database.rawQuery("SELECT delo from vnosi", null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            VnosiDB curdela = cursorToDela(cursor);
            dela.add(curdela);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return dela;
    }

    private VnosiDB cursorToDela(Cursor cursor) {
        VnosiDB curdela = new VnosiDB();
        curdela.setId(cursor.getLong(0));
        curdela.setDelo(cursor.getString(4));
        return curdela;
    }

    private VnosiDB cursorToVnos(Cursor cursor) {
        VnosiDB vnos = new VnosiDB();
        vnos.setId(cursor.getInt(0));
        vnos.setOdt(cursor.getInt(1));
        vnos.setDeltat(cursor.getInt(2));
        vnos.setNiz(cursor.getString(3));
        vnos.setDelo(cursor.getString(4));
        vnos.setPost(cursor.getInt(5));
        vnos.setDod(cursor.getInt(6));
        vnos.setOpis(cursor.getString(7));
        vnos.setVnos(cursor.getInt(8));
        return vnos;
    }
}

最后是一个调用数据库的 Ure.java:

    package com.sandpit.jazstudent;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Ure extends ListActivity {

    private VnosiDataSource datasource;

    SparseArray<Group> groups = new SparseArray<Group>();

    @SuppressLint("SdCardPath")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.layout.push_left_in, R.layout.push_left_out);
        setContentView(R.layout.moje_ure);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        datasource = new VnosiDataSource(this);
        datasource.open();

        List<VnosiDB> values = datasource.getAllDela();
        ArrayAdapter<VnosiDB> adapter = new ArrayAdapter<VnosiDB>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

        TextView novVnos = (TextView) findViewById(R.id.novVnosNaslov);
        novVnos.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.sandpit.jazstudent.VNOS"));
            }
        });
    }

    @Override
    protected void onResume() {
        datasource.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        datasource.close();
        super.onPause();
    }

    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.layout.push_right_in,
                R.layout.push_right_out);
    }

}

我用这段代码写信给数据库:

    shrani = (TextView) findViewById(R.id.shrani);
shrani.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        vnosDB = (int)myCalendar.getTimeInMillis();

        datasource.createVnos(zacetekDB, razlikaDB, nizDB, deloDB, postavkaDB, dodatekDB, opisDB, vnosDB);
        datasource.close();
        Toast test = Toast.makeText(Vnos.this, "Vnos " + deloDB + " uspešen!", Toast.LENGTH_LONG);
        test.show();
        finish();

    }
});

}

错误是:

    09-29 20:24:57.360: E/CursorWindow(17150): Failed to read row 0, column 4 from a CursorWindow which has 1 rows, 1 columns.
09-29 20:24:57.360: D/AndroidRuntime(17150): Shutting down VM
09-29 20:24:57.360: W/dalvikvm(17150): threadid=1: thread exiting with uncaught exception (group=0x412be2a0)
09-29 20:24:57.365: E/AndroidRuntime(17150): FATAL EXCEPTION: main
09-29 20:24:57.365: E/AndroidRuntime(17150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sandpit.jazstudent/com.sandpit.jazstudent.Ure}: java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.os.Looper.loop(Looper.java:137)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread.main(ActivityThread.java:4921)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at java.lang.reflect.Method.invokeNative(Native Method)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at java.lang.reflect.Method.invoke(Method.java:511)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at dalvik.system.NativeStart.main(Native Method)
09-29 20:24:57.365: E/AndroidRuntime(17150): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 4 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.database.CursorWindow.nativeGetString(Native Method)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.database.CursorWindow.getString(CursorWindow.java:438)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at com.sandpit.jazstudent.VnosiDataSource.cursorToDela(VnosiDataSource.java:84)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at com.sandpit.jazstudent.VnosiDataSource.getAllDela(VnosiDataSource.java:72)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at com.sandpit.jazstudent.Ure.onCreate(Ure.java:35)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.Activity.performCreate(Activity.java:5206)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-29 20:24:57.365: E/AndroidRuntime(17150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-29 20:24:57.365: E/AndroidRuntime(17150):    ... 11 more

希望我提供了解决此问题可能需要的所有代码。感谢你的帮助!谢谢!

4

2 回答 2

2

根据错误,您试图访问返回游标中的第 5 列,尽管它只有 1 列。

在您的VnosiDataSource.java中,尝试更改第 84 行,即

curdela.setDelo(cursor.getString(4));

curdela.setDelo(cursor.getString(
    cursor.getColumnIndexOrThrow(DatabaseManidzer.COLUMN_DELO)));
于 2013-09-29T20:49:12.480 回答
0

该函数cursorToDela尝试从游标中读取第五列,但getAllDela只为该游标选择一列。

于 2013-09-29T20:41:43.693 回答