[英]Obtaining the primary key identifier value for a record (SQL)
I have been working on a school project- an app.我一直在研究一个学校项目——一个应用程序。 I've been using an sql db for my database.
我一直在为我的数据库使用 sql db。 For one particular task, I wanted to use the customer's username (which is unique) to find the corresponding customer_id value (an auto increment integer primary key) for other purposes.
对于一项特定任务,我想使用客户的用户名(这是唯一的)来查找相应的 customer_id 值(一个自动递增的整数主键)以用于其他目的。 The code I used looked like this:
我使用的代码如下所示:
public int getCustomerid(String username){
SQLiteDatabase db = this.getWritableDatabase();
Cursor resultset = db.rawQuery("SELECT cust_code FROM Customer where username='" + username+ "'", null);
resultset.moveToFirst();
return resultset.getInt(0);
}
I have used this format of queries for obtaining other values like password, email id, etc. However, when I try to use this method to extract the primary key value, I get a null pointer exception error.我已经使用这种查询格式来获取密码、电子邮件 ID 等其他值。但是,当我尝试使用这种方法来提取主键值时,出现空指针异常错误。 Is there any way to solve this?
有没有办法解决这个问题?
Thanks,谢谢,
I do not know why this works:我不知道为什么会这样:
public int getCustomerid(String username){ public int getCustomerid(字符串用户名){
SQLiteDatabase db = this.getWritableDatabase();
String SQLquery = "SELECT cust_code FROM Customer where username='" + username+ "'"
Cursor resultset = db.rawQuery(SQLquery, null);
resultset.moveToFirst();
return resultset.getInt(0);
}
I now see how creating parameters adds more clarity.我现在看到创建参数如何增加清晰度。 Thank you very much Mr. Gordon Linoff.
非常感谢 Gordon Linoff 先生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.