简体   繁体   English

如何在 SQLite 的 varchar 列中使用(空字符)select 行?

[英]How to select rows with \u0000 (null character) in a varchar column in SQLite?

Long story short, lots of bogus data entered the SQLite database over time, where there is valid data before a null character in a varchar column - and then bogus data afterwards.长话短说,随着时间的推移,大量虚假数据进入了 SQLite 数据库,其中在 varchar 列中的 null 字符之前有有效数据 - 然后是虚假数据。 I'm using to represent the null character here.我在这里用来表示 null 字符。

validData\u0000bogusData

I need to clean the dataset so that the data after null character (and the null character) is gone.我需要清理数据集,以便 null 字符(和 null 字符)之后的数据消失。 Unfortunately the data doesn't fit a pattern so only way is to look for the null character.不幸的是,数据不符合某种模式,所以唯一的方法是寻找 null 字符。

I've been trying to query the bad data so that I can clean it up.我一直在尝试查询坏数据,以便清理它。

SQL that I have tried:我试过的SQL:

SELECT id FROM table WHERE field LIKE "%" || CHAR(0) || "%"

Result: This returns all rows in the table.结果:这将返回表中的所有行。 I need just the rows with null character in them.我只需要其中包含 null 个字符的行。

String sql = "SELECT id FROM table WHERE field LIKE \"%\u0000%\"";

Result: SQLite stops processing SQL string when it encounters null char , Error: SQL syntax error near LIKE "%结果:SQLite遇到null char时停止处理SQL字符串,错误:SQL syntax error near LIKE "%

Any help is appreciated, all I could find was info on getting rows with NULL value.感谢任何帮助,我所能找到的只是有关获取具有 NULL 值的行的信息。

Use INSTR() to get the rows which contain CHAR(0) :使用INSTR()获取包含CHAR(0)的行:

SELECT * FROM tablename WHERE INSTR(field, CHAR(0));

and if you want you can update the table to remove the bogus data:如果你愿意,你可以更新表格以删除虚假数据:

UPDATE tablename
SET field = SUBSTR(field, 1, INSTR(field, CHAR(0)) - 1)
WHERE INSTR(field, CHAR(0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM