如何在SQLiteDatabase辨别一个变种或Android的2瓦尔参数? [英] How does the SQLiteDatabase to distinguish parameters for one var or two vars in android?

查看:117
本文介绍了如何在SQLiteDatabase辨别一个变种或Android的2瓦尔参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我:结果
在以下code时,参数2为变种numberType和参数5为变种的id

 光标光标= db.rawQuery(SELECT * FROM MYTABLE其中,numberType =和id =?,新的String [] {2,5});

在以下code,无论是参数6和参数4是用于变种标识。
我认为,code会导致错误,我认为新的String [] {6,4}必须在两个瓦尔匹配,对吧?

  db.delete(人,ID+=?,新的String [] {6,4});

我不能使用db.delete(...),以实现相同的功能,正如从test.db的删除其中id在('6','4') !吧?

<一个href=\"http://stackoverflow.com/questions/19723403/how-can-pass-two-parameters-to-rawquery-in-android\">How可以在android系统传递两个参数rawQuery?结果
<一href=\"http://stackoverflow.com/questions/19701049/how-can-i-create-a-$c$c-if-i-need-to-delete-some-record-using-in-parameter-in-an\">How我可以创建一个code,如果我需要使用在Android的参数来删除一些记录?


解决方案

  

我觉得code会导致错误,我认为新的String [] {6,4}必须
  匹配两个瓦尔吧?


简短的回答:的是。这件作品code将无法正常工作。

说明:

您要绑定两个参数 - 换句话说,你要替换两个占位符的 与bindArgs 1 ? (在你的案件数量6和4)。

在语句的参数数目要匹配的绑定到语句参数号 - 所以,如果你有三个参数查询(否则将引发异常):

 从用户选择*其中id =?和年龄=?

您必须创建 whereArgs 字符串数组相同数量的参数(项):

 的String [] = whereArgs {20345,18};


  

如何在SQLiteDatabase区分参数一个VAR或
  在Android的2瓦尔?


如上所述。对于你的情况正确的解决方案是:


  • 添加第二个参数 WHERE 条款

  • 从字符串数组中删除第二项

它可以是这样的:

  db.delete(人,ID =和引脚=?,新的String [] {6,4});
db.delete(人,ID =?,新的String [] {6});
db.delete(人,身份证件(?,)?,新的String [] {6,4});

结果
1 更换为同样喜欢经典的Java:在查询的第一个参数将与第一项阵列等来代替

Somebody told me:
In the following code, the parameter "2" is for var numberType and the parameter "5" is for var id

Cursor cursor = db.rawQuery("SELECT * FROM mytable where numberType=? and id=?", new String[] { "2","5" });

In the following code, both the parameter "6" and the parameter "4" are for var id. I think the code will cause error, I think new String[]{"6","4"} must match two vars, right?

db.delete("person", "id"+"=?", new String[]{"6","4"});

I can't use db.delete (...) to implement the same function just like delete from test.db where id in ('6','4')! right ?

How can pass two parameters to rawQuery in android?
How can I create a code if I need to delete some record using In parameter in android?

解决方案

I think the code will cause error, I think new String[]{"6","4"} must match two vars, right?

Short answer: Yes. This piece of code won't work.

Explanation:

You want to bind two parameters - in other words you want to replace two placeholders ? with bindArgs1 (in your case number 6 and 4).

Number of parameters in statement have to match number of parameters binded to statement - so if you have three parameters in query (otherwise Exception will be thrown):

select * from Users where id = ? and age = ?

You have to create whereArgs string-array with same number of parameters (items):

String[] whereArgs = {"20345", "18"};

How does the SQLiteDatabase to distinguish parameters for one var or two vars in android?

Explained above. Correct solutions for your case are:

  • add second parameter to WHERE clause
  • remove second item from string-array

It can looks like:

db.delete("person", "id = ? and pin = ?", new String[] {"6", "4"});
db.delete("person", "id = ?", new String[] {"6"});
db.delete("person", "id in(?, ?)", new String[] {"6", "4"});


1Replacing is same like in classic Java: first parameter in query will be replaced with first item in array etc.

这篇关于如何在SQLiteDatabase辨别一个变种或Android的2瓦尔参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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