简体   繁体   English

如何在 SQLite3 中使用执行而不是执行脚本?

[英]How do I use execute, rather than execute script in SQLite3?

In the following code, scheduling.sql is executing properly, while dummy.sql is not.在下面的代码中,scheduling.sql 正常执行,而 dummy.sql 没有。 I'm not getting an error message.我没有收到错误消息。 It just doesn't appear to be doing anything.它似乎没有做任何事情。 For dummy.sql, I prefer to use execute, rather than executescript, so I can pass it some parameters.对于dummy.sql,我更喜欢使用execute,而不是executescript,所以我可以给它传递一些参数。 What am I doing wrong here?我在这里做错了什么?

db = sqlite3.connect(':memory:')
cursor = db.cursor()

with open('scheduling.sql', 'r') as sql_file:
    sql_script = sql_file.read()
cursor.executescript(sql_script)

with open('dummy.sql', 'r') as sql_file:
    sql_script = sql_file.read()
fetch=cursor.fetchone()
while fetch:
    cursor.execute(fetch)
    fetch=cursor.fetchone()

It never executes dummy.sql .它从不执行dummy.sql What is fetch after fetch=cursor.fetchone() executes ?什么是fetchfetch=cursor.fetchone()执行? Guess is something falsey.猜测是假的。 fetch... comes after execute.... . fetch...execute....之后出现execute....

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

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