java 值中有空格的sql查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2742039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:37:39  来源:igfitidea点击:

sql query with space in the value

javasqlormopenjpa

提问by user234194

How do I write where clause where the value has spaces:

如何在值有空格的地方编写 where 子句:

Actually I am using openJPA and I have to set parameter. The value i will be setting has a space, eg: String somevalue="firstname lastname"; query.setparameter(somevalue);

实际上我正在使用 openJPA 并且我必须设置参数。我将设置的值有一个空格,例如:String somevalue="firstname lastname"; query.setparameter(somevalue);

I did this but doesnot work. Any suggestion will be appreciated. Thanks.

我这样做了,但不起作用。任何建议将不胜感激。谢谢。

回答by akf

That should work. The space within the quotes is valid. Perhaps there are spaces after the lastname, or the casing of lastname is incorrect.

那应该有效。引号内的空格有效。可能姓后有空格,或者姓的大小写不正确。

回答by Mark Rushakoff

That syntax does indeed work; you must be typing in a name that does not exist.

这种语法确实有效;您必须输入一个不存在的名称。

sqlite> CREATE TABLE FOO (name TEXT);
sqlite> INSERT INTO FOO VALUES('joe smith');
sqlite> SELECT * FROM FOO WHERE name = 'joe smith';
joe smith

If this is a one-off search, you might be better off trying the LIKEoperatorto find the match:

如果这是一次性搜索,您最好尝试使用LIKE运算符来查找匹配项:

sqlite> SELECT * FROM FOO WHERE name LIKE '%smith%';
joe smith

回答by jvenema

There's nothing wrong with that query.

该查询没有任何问题。

If it's not returning results, it's because you've got a problem with your data. Are you storing as 'char' or 'varchar'? If it's 'char', then you'll have extra padding characters in there to watch for.

如果它没有返回结果,那是因为您的数据有问题。您是存储为“char”还是“varchar”?如果它是'char',那么您将有额外的填充字符需要注意。