调用getReadableDatabase时获取SQLiteException

时间:2015-05-26 06:47:16

标签: android android-sqlite

上面的代码是意向服务的一部分,我正在访问GinisTable类,它实际上是一个DBHelper类。

 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)

以上是异常堆栈跟踪。

6 个答案:

答案 0 :(得分:0)

替换此

 SQLiteDatabase db = this.getReadableDatabase();

由此:

 SQLiteDatabase db = GinisTable.this.getReadableDatabase();

如果工作请标记正确的

答案 1 :(得分: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;
}

在某些设备中,此连接会阻止创建新连接。

答案 2 :(得分:0)

我遇到了完全相同的问题。

SQLiteOpenHelper的构造函数不会调用OnCreate。在此行之后:

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

你必须添加一行getWritableDatabase()或getReadableDatabase(),如果不存在,将调用onCreate。

答案 3 :(得分:0)

将同一数据库文件中的所有表放入同一个sqlite帮助器中。帮助程序用于版本化数据库文件,而不是表

答案 4 :(得分:0)

什么是Config.DB_NAME?在项目的其他地方还有另一个电话吗?

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

如果是这种情况,则创建数据库,以便不会调用此类中的onCreate,因此该表不存在。

解决方案是将所有表放在SQLiteOpenHelper的一个派生类中。

答案 5 :(得分: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/";
}

3. 在连接到DataBase之前,设置了错误的语言环境。与Locale.setDefault(Settings.ACTION_LOCALE_SETTINGS);

一样

4. 或许以前打开过数据库文件。