当cursor.movetofirst为false时该怎么办(SQLite)

时间:2014-02-17 14:28:14

标签: java android sqlite cursor

我正在使用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()函数。

我的光标不是null但函数moveToFirst()失败但我不知道为什么我不知道该怎么做才能解决这个问题因为我之前从未遇到过这个问题而且我的数据库运行良好..

谢谢你的帮助:) bottus。

编辑:我试着看看我在infos表中有多少行,有零行,这就是为什么movetofirst()失败我相信但是如果我在表中有一行,行为完全相同。

1 个答案:

答案 0 :(得分:4)

根据Android documents

  

如果光标为空,此方法将返回false。

所以你不能在那里有任何数据,你应该妥善管理。检查拼写错误等。