SQLite3 연습.

python 대화형 셸에서 SQLite3 DB에 데이터를 밀어넣어보는 예제. DATETIME(‘NOW’)를 확인해보려고 씀.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sqlite3 as lite
>>> import time
>>> conn = lite.connect('test.db')
>>> cs = conn.cursor()
>>> cs.execute("CREATE TABLE t1 (id INTEGER PRIMARY_KEY AUTO_INCREMENT NOT_NULL, name VARCHAR(255), at DATETIME)")
<sqlite3.Cursor object at 0x01FD03A0>
>>> conn.commit()
>>> chars = "abcdefghijklmnopqrstuvwxyz"
>>> for a in range(len(chars)):
    cs.execute("INSERT INTO t1 VALUES(?,?,DATETIME('NOW'));",(a, chars[a]))
    time.sleep(1)

<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
<sqlite3.Cursor object at 0x01FD03A0>
>>> conn.commit()
>>> cs.execute("SELECT * FROM t1")
<sqlite3.Cursor object at 0x01FD03A0>
>>> a = cs.fetchone()
>>> a
(0, u'a', u'2013-02-25 10:29:12')
>>> cs.execute("SELECT * FROM t1 WHERE at >= '2013-02-25 10:29:18' AND at <= '2013-02-25 10:29:25'")
<sqlite3.Cursor object at 0x01FD03A0>
>>> la = cs.fetchall()
>>> for item in la:
    print item

(6, u'g', u'2013-02-25 10:29:18')
(7, u'h', u'2013-02-25 10:29:19')
(8, u'i', u'2013-02-25 10:29:20')
(9, u'j', u'2013-02-25 10:29:21')
(10, u'k', u'2013-02-25 10:29:22')
(11, u'l', u'2013-02-25 10:29:23')
(12, u'm', u'2013-02-25 10:29:24')
(13, u'n', u'2013-02-25 10:29:25')
>>> cs.close()
>>> conn.close()
>>>