2014-12-31 133 views
-1

我的Android與SQLite數據庫有多個問題。我是自己開發和學習的新手。所以請使用laymen的條款,並假設我不知道你在說什麼大聲笑。具體我有麻煩機智的方法:Android的SQLite數據庫中的錯誤

  • getLast()
  • getLatestSubjectForContact()
  • getAllRows()
  • 的isEmpty()

如果你看到任何其他錯誤,我會接受反饋。感謝您提供的所有幫助。這裏是我的數據庫代碼

package com.swavey.testing; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.provider.BaseColumns; 
import android.security.KeyChain; 

import java.util.Date; 

/** 
* Created by Adrian on 11/5/2014. 
*/ 
public class smsDatabase { 

    private static final String KEY_ID = "_id"; 
    private static final int COLUMN_ID =0; 

    // database info 
    public static final String DATABASE_NAME = "texts"; 
    public static final String DATABASE_TABLE = "mainTable"; 
    public static final int DATABASE_VERSION = 4; 

    // list of fields 
    public static final String KEY_ADDRESS = "address"; 
    public static final String KEY_BODY = "body"; 
    private static final String KEY_DATE = "date"; 
    private static final String KEY_READ = "read"; 
    private static final String KEY_THREADID = "thread_id"; 
    private static final String KEY_TYPE = "type"; 
    private static final String KEY_SEEN = "seen"; 

    //list of field numbers 
    private static final int COL_ADDRESS = 1; 
    private static final int COL_BODY = 2; 
    private static final int COL_DATE = 3; 
    private static final int COL_READ = 4; 
    private static final int COL_THREADID = 5; 
    private static final int COL_TYPE = 6; 
    private static final int COL_SEEN = 7; 

    //create string array of all fields; 
    public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADDRESS, KEY_BODY, KEY_DATE, 
     KEY_READ, KEY_THREADID, KEY_TYPE, KEY_SEEN}; 


    private static final String DATABASE_CREATE_SQL = "create table " + DATABASE_TABLE 
      + " (" + KEY_ID + " integer primary key autoincrement, " 
      +KEY_ADDRESS + " text not null, " 
      +KEY_BODY + " text not null, " 
      +KEY_DATE + " text not null, " 
      +KEY_READ+ " text not null, " 
      +KEY_THREADID+ " text not null, " 
      +KEY_TYPE+ " text not null, " 
      +KEY_SEEN+ " text not null" 
      + ");"; 

    private final Context context; 

    private DatabaseHelper dbHelper; 
    private SQLiteDatabase db; 

    public smsDatabase (Context cxt) { 
     this.context = cxt; 
     dbHelper = new DatabaseHelper(context); 
    } 

    //open database 
    public void open() { 
     db = dbHelper.getWritableDatabase(); 
    } 
    //close database 
    public void close() { 
     dbHelper.close(); 
    } 

    //returns a cursor with all rows loaded 
    public Cursor getAllRows() { 
     String where = null; 
     Cursor cursor = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, 
     null, null); 

     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     return cursor; 
    } 

    public boolean isEmpty() { 
     String where = null; 
     Cursor cursor = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, 
       null, null); 
     if (cursor!=null) return false; 
     return true; 
    } 


    // insert sms into table 
    public long insertSMS (SMS sms) { 
     ContentValues iv = new ContentValues(); 
     iv.put(KEY_ADDRESS, sms.getAddress()); 
     iv.put(KEY_BODY, sms.getBody()); 
     iv.put(KEY_DATE, sms.getDate()); 
     iv.put(KEY_READ, sms.getRead()); 
     iv.put(KEY_THREADID, sms.getThread_id()); 
     iv.put(KEY_TYPE, sms.getType()); 
     iv.put(KEY_SEEN, sms.getSeen()); 




     return db.insert(DATABASE_TABLE, null, iv); 
    } 

    public Cursor getLast() { 
     SMS txt = new SMS(); 
     String where = null; 
     Cursor c = db.query(true, DATABASE_TABLE,ALL_KEYS, where, null, null, null, null, null); 
     c.moveToLast(); 
     return c; 

    } 

    public void deleteLast() { 
     String where = null; 
     Cursor c = db.query(true, DATABASE_TABLE,ALL_KEYS, where, null, null, null, null, null); 
     String las = Integer.toString(c.getCount()); 
     db.delete(DATABASE_TABLE, KEY_ID + "=" + las, null); 
    } 

    public void deleteRow(String address, String date, String body) { 
     db.delete(DATABASE_TABLE, KEY_ADDRESS + "=" + address +" and " + KEY_DATE + "=" + 
     date + " and " + KEY_BODY + "=" + body, null); 
    } 

    public String getLatestSubjectForContact() { 
     Cursor c = getLast(); 
     String sub = c.getString(c.getColumnIndex("body")); 
     if (sub.length() > 30) { 
      sub = sub.substring(0, 30) + "..."; 
      return sub; 
     } 
     sub = sub + "..."; 
     return sub; 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper { 
     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     public void onCreate(SQLiteDatabase _db) { 
      _db.execSQL(DATABASE_CREATE_SQL); 
     } 


     public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { 
      _db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE); 
      onCreate(_db); 
     } 
    } 
} 

的logcat的getLast()方法:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.swavey.testing/com.swavey.testing.MainActivity}: java.lang.IllegalStateException: Couldn't read row 23, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
      at android.app.ActivityThread.access$600(ActivityThread.java:130) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4745) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.IllegalStateException: Couldn't read row 23, col -1 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:434) 
      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
      at com.swavey.testing.MainActivity.addText(MainActivity.java:144) 
      at com.swavey.testing.MainActivity.initialSync(MainActivity.java:105) 
      at com.swavey.testing.MainActivity.Sync(MainActivity.java:71) 
      at com.swavey.testing.MainActivity.onCreate(MainActivity.java:41) 
      at android.app.Activity.performCreate(Activity.java:5008) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
            at android.app.ActivityThread.access$600(ActivityThread.java:130) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4745) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

的建議,試圖從如果遊標檢查獲取數據之前查詢返回的東西。 – issathink

+0

我該如何做? –

+0

像這樣:'if(c.getCount()> 0)' – issathink

回答

1

試試這個:

public Cursor getLast() { 
    SMS txt = new SMS(); 
    String query = "SELECT * FROM mainTable"; 
    Cursor c = db.rawQuery(query, null); 
    if(c != null && c.getCount() > 1) 
     return c.moveToLast(); 
    return null; 
} 

public void deleteLast() { 
    String query = "SELECT KEY_ID from mainTable order by KEY_ID DESC limit 1"; 
    long lastId = -1; 
    Cursor c = db.rawQuery(query, null); 
    if (c != null && c.moveToFirst()) { 
     lastId = c.getLong(0); 
    } 

    if(lastId != -1) 
     db.delete(DATABASE_TABLE, KEY_ID + "=" + lastId, null); 
} 
+0

它仍然拋出相同的錯誤。在執行此方法之前,數據庫遊標具有正確的計數4.只需運行此方法即可返回數據庫遊標數爲24並引發所述錯誤。 –

+0

它不是C是空錯誤,所以答案不起作用。計數是24.很可能它的查詢有問題。 但仍然,整個logcat現在poseks。 –

+0

我認爲問題來自deleteLast(),我有種重寫它看看。 – issathink