我在用sqlite做一个项目。
我刚才确实创建了我的数据库等等,但昨天我从手机上卸载了我的应用程序,并试图通过eclipse再次运行它,似乎从那一刻起,它就不再工作了。
首先是代码:
我的sqlhelper:

    public class SQLHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "listopresto";

public static final String TABLE_CATEGORIES = "categories";
public static final String CATEGORY_NAME = "name";
public static final String CATEGORY_ID = "id";
public static final String CATEGORY_ID_PARENT = "parent_id";
public static final String CATEGORY_URL_IMAGE = "image_url";

public static final String TABLE_SHOPPING_LIST = "shopping_list";
public static final String SHOPPING_LIST_ID = "shopping_list_id";
public static final String SHOPPING_LIST_NAME = "shopping_list_name";
public static final String SHOPPING_LIST_DATE_CREATION = "shopping_list_date_creation";

public static final String TABLE_SHOPPING_LIST_ITEMS = "shopping_list_items";
public static final String SHOPPING_LIST_ITEMS_LIST_ID = "shopping_list_items_list_id";
public static final String SHOPPING_LIST_ITEMS_ID = "shopping_list_items_id";
public static final String SHOPPING_LIST_ITEMS_NB_ITEMS = "shopping_list_items_nb_items";
public static final String SHOPPING_LIST_ITEMS_CHECKED = "shopping_list_items_checked";

public static final String TABLE_INFOS = "infos";
public static final String INFOS_AGE = "age";
public static final String INFOS_MAIL = "mail";
public static final String INFOS_DISPLAY_PRICE = "display_price";
public static final String INFOS_TOKEN = "token";
public static final String INFOS_REFRESH_TOKEN = "refresh_token";
public static final String INFOS_TOKEN_EXPIRATION = "token_expiration";
public static final String INFOS_REFRESH_TOKEN_EXPIRATION = "refresh_token_expiration";
public static final String INFOS_APP_VERSION = "app_version";

public static final String TABLE_ITEMS = "items";
public static final String ITEM_ID = "id";
public static final String ITEM_NAME = "name";
public static final String ITEM_CATEGORY_ID = "item_category_id";
public static final String ITEM_PRICE = "item_price";

public SQLHelper(Context context){
    super(context, DATABASE_NAME, null, 25);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE_CATEGORIES = "CREATE TABLE " + TABLE_CATEGORIES + "(" + CATEGORY_NAME + " TEXT," + CATEGORY_ID + " INTEGER, " + CATEGORY_ID_PARENT + " INTEGER," + CATEGORY_URL_IMAGE + " TEXT" + ")" ;
    String CREATE_TABLE_INFOS = "CREATE TABLE " + TABLE_INFOS + "(" + INFOS_AGE + " INTEGER," + INFOS_MAIL + " TEXT," + INFOS_DISPLAY_PRICE + " TEXT," + INFOS_TOKEN + " TEXT," + INFOS_REFRESH_TOKEN + " TEXT," + INFOS_TOKEN_EXPIRATION + " TEXT, " + INFOS_REFRESH_TOKEN_EXPIRATION + " TEXT, " + INFOS_APP_VERSION + " TEXT" + ")";
    String CREATE_TABLE_ITEMS = "CREATE TABLE " + TABLE_ITEMS + "(" + ITEM_ID + " INTEGER," + ITEM_NAME + " TEXT," + ITEM_CATEGORY_ID + " INTEGER," + ITEM_PRICE + " REAL" + ")";
    String CREATE_TABLE_SHOPPING_LIST = "CREATE TABLE " + TABLE_SHOPPING_LIST + "(" + SHOPPING_LIST_ID + " INTEGER," + SHOPPING_LIST_NAME + " TEXT," + SHOPPING_LIST_DATE_CREATION + " TEXT" + ")";
    String CREATE_TABLE_SHOPPING_LIST_ITEMS = "CREATE TABLE " + TABLE_SHOPPING_LIST_ITEMS + "(" + SHOPPING_LIST_ITEMS_LIST_ID + " INTEGER," + SHOPPING_LIST_ITEMS_ID + " INTEGER," + SHOPPING_LIST_ITEMS_NB_ITEMS + " INTEGER," + SHOPPING_LIST_ITEMS_CHECKED + " INTEGER" + ")";
    db.execSQL(CREATE_TABLE_CATEGORIES);
    db.execSQL(CREATE_TABLE_INFOS);
    db.execSQL(CREATE_TABLE_ITEMS);
    db.execSQL(CREATE_TABLE_SHOPPING_LIST);
    db.execSQL(CREATE_TABLE_SHOPPING_LIST_ITEMS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_INFOS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHOPPING_LIST);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHOPPING_LIST_ITEMS);
    onCreate(db);
}
/*
 *
 *  METHODES TABLE INFOS
 * z
 */

public void addInfos(InfosModel infos){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
     values.put(INFOS_AGE, infos.getAge());
     values.put(INFOS_MAIL, infos.getMail());
     values.put(INFOS_DISPLAY_PRICE, infos.getDisplayPrice());
     values.put(INFOS_TOKEN, infos.getToken());
     values.put(INFOS_REFRESH_TOKEN, infos.getRefreshToken());
     values.put(INFOS_TOKEN_EXPIRATION, infos.getTokenExpiration());
     values.put(INFOS_REFRESH_TOKEN_EXPIRATION, infos.getRefreshTokenExpiration());
     values.put(INFOS_APP_VERSION, infos.getAppVersion());
     db.insert(TABLE_INFOS, null, values);
     db.close();
}

public void updateInfos(InfosModel allInfos){
    String Query = "UPDATE " + TABLE_INFOS +
                         " SET " + INFOS_AGE + "=" + allInfos.getAge() +
                         ", " + INFOS_MAIL + "=\"" + allInfos.getMail() + "\""+
                         ", " + INFOS_DISPLAY_PRICE + "=\"" + allInfos.getDisplayPrice() + "\"" +
                         ", " + INFOS_TOKEN + "=\"" + allInfos.getToken() + "\"" +
                         ", " + INFOS_REFRESH_TOKEN + "=\"" + allInfos.getRefreshToken() + "\"" +
                         ", " + INFOS_TOKEN_EXPIRATION + "=\"" + allInfos.getTokenExpiration() + "\"" +
                         ", " + INFOS_REFRESH_TOKEN_EXPIRATION + "=\"" + allInfos.getRefreshTokenExpiration() + "\"" +
                         ", " + INFOS_APP_VERSION + "=\"" + allInfos.getAppVersion() + "\"";
    SQLiteDatabase db = this.getReadableDatabase();
    db.execSQL(Query);
}

public InfosModel getInfos(){
    String selectQuery = "SELECT * FROM " + TABLE_INFOS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor != null){

        if (cursor.moveToFirst()){
            InfosModel infos = new InfosModel();
            infos.setMail(cursor.getString(cursor.getColumnIndex(INFOS_MAIL)));
            infos.setAge(cursor.getInt(cursor.getColumnIndex(INFOS_AGE)));
            infos.setDisplayPrice(cursor.getString(cursor.getColumnIndex(INFOS_DISPLAY_PRICE)));
            infos.setToken(cursor.getString(cursor.getColumnIndex(INFOS_TOKEN)));
            infos.setRefreshToken(cursor.getString(cursor.getColumnIndex(INFOS_REFRESH_TOKEN)));
            infos.setTokenExpiration(cursor.getString(cursor.getColumnIndex(INFOS_TOKEN_EXPIRATION)));
            infos.setRefreshTokenExpiration(cursor.getString(cursor.getColumnIndex(INFOS_REFRESH_TOKEN_EXPIRATION)));
            infos.setAppVersion(cursor.getString(cursor.getColumnIndex(INFOS_APP_VERSION)));
            cursor.close();
            return (infos);
        }
    }
    return (null);
}


public int getInfosCount() {
    String countQuery = "SELECT  * FROM " + TABLE_INFOS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
 }
}

我使用getinfo函数的java活动:
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    SQLHelper sqlHelper = new SQLHelper(this);
    InfosModel infos = sqlHelper.getInfos();
    Log.i("infos", infos.getMail());
 }

我相信这个问题来自getInfos()函数。
我的游标不是空的,但是moveToFirst()函数失败了,但是我不知道为什么,也不知道该怎么做来修复这个问题,因为我以前从来没有遇到过这个问题,而且我的数据库工作得很好。
谢谢你的帮助:)
波图斯。
编辑:我试图查看infos表中有多少行,没有行,这就是moveToFirst()失败的原因,但如果表中有行,则行为完全相同。

最佳答案

根据Android documents
如果光标为空,此方法将返回false。
所以你不能有任何数据在那里,你应该适当地管理。检查是否有错别字等。

09-05 18:53