在sql commed中,我要在条件运算符和变量一起使用的条件下,现在我必须在此变量中传递值,对此正确的语法是什么:
select col1 from testtable where col2 in :testval
mysql版本5.1
答案 0 :(得分:1)
不能。参数化SQL仅适用于单个值。使用这个:
public class AuthActivity extends AppCompatActivity implements OnMessageListener {
private SmsInterceptor smsInterceptor;
private EditText etAuthPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth);
initViews();
initSmsInterceptor();
}
@Override
protected void onResume() {
super.onResume();
smsInterceptor.register();
}
@Override
protected void onPause() {
super.onPause();
smsInterceptor.unregister();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
smsInterceptor.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void messageReceived(String message) {
// You can perform your validation here
etAuthPassword.setText(message);
}
private void initViews() {
etAuthPassword = findViewById(R.id.etAuthPassword);
etAuthPassword.addTextChangedListener(new SmsTextWatcher() {
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 4) {
btnAuthPassword.setEnabled(true);
checkCode();
} else {
btnAuthPassword.setEnabled(false);
}
}
});
}
private void initSmsInterceptor() {
smsInterceptor = new SmsInterceptor(this, this);
// Not necessary
smsInterceptor.setRegex(SMS_CODE_REGEX);
smsInterceptor.setPhoneNumber(PHONE_NUMBER);
}
private void checkCode() {
// Validation
if (isValid) {
navigateToMainScreen();
}
}
}
然后可以将values = ["foo", "bar", "baz"]
variables = { "var%d" % i: value for i, value in enumerate(values) }
placeholders = ", ".join(":%s" % var for var in variables.keys())
sql = "select col1 from testtable where col2 in (" + placeholders + ")"
与sql
绑定一起使用。
答案 1 :(得分:1)
您可以使用ORM库进行更高级别的访问,而不必使用普通的SQL。
例如(取自https://stackoverflow.com/a/8603129/196206):
session.query(MyUserClass).filter(MyUserClass.id.in_((123,456))).all()
# alternative - without the ORM part, just query builder:
session.execute(
select(
[MyUserTable.c.id, MyUserTable.c.name],
MyUserTable.c.id.in_((123, 456))
)
).fetchall()
答案 2 :(得分:0)
应该像这样:从测试表中选择col1,其中col2在(var1,var2,...)中
样品
表名称:测试
ID名称Roll_No
Mysql Query:从Test where Roll_No IN(25,35,45)中选择名称;
结果将是:
Ashish Rao
约瑟夫
穆罕默德