読者です 読者をやめる 読者になる 読者になる

データベースの初歩

import sqlite3
import os

dbpath = 'hoge.db'

if not os.path.exists(dbpath):
  with sqlite3.connect(dbpath) as conn:
    conn.execute('CREATE TABLE hoge (id INTEGER PRIMARY KEY, language, author)')

with sqlite3.connect(dbpath) as conn:
  conn.executemany(
    'INSERT INTO hoge (language, author) VALUES (?, ?)',
    [('perl', 'larry'), ('python', 'guido'), ('ruby', 'matz')]
    )
  conn.execute('DELETE FROM hoge WHERE id == 2')
  cur = conn.execute('SELECT * FROM hoge')
  for x in cur:
    print x
$ python hoge.py
(1, u'perl', u'larry')
(3, u'ruby', u'matz')