0

我在添加另一列时遇到问题,它只是在 5 列之后出错。它在日志中说我只有 5 列…检查这个 Logcat

11-05 03:31:45.455: I/dalvikvm(3845): Turning on JNI app bug workarounds for target SDK version 8...
11-05 03:31:46.395: D/dalvikvm(3845): GC_FOR_ALLOC freed 78K, 8% free 2671K/2884K, paused 80ms, total 82ms
11-05 03:31:46.405: I/dalvikvm-heap(3845): Grow heap (frag case) to 3.341MB for 635812-byte allocation
11-05 03:31:46.475: D/dalvikvm(3845): GC_FOR_ALLOC freed <1K, 7% free 3291K/3508K, paused 67ms, total 67ms
11-05 03:31:46.595: D/Insert:(3845): Inserting ..
11-05 03:31:46.595: D/Reading:(3845): Reading all naps..
11-05 03:31:47.395: D/(3845): HostConnection::get() New Host Connection established 0x2a20c298, tid 3845
11-05 03:32:01.885: D/dalvikvm(3845): GC_FOR_ALLOC freed 220K, 9% free 3584K/3936K, paused 78ms, total 91ms
11-05 03:32:30.646: D/dalvikvm(3895): GC_FOR_ALLOC freed 42K, 7% free 2671K/2848K, paused 90ms, total 93ms
11-05 03:32:30.667: I/dalvikvm-heap(3895): Grow heap (frag case) to 3.341MB for 635812-byte allocation
11-05 03:32:30.796: D/dalvikvm(3895): GC_FOR_ALLOC freed <1K, 6% free 3291K/3472K, paused 100ms, total 100ms
11-05 03:32:30.876: D/Insert:(3895): Inserting ..
11-05 03:32:30.876: D/Reading:(3895): Reading all naps..
11-05 03:32:30.976: E/CursorWindow(3895): Failed to read row 0, column 5 from a CursorWindow which has 11 rows, 5 columns.
11-05 03:32:30.986: D/AndroidRuntime(3895): Shutting down VM
11-05 03:32:30.986: W/dalvikvm(3895): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-05 03:32:31.016: E/AndroidRuntime(3895): FATAL EXCEPTION: main
11-05 03:32:31.016: E/AndroidRuntime(3895): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.androidsqlite/com.androidhive.androidsqlite.NapDbase}: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.os.Looper.loop(Looper.java:137)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at java.lang.reflect.Method.invoke(Method.java:525)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at dalvik.system.NativeStart.main(Native Method)
11-05 03:32:31.016: E/AndroidRuntime(3895): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.database.CursorWindow.nativeGetString(Native Method)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.database.CursorWindow.getString(CursorWindow.java:434)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at com.androidhive.androidsqlite.DatabaseHandler.getAllNapChecks(DatabaseHandler.java:110)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at com.androidhive.androidsqlite.NapDbase.onCreate(NapDbase.java:75)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.Activity.performCreate(Activity.java:5133)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-05 03:32:31.016: E/AndroidRuntime(3895):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-05 03:32:31.016: E/AndroidRuntime(3895):     ... 11 more

我的数据库。

package com.androidhive.androidsqlite;

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

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

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "NapsManager";

// NapChecks table name
private static final String TABLE_NapS = "Naps";

// NapChecks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_MALL = "mall";
private static final String KEY_LATIT = "latit";
private static final String KEY_LONGIT = "longit";
private static final String KEY_INTER = "inte";
private static final String KEY_CATE = "cate";
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_NapS_TABLE = "CREATE TABLE " + TABLE_NapS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_MALL + " TEXT," + KEY_LATIT + " TEXT,"
            + KEY_LONGIT + " TEXT," + KEY_INTER + " TEXT,"
            + KEY_CATE + " TEXT" +")";
    db.execSQL(CREATE_NapS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NapS);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new nap
void addNapCheck(NapCheck nap) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, nap.getName()); // NapCheck Name
    values.put(KEY_MALL, nap.getMall()); // NapCheck Phone
    values.put(KEY_LATIT, nap.getLatit()); // NapCheck Name
    values.put(KEY_LONGIT, nap.getLongit());
    values.put(KEY_INTER, nap.getInte());
    // Inserting Row
    db.insert(TABLE_NapS, null, values);
    db.close(); // Closing database connection
}

// Getting single nap
NapCheck getNapCheck(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NapS, new String[] { KEY_ID,
            KEY_NAME, KEY_MALL, KEY_LATIT, KEY_LONGIT, KEY_INTER, }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    NapCheck nap = new NapCheck(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2),cursor.getString(3), cursor.getString(4), cursor.getString(5));
    // return nap
    return nap;
}

// Getting All NapChecks
public List<NapCheck> getAllNapChecks() {
    List<NapCheck> NapList = new ArrayList<NapCheck>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NapS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            NapCheck nap = new NapCheck();
            nap.setID(Integer.parseInt(cursor.getString(0)));
            nap.setName(cursor.getString(1));
            nap.setMall(cursor.getString(2));
            nap.setLatit(cursor.getString(3));
            nap.setLongit(cursor.getString(4));
            nap.setLongit(cursor.getString(5));
            // Adding nap to list
            NapList.add(nap);
        } while (cursor.moveToNext());
    }

    // return nap list
    return NapList;
}

// Updating single nap
public int updateNapCheck(NapCheck nap) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, nap.getName());
    values.put(KEY_MALL, nap.getMall());
    values.put(KEY_LATIT, nap.getLatit());
    values.put(KEY_LONGIT, nap.getLongit());
    values.put(KEY_INTER, nap.getLongit());
    // updating row
    return db.update(TABLE_NapS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(nap.getID()) });
}

// Deleting single nap
public void deleteNapCheck(NapCheck nap) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NapS, KEY_ID + " = ?",
            new String[] { String.valueOf(nap.getID()) });
    db.close();
}


// Getting Naps Count
public int getNapChecksCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NapS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

这是我的 Getter 和 Setter

package com.androidhive.androidsqlite;

public class NapCheck {

//private variables
int _id;
String _name;
String _mall;
String latit;
String longit;
String inte;
String cate;
// Empty constructor
public NapCheck(){

}
// constructor
public NapCheck(int id, String name, String _mall,String latit, String longit, String inte){
    this._id = id;
    this._name = name;
    this._mall = _mall;
    this.latit = latit;
    this.longit = longit;
    this.inte = inte;
}

// constructor
public NapCheck(String name, String _mall,String latit, String longit, String inte){
    this._name = name;
    this._mall = _mall;
    this.latit = latit;
    this.longit = longit;
    this.inte = inte;
}
// getting ID
public int getID(){
    return this._id;
}

// setting id
public void setID(int id){
    this._id = id;
}

// getting name
public String getName(){
    return this._name;
}

// setting name
public void setName(String name){
    this._name = name;
}

// getting phone number
public String getMall(){
    return this._mall;
}

// setting phone number
public void setMall(String phone_number){
    this._mall = phone_number;
}


public String getLatit(){
    return this.latit;
}

// setting phone number
public void setLatit(String latit){
    this.latit = latit;
}


public String getLongit(){
    return this.longit;
}

// setting phone number
public void setLongit(String longit){
    this.longit = longit;
}

public String getInte(){
    return this.inte;
}

public void setInte(String inte){
    this.inte = inte;
}

public String getCate(){
    return this.cate;
}

// setting phone number
public void setCate(String cate){
    this.cate = cate;
}
}

我不知道每次我在公共列表 getAllNapChecks() 中设置一列时出现什么问题,它会被窃听并说我只有五列我不知道我已经这样做了好几个小时了..提前谢谢..只是告诉你是否需要更多

public List<NapCheck> getAllNapChecks() {
    List<NapCheck> NapList = new ArrayList<NapCheck>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NapS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            NapCheck nap = new NapCheck();
            nap.setID(Integer.parseInt(cursor.getString(0)));
            nap.setName(cursor.getString(1));
            nap.setMall(cursor.getString(2));
            nap.setLatit(cursor.getString(3));
            nap.setLongit(cursor.getString(4));

这条线是我大部分时间遇到问题的地方..

            nap.setLongit(cursor.getString(5));

            // Adding nap to list
            NapList.add(nap);
        } while (cursor.moveToNext());
    }

    // return nap list
    return NapList;
}
4

3 回答 3

0

The onCreate (SQLiteDatabase db) method in DatabaseHandler is called only once when the database is accessed for the first time. This means that any changes to the schema will not result in this method being called.

To apply the change in schema, you can take either of two actions:

  1. Uninstall and then install the application in the device/emulator.
  2. Increase the value of DATABASE_VERSION in order to trigger a call to onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion). As per your implementation, this will result in the existing table being dropped and recreated.
于 2013-11-05T09:59:09.383 回答
0

我认为 KEY_INTER(last field) 之后不需要的空格和逗号是问题的改变。

Cursor cursor = db.query(TABLE_NapS, new String[] { KEY_ID,
        KEY_NAME, KEY_MALL, KEY_LATIT, KEY_LONGIT, KEY_INTER}, KEY_ID + "=?",
        new String[] { String.valueOf(id) }, null, null, null, null);
于 2013-11-05T09:05:13.313 回答
0

可能这不是您创建的第一个数据库模式。

如果你编辑你的数据库模式,你必须增加你的数据库版本。否则不会调用 onUpdate()。

尝试:

private static final int DATABASE_VERSION = 2;
于 2013-11-05T11:22:04.197 回答