SQLite strings with NUL

守給你的承諾、 提交于 2020-12-05 11:09:22

问题


  1. Can strings in SQLite 3 include NUL characters?
  2. If the answer to 1 is "yes", how can they be written in SQL queries? SQLite doesn't seem to have chr or char functions.

回答1:


In general, no - SQLite internally is not 8-bit clean, probably due to its Tcl heritage. While NULs do not cause corruption problems, SQLite typically stops processing strings at the first embedded NUL character.

This is true even for operators such as GLOB. For instance, you cannot match a BLOB column with GLOB when you have embedded NUL characters, e.g. this

select * from table where blobcol glob x'00022a';

will only match empty blob values: While you can use literal BLOB syntax (i.e. x'hexdigits') and use the resulting values where strings are used, SQLite typically only uses the part before the first NUL.

At least,. this is the state of affairs up to including SQLite 3.26.0.

Note that SQLite also has a BLOB type which can store embedded NULs without any issues, but there is very little functionality in SQLite to use them, and they are often harder to use from SQL interface libraries.

They also silently convert to strings in many contexts, at which point the embedded NULs start causing issues again.




回答2:


Not sure from which version onwards it is supported, but you can do it:

create table foo (bar data);
insert into foo(bar) values (x'001122334400ff');
select length(bar),hex(bar),bar from foo;


来源:https://stackoverflow.com/questions/4049348/sqlite-strings-with-nul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!