![](/img/trans.png)
[英]Python: writes only one row into SQLite DB despite loading the full file data into pandas framework
[英]Python inserts only One row on a SQLite DB
为什么我的代码只插入一行?
thewholeenchilada = ("SELECT SUBSTR(email, (SELECT INSTR(email,'@'))) AS org, SUM(count) as count FROM Em GROUP BY org ORDER BY count DESC")
for salida in cur.execute(thewholeenchilada):
cur.execute('''INSERT INTO Counts (org, count)
VALUES (?, ?)''', (salida[0],row[1]))
print((str(salida[0]), salida[1]))
conn.commit()
避免循环并运行一个INSERT INTO ... SELECT
查询。 现在,您在外部和内部循环中重复使用相同的光标,从而导致处理问题。 使用两个不同的游标或有效地组合并让数据库引擎运行动作查询:
sql = '''INSERT INTO Counts (org, [count])
SELECT SUBSTR(email, INSTR(email, '@')+1) AS org,
SUM(count) as [count]
FROM Em
GROUP BY org
ORDER BY count DESC
'''
cur.execute(sql)
conn.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.