程式語言 - Python - v3.x - Use Sqlite3



參考資訊:
https://docs.python.org/3/library/sqlite3.html

main.py

import sqlite3

con = sqlite3.connect("main.db")
cur = con.cursor()

cur.execute("CREATE TABLE movie(title, year, score)")

cur.execute("""
    INSERT INTO movie VALUES
        ('M1', 1975, 8.2),
        ('M2', 1971, 7.5)
""")

cur.execute("SELECT * FROM movie")
r = cur.fetchall()

for x in r:
    print(x)

con.commit()
con.close()

執行

$ rm -rf main.db
$ python3 main.py
    ('M1', 1975, 8.2)
    ('M2', 1971, 7.5)