我想基于一个特定ID来查询我的数据库。
字符串SQL =SELECT * FROM捣烂WHERE recipe_id ='+编号+'; 光标数据= database.rawQuery(SQL,NULL);如果这是第一次的活动已经运行,该表将随着id列存在,但不会有与特定ID的记录。我如何检查是否该特定记录是否存在,如果它不增加吗?我发现很多到关于检查,如果一个特定的列存在,但没有任何有关检查,如果一个特定的记录存在。
到目前为止,我已经尝试获取ID列索引和检查,看它是否返回-1,但它实际上是返回1由于某种原因。我可以使用if语句来验证ID列尚未建立?
解决方案 如果(cursor.moveToFirst()){ //记录存在 } 其他 { //没有找到记录 }I am trying to query my database based on a specific id.
String sql = "SELECT * FROM mash WHERE recipe_id = '" + id + "'"; Cursor data = database.rawQuery(sql, null);If this is the first time the activity has been run, the table WILL exist along with the id column, but there will not be a record with the specific id. How can I check if that specific record exists and if it does not, add it? I have found lots of into regarding checking if a specific column exists, but nothing about checking if a specific record exists.
So far I have tried getting the id column index and checking to see if it returns -1, but it actually is returning 1 for some reason. What can I use in an if statement to verify that the id column has not yet been created?
解决方案 if (cursor.moveToFirst()) { // record exists } else { // record not found }
更多推荐
Android的SQL:检查记录在数据库存在
发布评论