LoginSignup
2
3

More than 5 years have passed since last update.

めざせpythonライブラリマスター (32)sql

Posted at

【ライブラリ説明】

 pythonでsql使えます

【準備】

 (sqlite3が必要なのかな?)

【プログラム】

sql.py
# -*- coding: utf-8 -*-

import sqlite3
import sql

connection = sqlite3.connect(':memory:')
bliss = sql.SQL(connection)

bliss.run("CREATE TABLE contributors (firstname VARCHAR, lastname VARCHAR)")
bliss.run("INSERT INTO contributors VALUES (?, ?)", [('Andrew', 'Kuchling'),
                                                     ('James', 'Henstridge'),
                                                     ('Daniele', 'Varrazzo'),
                                                     ('Marc-Andre', 'Lemburg')])

print bliss.one("SELECT firstname FROM contributors WHERE lastname='Lemburg'")
# Marc-Andre

print bliss.all("SELECT firstname FROM contributors")
# [u'Andrew', u'James', u'Daniele', u'Marc-Andre']

print bliss.all("SELECT firstname, LENGTH(lastname) AS length FROM contributors")
'''
 [Record(firstname=u'Andrew', length=8),
  Record(firstname=u'James', length=10),
  Record(firstname=u'Daniele', length=8),
  Record(firstname=u'Marc-Andre', length=7)]
'''

【参考サイト】

 pypi
 github

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3