私は自分がやろうとしていることに少し迷っています。Python 内で SQL を実行する方法を学習します。
Python sqlite に 3 つのテーブル 1、2、3 を持つデータベースがあります。
次に、.sql ファイルに記述した一連のいくつかのクエリがあります。
ループを作成しましたが、SQL ファイルを読み取って実行する手順がありません。
# For each of the 3 tables, query the database and print the contents
for table in ['1', '2', '3']:
# Plug in the name of the table into SELECT * query
result = c.execute("SELECT * FROM %s;" % table);
# Get all rows.
rows = result.fetchall();
# \n represents an end-of-line
print "\n--- TABLE ", table, "\n"
私の SQL クエリは、queries.sql という名前の別の .sql ファイルに記述されています。ループのどこに置くのですか?