在查询中跨越多行的SQL字符串值

51

更新: 生物信息可能包含撇号(请参见更新的示例)

我有一个 SQL 查询,其中一个值跨越多行,导致查询失败:

UPDATE User SET UserId=12345, Name="J Doe", Location="USA", Bio="I'm a
bio that has an apostrophe, and I'm 
spanning multiple lines!" 
WHERE UserId=12345
在 C# 中,你可以在字符串前面加上 @ 符号,比如 Bio=@"...",这样就可以让它跨越多行。但我不确定如何在 SQL 查询中实现相同的效果。你如何让一个字符串跨越多行而不必手动拼接字符串呢?
Bio="I'm a"
+" bio that has an apostrophe, and I'm"
+" spanning multiple lines!" 

你使用的是哪个数据库服务器? - John Hartsock
@JohnHartsock SQLite(也重新标记) - Kiril
那里面有实际的换行符吗?你尝试过执行匹配 "my bio\n spans\n multiple\n lines!" 的查询吗?还是查询字符串太长,这个做不到? - ZacAttack
ZacAttack,我还没有尝试将任何内容与个人简介进行匹配,我只是想更新一条记录,其中包含可能包含多行的个人简介。 - Kiril
4个回答

77

SQL Server允许以下语法(请注意使用单引号而不是双引号)

UPDATE User
SET UserId = 12345
   , Name = 'J Doe'
   , Location = 'USA'
   , Bio='my bio
spans 
multiple
lines!'
WHERE UserId = 12345

请注意,个人简介可能包含单引号(撇号),例如:“我是一个带有撇号的个人简介,因此不能用单引号括起来”。 - Kiril
2
"除非你转义引号或参数化查询"。如果我的个人简介以我最喜欢的引言结尾,例如:“嗨, 这是我的个人简介 这是我最喜欢的引言 “我们能体验到的最美丽的事物就是神秘的。” -阿尔伯特·爱因斯坦” - Code Magician
为什么这个能够工作,但原问题中的查询却不能?是因为这个使用单引号而不是双引号,还是其他原因? - Tanner Swett
1
@TannerSwett 是的,SQL Server 使用单引号而不是双引号。 - Adam Wenger
无法在此字符串中间添加SQL注释,因为它会被解释为字符串的一部分。 - Nate Anderson
显示剩余2条评论

3

"BIO"列的数据类型是什么?数据库服务器是SQL、Oracle还是MySQL?您可以根据列的数据类型限制(例如:varchar(200))跨越多行。尝试使用单引号,这可能会有所不同。以下内容对我有效:

update table set mycolumn = 'hello world,
my name is carlos.
goodbye.'
where id = 1;

此外,如果您在C#中将SQL字符串连接起来,则可能需要检查单引号。 如果变量包含单引号,则可能会使代码逸出SQL语句,从而无法显示您期望看到的所有行。
顺便说一下,您可以像在C#中一样使用分号来限定SQL语句,这只是一个提示。

生物数据类型是 VARCHAR,请注意,生物可能包含单引号,例如:“我是一个带有撇号的生物,所以不能用单引号括起来”。 - Kiril

-1

使用VARCHAR时,您可能还需要指定长度,或者通常最好这样做

那么抓取文本,将其制作成字符串,然后将其放入查询中怎么样?

String TableName = "ComplicatedTableNameHere";  
EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
String editTextString1 = editText1.getText().toString();  

崩溃了

String TableName = "ComplicatedTableNameHere";            
    //sets the table name as a string so you can refer to TableName instead of writing out your table name everytime

EditText editText1 = (EditText) findViewById(R.id.EditTextIDhere); 
    //gets the text from your edit text fieldfield 
    //editText1 = your edit text name
    //EditTextIDhere = the id of your text field

String editTextString1 = editText1.getText().toString();  
    //sets the edit text as a string
    //editText1 is the name of the Edit text from the (EditText) we defined above
    //editTextString1 = the string name you will refer to in future

然后使用

       /* Insert data to a Table*/
       myDB.execSQL("INSERT INTO "
         + TableName
         + " (Column_Name, Column_Name2, Column_Name3, Column_Name4)"
         + " VALUES ( "+EditTextString1+", 'Column_Value2','Column_Value3','Column_Value4');");

希望这能在某种程度上有所帮助... 注意:每个字符串都在

中。
'"+stringname+"'

是 'and' 使字符串的多行元素生效,没有它,你只能得到第一行,甚至不确定是否得到整行,可能只有第一个单词。


6
不要这样做,这是 SQL 注入漏洞的教科书式例子。 - Tamas Hegedus

-4

我更喜欢使用@符号,这样我可以将查询粘贴到查询文件中并完全看到它:

string name = "Joe";
string gender = "M";
string query = String.Format(@"
SELECT
   *
FROM
   tableA
WHERE
   Name = '{0}' AND
   Gender = '{1}'", name, gender);

对于长而复杂的查询来说,这真的非常棒。好的一点是它保留了制表符和换行符,因此将其粘贴到查询浏览器中可以保留良好的格式。


7
这看起来是针对C#而不是SQL的。 - grrizzly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接