PythonでSQL(CREATE/INSERT)を実行する方法(SQLite)です
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')")
Commitを実行します
conn.commit()
全体の流れ
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()
# Connection 切断
conn.close()