pythonからsqliteに接続する

import sqlite3

dbpath = "test.sqlite"
conn = sqlite3.connect(dbpath)

cur = conn.cursor()
cur.executescript("""
DROP TABLE IF EXISTS items;

CREATE TABLE items(
	item_id INTEGER PRIMARY KEY,
	name TEXT UNIQUE,
	price INTEGER
);
INSERT INTO items(name, price)VALUES('Apple', 800);
INSERT INTO items(name, price)VALUES('Orange', 780);
INSERT INTO items(name, price)VALUES('Banana', 430);
""")

conn.commit()

cur = conn.cursor()
cur.execute("SELECT item_id, name, price FROM items")
item_list = cur.fetchall()
for it in item_list:
	print(it)

[vagrant@localhost python]$ python3 app.py
(1, ‘Apple’, 800)
(2, ‘Orange’, 780)
(3, ‘Banana’, 430)

import sqlite3

filepath = “test2.sqlite”
conn = sqlite3.connect(filepath)

cur = conn.cursor()
cur.execute(“DROP TABLE IF EXISTS items”)
cur.execute(“”” CREATE TABLE items(
item_id INTEGER PRIMARY KEY,
name TEXT,
price INTEGER)”””)
conn.commit()

cur = conn.cursor()
cur.execute(
“INSERT INTO items (name,price) VALUES (?,?)”,
(“Orange”, 520))
conn.commit()

cur = conn.cursor()
data = [(“Mango”, 770), (“Kiwi”,400), (“Grape”,800),(“Peach”,940),(“Persimmon”,700),(“Banana”, 400)]
cur.executemany(
“INSERT INTO items(name,price) VALUES(?,?)”, data)
conn.commit()

cur = conn.cursor()
price_range = (400, 700)
cur.execute(
“SELECT * FROM items WHERE price>=? AND price<=?", price_range) fr_list = cur.fetchall() for fr in fr_list: print(fr) [/python] [vagrant@localhost python]$ python3 app.py (1, 'Orange', 520) (3, 'Kiwi', 400) (6, 'Persimmon', 700) (7, 'Banana', 400)