插入或SQLite中更新,并采用了Android的database.query();采用了、SQLite、Android、query

由网友(看清我。。)分享简介:有没有办法来改变我的功能:is there a way to change my function:public categorie createCategoria(String categoria) {ContentValues values = new ContentValues();values.put(My...

有没有办法来改变我的功能:

is there a way to change my function:

public categorie createCategoria(String categoria) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_NOME, categoria);
            values.put(MySQLiteHelper.COLUMN_PREF, 0);

            long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
                values);

            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            categorie newCategoria = cursorToCategorie(cursor);
            cursor.close();
            return newCategoria;
          } 

这是一个原始的插入,我想改变这种功能,使之更新或插入相应。我想改变这种becouse我已经使用在某些地方这个功能,但现在我需要选择是否插入行或更新(或忽略插入)连续用相同的 COLUMN_NOME 。有人可以帮助我这样做呢?

this is a raw insert, i would like to change this function to make it update or insert accordingly. i would like to change this becouse i'm already using this function in some places, but now i need to choose if insert a row or update (or ignoring the insert) a row with the same COLUMN_NOME. can someone help me doing this?

我的意思是我想插入新行仅如果没有另一个具有相同的名称(像往常一样,你知道的)。

i mean i would like to insert a new row ONLY if there isn't another with the same name (as usual you know).

推荐答案

您可以调用 INT nRowsEffected = database.update(...); ,如果没有行由任该行不存在更新生效(或你的大清洗更新()!)因此,你需要调用 database.insert(。 ..)。当然,如果nRowsEffected> 0,那么你就完成了。

you could call int nRowsEffected = database.update(...); if there are no rows effected by the update either the row doesn't exist (or you hosed your update()!) therefore you need to call database.insert(...). of course if nRowsEffected > 0 then you are done.

阅读全文

相关推荐

最新文章