android sqlite游标数0?

时间:2017-02-14 01:58:02

标签: android sqlite

您好我是使用SQLite的新手。当我在getData()方法中使用原始查询创建游标时,它表示当我希望它为1时,光标的计数为0.当我存储信息表时,程序运行良好,并且达到最佳状态我的知识存储正确。关于为什么我的计数为0的任何信息将不胜感激。

public class DBHandler extends SQLiteOpenHelper {

private static final int DATA_BASE_VERSION = 1;
private static final String DATABASE_LINK_NAME = "appData.db";

public static final String TABLE_APP_DATA = "table_data";
public static final String COLUMN_ID = "col_id";
public static final String COLUMN_PERSON1 = "col_person1";
public static final String COLUMN_PERSON2 = "col_person2";
public static final String COLUMN_DS= "col_ds";
public static final String COLUMN_GE= "col_ge";

String QUERY_DATA = "CREATE TABLE " + TABLE_APP_DATA + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY, " +
        COLUMN_PERSON1 + " TEXT, " +
        COLUMN_PERSON2 + " TEXT, " +
        COLUMN_DS + " TEXT, " +
        COLUMN_GE + " TEXT " +
        ");";

public DBHandler(Context context) {
    super(context, DATABASE_LINK_NAME, null, DATA_BASE_VERSION);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_APP_DATA);
    onCreate(db);
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL(QUERY_DATA);
}

//resets the data base
public void reset(){
    onUpgrade(getReadableDatabase(),0,0);
}

//saves data application
public void saveData(){
    Data data = Data.getInstance();

    //clear tables
    reset();

    //save app data
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_ID, 0);
    values.put(COLUMN_PERSON1,data.getPerson1());
    values.put(COLUMN_PERSON2,data.getPerson2());
    values.put(COLUMN_DS,String.valueOf(data.doesSex()));
    values.put(COLUMN_GE,String.valueOf(data.doesGameExists()));
    db.insert(TABLE_APP_DATA, null, values);
    db.close();
    System.out.println("Successfully added app data");

}

//load app data
public void loadData(){
    SQLiteDatabase db = getWritableDatabase();

    Data.getInstance().updataDataFile(getData(db));

    db.close();
}

public Data getData(SQLiteDatabase db){
    String query = "SELECT * FROM " + TABLE_APP_DATA + " WHERE 1";

    //cursor points to location
    Cursor c = db.rawQuery(query,null);
    c.moveToFirst();

    if(c.getCount() <= 0)
        System.out.println("The Cursor count is 0!");

    String p1 = c.getString(c.getColumnIndex(COLUMN_PERSON1));
    String p2 = c.getString(c.getColumnIndex(COLUMN_PERSON2));
    boolean doesSex = Boolean.valueOf(c.getString(c.getColumnIndex(COLUMN_DS)));
    boolean gameExists = Boolean.valueOf(c.getString(c.getColumnIndex(COLUMN_GE)));

    return new Data(p1,p2,doesSex,gameExists);
}

}

0 个答案:

没有答案