Android的SQL:检查记录在数据库存在 [英] Android SQL: Check if Record in Database Exists

查看:149
本文介绍了Android的SQL:检查记录在数据库存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于一个特定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:检查记录在数据库存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆