使用database.query()在SQlite和Android中插入或更新;
发布时间:2020-12-12 19:05:23 所属栏目:百科 来源:网络整理
导读:有没有办法改变我的功能: public categorie createCategoria(String categoria) { ContentValues values = new ContentValues(); values.put(MySQLiteHelper.COLUMN_NOME,categoria); values.put(MySQLiteHelper.COLUMN_PREF,0); long insertId = database.
有没有办法改变我的功能:
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); cursor.moveToFirst(); categorie newCategoria = cursorToCategorie(cursor); cursor.close(); return newCategoria; } 这是一个原始插入,我想更改此功能,使其更新或插入相应.我想改变这个因为我已经在某些地方使用了这个功能,但现在我需要选择是否插入一行或更新(或忽略插入)一行具有相同的COLUMN_NOME.有人可以帮我这么做吗? 我的意思是我只想插入一个新行,如果没有另一个具有相同的名称(通常你知道). 你可以调用int nRowsEffected = database.update(…);如果没有受更新影响的行,则该行不存在(或者您需要更新update()!)因此您需要调用database.insert(…).当然,如果nRowsEffected> 0然后你就完成了.(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |