使用sqlite3的时候,遇到个很奇怪的问题,select某字段的时候,报错感觉像是该字段不存在,但是表中确实是有这个字段的。查询其他字段均没有问题,唯独group这个字段
该表是 kafka监控工具KafkaOffsetMonitor-assembly-0.2.0.jar创建的表,表结构如下
sqlite> .schema OFFSETS
CREATE TABLE "OFFSETS" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"group" VARCHAR(254) NOT NULL,"topic" VARCHAR(254) NOT NULL,"partition" INTEGER NOT NULL,"offset" BIGINT NOT NULL,"log_size" BIGINT NOT NULL,"owner" VARCHAR(254),"timestamp" BIGINT NOT NULL,"creation" BIGINT NOT NULL,"modified" BIGINT NOT NULL);
CREATE INDEX "idx_search" on "OFFSETS" ("group","topic");
CREATE INDEX "idx_time" on "OFFSETS" ("timestamp");
CREATE UNIQUE INDEX "idx_unique" on "OFFSETS" ("group","topic","partition","timestamp");
查询操作如下,select group字段报错:Error: near “group”: syntax error,但是select topic是没问题的。
sqlite> select * from OFFSETS limit 1;
id group topic partition offset log_size owner timestamp creation modified
---------- ----------------------- ----------------------- ---------- ---------- ---------- ---------- ------------- ------------- -------------
5092201 IM_APP_NOTICE_MSG_OLD_1 IM_APP_NOTICE_MSG_TOPIC 0 1021391 1032122 1483027807567 1471691588963 1471833538219
sqlite> select * from OFFSETS where group='IM_APP_NOTICE_MSG_OLD_1';
Error: near "group": syntax error
sqlite> select * from OFFSETS where topic='IM_APP_NOTICE_MSG_TOPIC' limit 1;
id group topic partition offset log_size owner timestamp creation modified
---------- ----------------------- ----------------------- ---------- ---------- ---------- ---------- ------------- ------------- -------------
5092201 IM_APP_NOTICE_MSG_OLD_1 IM_APP_NOTICE_MSG_TOPIC 0 1021391 1032122 1483027807567 1471691588963 1471833538219
sqlite> select group from OFFSETS limit 1;
Error: near "group": syntax error
sqlite> select topic from OFFSETS limit 1;
topic
-----------------------
IM_APP_NOTICE_MSG_TOPIC
搜索了半天,也没找到相关资料,但是无意在group字段上加引号后,居然不会报错了。╮(╯▽╰)╭
sqlite> select "topic" from OFFSETS limit 1;
topic
-----------------------
IM_APP_NOTICE_MSG_TOPIC
遇到第二个问题:
查询某字典时,字段值若太长,出来的结果会被截断。但是select * 查询出来的不会被截断。还好python操作的时候没有这个问题
sqlite> select DISTINCT "group" from OFFSETS ;
group
----------------------
IM_APP_NOTICE_MSG_OLD_
IM_APP_NOTICE_MSG_OLD_
>>> import sqlite3
>>> conn = sqlite3.connect('offsetapp.db')
>>> cur = conn.cursor()
>>> cur.execute('select DISTINCT "group" from OFFSETS ;')
<sqlite3.Cursor object at 0x7f508817c190>
>>> cur.fetchall()
[(u'BINSPECT_MSG_SERVICE_1',), (u'IM_APP_NOTICE_MSG_OLD_1',), (u'IM_APP_NOTICE_MSG_OLD_10',), (u'IM_APP_NOTICE_MSG_OLD_2',), (u'IM_APP_NOTICE_MSG_OLD_3',), ]