PythonでSQL(SELECT)を実行する方法(SQLite)です
レコードを取得するSQLを実行します
for row in c.execute('SELECT * FROM test_table ORDER BY id'):
print(row)
全体の流れ
import sqlite3
# Connection 接続
conn = sqlite3.connect('test.db')
# Cursor 取得
c = conn.cursor()
# SQL 実行 テーブル作成
c.execute('CREATE TABLE test_table(id integer, name text)')
# SQL 実行 レコード追加
c.execute("INSERT INTO test_table VALUES ('1','aaa')")
c.execute("INSERT INTO test_table VALUES ('2','bbb')")
# Commit 実行
conn.commit()
# SQL 実行 レコード取得
for row in c.execute('SELECT * FROM test_table ORDER BY id'):
print(row)
# Connection 切断
conn.close()