0
import os
import sqlite3

g_rdb_path = 'mails.db'

def test():
    c = sqlite3.connect(g_rdb_path)
    t = c.cursor()
    t.execute('''SELECT m_mail_info.id
    FROM m_mail_info, json_each(m_mail_info.froms)
    WHERE json_each.value LIKE '%{0}%'
    UNION
    SELECT m_mail_info.id
    FROM m_mail_info, json_tree(m_mail_info.tos)
    WHERE json_tree.value LIKE '%{0}%' and json_tree.type ="text"
    UNION
    SELECT m_mail_info.id
    FROM m_mail_info, json_tree(m_mail_info.ccs)
    WHERE json_tree.value LIKE '%{0}%' and json_tree.type ="text"
    UNION
    SELECT m_mail_info.id
    FROM m_mail_info, json_tree(m_mail_info.bccs)
    WHERE json_tree.value LIKE '%{0}%' and json_tree.type ="text"
    union
    SELECT m_mail_info.id
    FROM m_mail_info
    WHERE m_mail_info.subject like '%{0}%' or m_mail_info.plainContent like '%{0}%'
    '''.format('name'))
    print([i for i in t.fetchall()])
    t.close()
    c.close()

if __name__ == '__main__':
    test()

in the m_mail_info table, column froms, tos, ccs, bccs are of TEXT type, in the format like '{"a":"123"}' or '[{"a":"123"}]'.

when I run the script with conda env:

python=3.8.10 32bit, windows it prints:

[(21,), (22,), (23,), (29,), (36,), (38,), (39,), (41,), (43,), (44,), (53,), (55,), (56,), (57,), (58,), (59,), (60,), (61,), (62,), (63,), (66,), (67,), (, (9072,), (73,), (78,), (81,), (115,), (120,), (129,), (158,), (162,), (163,), (164,), (167,), (168,), (171,), (173,), (186,), (190,), (768,), (779,), (818,), ( (108901,), (906,), (948,), (1034,), (1035,), (1036,), (1037,), (1041,), (1043,), (1050,), (1052,), (1053,), (1054,), (1055,), (1056,), (1060,), (1062,), (1071,), (11, (1076,), (1080,), (1082,), (1084,), (1086,), (1087,), (1089,), (1090,), (1093,), (1095,), (1096,), (1097,), (1098,), (1100,), (1102,), (1108,), (1109,), (1114,), (1115,), (1117,), (1119,), (1121,), (1122,), (1126,), (1129,), (1131,), (1133,), (1139,), (1147,), (1149,), (1152,), (1153,), (1154,), (1158,), (1160,), (1169,)]

whatever it is, it can print the result.

while if I run the script with the python env installed by python.org 3.8.10 32bit release, it prints Error:

Traceback (most recent call last):
  File "test.py", line 68, in <module>
    test()
  File "test.py", line 43, in test
    t.execute('''SELECT m_mail_info.id
sqlite3.OperationalError: no such table: json_tree

And I've tried with raw sql on SQLiteStudio, it returns the same result as in Conda env

enter image description here

I don't know how to find the difference between conda env and python official release env.

If you want to reproduce the issue, you may create a table with only one column, and in the query statement you must make use of json_each or json_tree .

Arthur
  • 63
  • 1
  • 8

1 Answers1

1

The json_each and json_tree functions are only included by default in SQLite from version 3.38 (2022-02-22).

Python 3.8 is much older, and because of that uses an older version that does not include them.

You can confirm it with:

import sqlite3

print(sqlite3.sqlite_version)

This other SO post suggests that it is possible to replace the sqlite3.dll module directly in the Python tree...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • it works. while conda env3.8 returns the version '3.38.3', while the official returns '3.35.5'. I copy the conda's sqlite3.dll to the official one. – Arthur May 06 '23 at 09:31