2015-05-26 74 views
-1

上面的代碼是intent服務的一部分,我正在訪問實際上是DBHelper類的GinisTable類。在調用getReadableDatabase時獲取SQLiteException

protected void onHandleIntent(Intent intent) { 
     if (intent != null) { 
      DeviceInfoTable deviceInfoTable = new DeviceInfoTable(UploadGiniData.this); 
      DeviceInfo deviceInfo = deviceInfoTable.getDeviceInfo(); 

      String generatedDeviceInfo = deviceInfo.getGeneratedDeviceId(); 

      GinisTable ginisTable = new GinisTable(UploadGiniData.this); 
      List<GiniInfo> giniInfoList = ginisTable.getGiniInfoList(); 
} 

我GinisTable類是如下:

package com.mastishka.pocketgini.database.tables; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.widget.Toast; 

import com.mastishka.pocketgini.Config; 
import com.mastishka.pocketgini.database.beans.GiniInfo; 

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

public class GinisTable extends SQLiteOpenHelper{ 

    public static final String TABLE_GINI     = "ginis"; 
    public static final String FIELD_GINI_ID    = "gini_id"; 
    public static final String FIELD_NAME     = "name"; 
    public static final String FIELD_PROFESSION   = "profession"; 
    public static final String FIELD_GINI_PIC_PATH  = "gini_pic_path"; 
    public static final String FIELD_EXPERIENCE   = "experience"; 
    public static final String FIELD_PRIMARY_MOBILE  = "primary_mobile"; 
    public static final String FIELD_ISD_CODE    = "isd_code"; 
    public static final String FIELD_GENDER    = "gender"; 
    public static final String FIELD_DOB     = "dob"; 
    public static final String FIELD_LATITUDE    = "latitude"; 
    public static final String FIELD_LONGITUDE   = "longitude"; 
    public static final String FIELD_ADDRESS_LINE1  = "address_line1"; 
    public static final String FIELD_ADDRESS_LINE2  = "address_line2"; 
    public static final String FIELD_CITY     = "city"; 
    public static final String FIELD_STATE    = "state"; 
    public static final String FIELD_COUNTRY    = "country"; 
    public static final String FIELD_PHOTO_ID    = "photo_id"; 
    public static final String FIELD_PHOTO_ID_NUMBER  = "photo_id_number"; 
    public static final String FIELD_PHOTO_ID_PIC_PATH = "photo_id_pic_path"; 

    public Context context; 

    public GinisTable(Context context) { 
     super(context, Config.DB_NAME, null, 1); 
     this.context = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE IF NOT EXISTS " 
      +GinisTable.TABLE_GINI+"(" 
      +GinisTable.FIELD_GINI_ID+" INTEGER PRIMARY KEY, " 
      +GinisTable.FIELD_NAME+" VARCHAR(128), " 
      +GinisTable.FIELD_PROFESSION +" VARCHAR(128), " 
      +GinisTable.FIELD_GINI_PIC_PATH+" VARCHAR(128), " 
      +GinisTable.FIELD_EXPERIENCE+" INT, " 
      +GinisTable.FIELD_PRIMARY_MOBILE+" VARCHAR(15), " 
      +GinisTable.FIELD_ISD_CODE+" VARCHAR(15)," 
      +GinisTable.FIELD_GENDER+" TINYINT," 
      +GinisTable.FIELD_DOB+" VARCHAR(15)," 
      +GinisTable.FIELD_LATITUDE+" DOUBLE," 
      +GinisTable.FIELD_LONGITUDE+" DOUBLE," 
      +GinisTable.FIELD_ADDRESS_LINE1+" VARCHAR(64)," 
      +GinisTable.FIELD_ADDRESS_LINE2+" VARCHAR(128)," 
      +GinisTable.FIELD_CITY+" VARCHAR(128)," 
      +GinisTable.FIELD_STATE+" VARCHAR(64)," 
      +GinisTable.FIELD_COUNTRY+" VARCHAR(64)," 
      +GinisTable.FIELD_PHOTO_ID+" VARCHAR(64), " 
      +GinisTable.FIELD_PHOTO_ID_NUMBER+" VARCHAR(128), " 
      +GinisTable.FIELD_PHOTO_ID_PIC_PATH+" VARCHAR(128));"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

public int insertGini(GiniInfo giniInfo) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 

    contentValues.put(GinisTable.FIELD_NAME, giniInfo.getName()); 
    contentValues.put(GinisTable.FIELD_PROFESSION, giniInfo.getProfession()); 
    contentValues.put(GinisTable.FIELD_GINI_PIC_PATH, giniInfo.getPicPath()); 
    contentValues.put(GinisTable.FIELD_EXPERIENCE, giniInfo.getExperience()); 
    contentValues.put(GinisTable.FIELD_PRIMARY_MOBILE, giniInfo.getPrimaryMobile()); 
    contentValues.put(GinisTable.FIELD_ISD_CODE, giniInfo.getIsdCode()); 
    contentValues.put(GinisTable.FIELD_GENDER, giniInfo.getGender()); 
    contentValues.put(GinisTable.FIELD_DOB, giniInfo.getDob()); 
    contentValues.put(GinisTable.FIELD_LATITUDE, giniInfo.getLatitude()); 
    contentValues.put(GinisTable.FIELD_LONGITUDE, giniInfo.getLongitude()); 
    contentValues.put(GinisTable.FIELD_ADDRESS_LINE1, giniInfo.getAddressLine1()); 
    contentValues.put(GinisTable.FIELD_ADDRESS_LINE2, giniInfo.getAddressLine2()); 
    contentValues.put(GinisTable.FIELD_CITY, giniInfo.getCity()); 
    contentValues.put(GinisTable.FIELD_STATE, giniInfo.getState()); 
    contentValues.put(GinisTable.FIELD_COUNTRY, giniInfo.getCountry()); 
    contentValues.put(GinisTable.FIELD_PHOTO_ID, giniInfo.getPhotoID()); 
    contentValues.put(GinisTable.FIELD_PHOTO_ID_NUMBER, giniInfo.getPhotoIDNum()); 
    contentValues.put(GinisTable.FIELD_PHOTO_ID_PIC_PATH, giniInfo.getPhotoIDPicPath()); 

    int giniID = (int) db.insert(GinisTable.TABLE_GINI, null, contentValues); 
    return giniID; 
} 

public List<GiniInfo> getGiniInfoList() { 

    List<GiniInfo> giniInfoList = new ArrayList<GiniInfo>(); 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("select * from "+GinisTable.TABLE_GINI, null); 
    /*if(cursor != null && cursor.getCount() > 0) { 
     cursor.moveToFirst(); 

     while (cursor.isAfterLast() == false) { 

      GiniInfo giniInfo = new GiniInfo(); 
      giniInfo.setGiniID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GINI_ID)))); 
      giniInfo.setName(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_NAME))); 
      giniInfo.setProfession(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PROFESSION))); 
      giniInfo.setPicPath(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GINI_PIC_PATH))); 
      giniInfo.setExperience(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_EXPERIENCE)))); 
      giniInfo.setPrimaryMobile(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PRIMARY_MOBILE))); 
      giniInfo.setIsdCode(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ISD_CODE))); 
      giniInfo.setGender(Integer.parseInt(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_GENDER)))); 
      giniInfo.setDob(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_DOB))); 
      giniInfo.setLatitude(Double.parseDouble(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_LATITUDE)))); 
      giniInfo.setLongitude(Double.parseDouble(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_LONGITUDE)))); 
      giniInfo.setAddressLine1(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ADDRESS_LINE1))); 
      giniInfo.setAddressLine2(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_ADDRESS_LINE2))); 
      giniInfo.setCity(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_CITY))); 
      giniInfo.setState(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_STATE))); 
      giniInfo.setCountry(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_COUNTRY))); 
      giniInfo.setPhotoID(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID))); 
      giniInfo.setPhotoIDNum(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID_NUMBER))); 
      giniInfo.setPhotoIDPicPath(cursor.getString(cursor.getColumnIndex(GinisTable.FIELD_PHOTO_ID_PIC_PATH))); 

      giniInfoList.add(giniInfo); 
     } 
    }*/ 
    return giniInfoList; 
} 
} 

我的堆棧跟蹤是:

android.database.sqlite.SQLiteException: no such table: ginis (code 1): , while compiling: select * from ginis 
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893) 
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504) 
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1339) 
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1278) 
at com.mastishka.pocketgini.database.tables.GinisTable.getGiniInfoList(GinisTable.java:122) 
at com.mastishka.pocketgini.services.UploadGiniData.onHandleIntent(UploadGiniData.java:99) 
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.os.HandlerThread.run(HandlerThread.java:61) 

以上是異常堆棧跟蹤。

+0

'getReadableDatabase()'不返回空值。出錯時拋出異常。你是否用一個存根'return null'版本自己重寫了這個方法? – laalto

+0

不,我沒有重寫該方法。 –

+0

也發佈你的堆棧跟蹤。 – laalto

回答

0

將同一個數據庫文件中的所有表放到同一個sqlite助手中。幫助器用於版本控制數據庫文件,而不是表格

+0

是的,這就是我在問題的評論中寫的。很好,你解決了問題。但是,問題本身並不包含告訴問題所在的必要信息。投票結束,解決對別人沒有幫助。 – laalto

0

替換此

SQLiteDatabase db = this.getReadableDatabase(); 

通過這樣的:

SQLiteDatabase db = GinisTable.this.getReadableDatabase(); 

如果工作請標明正確

+0

根本不工作 –

0

嘗試關閉創建一個新的連接之前,現有的數據庫連接。

public int insertGini(GiniInfo giniInfo) { 

    SQLiteDatabase db = this.getWritableDatabase(); 
    // ............... 
    int giniID = (int) db.insert(GinisTable.TABLE_GINI, null, contentValues); 
    db.close(); //Close the database connection. 
    return giniID; 
} 

在某些設備中,此連接阻止創建新連接。

+0

無法正常工作。已經試過這個 –

+0

getReadableDatabase()永遠不會返回null,請發佈你的logcat,以便我們可以更好地幫助你。 – Kartheek

+0

NullPointerException不是因爲getReadableDatabase(),而是因爲數據庫中不存在表。創建表後,表名更改時可能會發生此錯誤,一旦卸載應用程序,然後再次安裝, – Kartheek

0

我有完全相同的問題。

SQLiteOpenHelper的構造函數不調用OnCreate中,這條線之後:

super(context, Config.DB_NAME, null, 1); 

你必須添加一行要麼getWritableDatabase()或getReadableDatabase(),它會調用的onCreate如果不存在。

+0

完成它但不工作 –

0

什麼是Config.DB_NAME?項目中是否有另一個電話?

super(context, Config.DB_NAME, null, 1); 

如果是這樣的話,數據庫被創建,所以該類中的onCreate將不會被調用,因此該表不存在。

解決方法是將所有表放在一起派生的SQLiteOpenHelper類。

0

1.也許在設備上的舊數據庫版本。完全從設備上刪除應用程序,並更改版本號super(context, DB_NAME, null, 2);末兩位數

2.檢查補丁與SDK版本兼容

if(android.os.Build.VERSION.SDK_INT >= 4.2){ 
    DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; 
} else { 
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
} 

到文件連接到數據庫之前設置錯了語言環境。 Like Locale.setDefault(Settings.ACTION_LOCALE_SETTINGS);

4.也許數據庫文件已被打開。

相關問題