我是否正确地将数据保存到我的sqlite数据库?

时间:2013-05-23 20:51:34

标签: android sql sqlite cursor

首先我有我的sql语句

private static final String DATABASE_CREATE_BADGES =
        "create table if not exists badges (id integer primary key autoincrement, "
        + "name VARCHAR, description VARCHAR, image_url VARCHAR, club_id VARCHAR, created_at VARCHAR);";

然后我确保创建它:

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try {
            Log.i("execSQL", "Creating badges");
            db.execSQL(DATABASE_CREATE_BADGES);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

接下来我有我的保存方法,其中我在调用方法时指定值。 Log,我显示了一个有效的字符串:

//---updates a badge record---
public boolean updateBadgesRecord(long rowId, String id, String name, String description, String image_url, String club_id, String created_at) 
{

    Log.i("image_url in updateBadgesRecord", image_url);
    ContentValues args = new ContentValues();
    if(id != null){
    args.put(KEY_BADGES_ID, id);
    }
    if(name != null){
    args.put(KEY_BADGES_NAME, name);
    }
    if(description != null){
    args.put(KEY_BADGES_DESCRIPTION, description);
    }
    if(image_url != null){
    args.put(KEY_BADGES_IMAGEURL, image_url);
    }
    if(club_id != null){
    args.put(KEY_BADGES_CLUBID, club_id);
    }
    if(created_at != null){
    args.put(KEY_BADGES_CREATEDAT, created_at);
    }

    return db.update(DATABASE_TABLE_BADGES, args, KEY_ROWID + "=" + rowId, null) > 0;
}

在调用这些方法之前,我打开数据库:

db = DBHelper.getWritableDatabase();

我认为它正在储蓄。这就是我试图获取数据的方式:

public Cursor getBadgeRecord(long rowId) throws SQLException 
{
    SQLiteDatabase db  = DBHelper.getReadableDatabase();
    Cursor mCursor =
            db.query(true, DATABASE_TABLE_BADGES, new String[] {KEY_ROWID,
            KEY_BADGES_ID, KEY_BADGES_NAME, KEY_BADGES_DESCRIPTION, KEY_BADGES_IMAGEURL, KEY_BADGES_CLUBID, KEY_BADGES_CREATEDAT}, 
            KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

这是我调用方法的地方,但是,prinln返回null:

public void activateBadgeList(){
    for(int i = 0; i < badgeButtons.length; i++){
        if(true){
        badgeButtons[i].setActivated(true);
        badgeButtons[i].setAlpha(1.0f);

        db.open();
        Cursor c = db.getBadgeRecord(i);
        String image_url = null;
        if (c.moveToFirst()){
               while(!c.isAfterLast()){
                  image_url = c.getString(c.getColumnIndex("image_url"));
                  // do what ever you want here
                  c.moveToNext();
               }
            }
            c.close();
        if(image_url != null){
        Log.i("image url", image_url);
        } else {
        Log.e("image url", "image url is null");    
        }
        badgeButtons[i] = UrlImageViewHelper.setUrlDrawable(badgeButtons[i], image_url);
        db.close();
        }
    }

所以我一定做错了什么......但它是什么?

1 个答案:

答案 0 :(得分:0)

这个问题的答案是:db.update(...)仅更新现有行。事先做db.insert(...)。