这是一段包含SQLite查询的Python3代码。
查询在SQLite浏览器中运行没有问题,但是从Python运行查询时出错:
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS Counts')
cur.execute('CREATE TABLE Counts (org TEXT, count INTEGER)')
cur.execute('INSERT INTO Counts (org,count) SELECT SUBSTR(email, (SELECT INSTR(email,'@'))) AS org, SUM(count) as count FROM Em GROUP BY org ORDER BY count DESC')
rows = cur.fetchall()
for row in rows:
print(row)
错误是:
(p)中的TypeError追溯(最近一次通话最近)5 cur.execute('CREATE TABLE Counts(org TEXT,count INTEGER)')----> 6 cur.execute('INSERT INTO Counts(org,count )SELECT SUBSTR(email,(SELECT INSTR(email,'@')))AS org,SUM(count)as count FROM Em GROUP BY org ORDER BY count DESC')7 8行= cur.fetchall()
TypeError:@:'str'和'str'不受支持的操作数类型
我找到了原因,但是仍然有问题...它不是INSERT的 在插入的开始和结尾处应将“替换为”。“ INSERT ... DESC”
cur.execute(“ INSERT INTO Counts(org,count)SELECT SUBSTR(email,(SELECT INSTR(email,'@'))))AS org,SUM(count)as count FROM Em GROUP BY org ORDER BY count DESC')。
但是它不执行INSERT,有什么想法吗?