Python2.5ã§SQLite3ã使ã£ã¦ã¿ãã
SQLiteã¨ã¯
Python2.5ã«ã¯ãæ¨æºã§SQLiteã¨ãã軽éãã¼ã¿ãã¼ã¹ãå«ã¾ãã¦ããã
SQLiteã¯ãJAVAã§ããã¨ããã®HSQLDBãApache Derbyã®ãããªæãã®ããã§ããã
SQLLiteã¯ãã¼ã¢ã³ããµã¼ãã¹ã§ã¯ãªããã¹ã¿ã³ãã¢ãã³ã§ã®ã¿åä½ãããï¼DerbyãHSQLDBã¯çµã¿è¾¼ã¿ã¢ã¼ãã¨ãµã¼ãã¼ã¢ã¼ãã®ä¸¡æ¹ããã¤ããï¼
ã¾ãããã¼ã¿ãã¼ã¹ã®ã¹ãã¢ããã£ã¹ã¯ä¸ã®åä¸ãã¡ã¤ã«ã«ãªãã
Windowsã§ããã¨ããã®ãACCESS95ã2000ã§ä½¿ããã¦ãããJETãã¼ã¿ãã¼ã¹ã¨ã³ã¸ã³ã®MDBãã¡ã¤ã«ã®ãããªæãã§ããããã
è¦ããã«åãªãã©ã¤ãã©ãªã§ããã
ã©ã¤ã»ã³ã¹ã¯ããªãã¨ãããªãã¯ãã¡ã¤ã³ï¼èä½æ¨©æ¾æ£ï¼ã§ããã
ãã®ãããPythonã«éãããC/C++ãPerlãPHPãªã©ã«åºãçµã¿è¾¼ã¾ãã¦ããã
Python2.4ã¾ã§ã¯ã¢ããªã³ã¨ãã¦å¥éã¤ã³ã¹ãã¼ã«ããå¿
è¦ããã£ããããããPython2.5ããã¯æ¨æºè£
åã¨ãªã£ã¦ããã
æ¨æºã©ã¤ãã©ãªã¨ãã¦ä½¿ãããã¨ã¯ã大ããªæå³ãããã¨æãã
Python2.5ã®å®è¡ç°å¢ããã»ããã¢ããããã°ããã®ã§ããã¼ã¿ãã¼ã¹ã使ãã¹ã¯ãªãããä½ããã¨ããå©ç¨ãããã¨ããé
å¸ãããã¨ããã©ãã大å¹
ã«æ·å±
ãããããã¨ééããªãã
ï¼JAVAãXMLãã¼ãµããã³ãã«ãã¦ãããããããã«ãDerbyãããããã³ãã«ãã¦ãããã°ããã®ã«ãï¼J2EEã®SDKã«ã¯ãã³ãã«ããã¦ããããï¼ï¼
ããã§ããã£ãã試ãã¦ã¿ãã
Python2.5ã§ã¯ãsqlite3ã¨ããã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ãããã ãã§æºåå®äºã§ããã
åºæ¬çãªä½¿ãæ¹(DMLã¨ãã©ã³ã¶ã¯ã·ã§ã³å¦ç)
SQLiteã¯ãDerbyãHSQLDBã¨åæ§ã«ãã¤ã³ã¡ã¢ãªã»ãã¼ã¿ãã¼ã¹ãã¨ãã¦åä½ãããã¨ãã§ããã
æ®çºæ§ã®ãã³ãã¯ã·ã§ã³ãéããã¨ãã¼ã¿ãæ¶ãã¦ãã¾ããã®ã ãããã¹ãããã°ã©ã ããã¹ã¿ã³ãã¢ãã³ã¢ããªã±ã¼ã·ã§ã³ã®ä¸ã§ä¸æãã¼ã¿ã®éè¨ãªã©ãè¡ãå ´åã«ã¯ä¾¿å©ã ã¨æãããã
ãã®ãã¤ã³ã¡ã¢ãªã»ãã¼ã¿ãã¼ã¹ãã§ãåºæ¬çãªä½¿ãæ¹ãå®é¨ãã¦ã¿ãã
ç§èªèº«ãPython使ãã§ã¯ãªãã®ã§ç¡é§ï¼ãããããã°ééããï¼ãããããããªããã
使ã£ã¦ã¿ãæ触ã§ã¯ãæå¤ã¨ç°¡åã¨ããããAPIã¯ããåºæ¥ã¦ããã
#!/usr/bin/env python # -*- coding: utf-8 -*- # Python2.5以éã®ã¿ # 13.13 sqlite3 -- DB-API 2.0 interface for SQLite databases ããã³ã # http://www.python.org/dev/peps/pep-0249/ # ãåç §ã®ãã¨ã # SQLiteã®å©ç¨ import sqlite3 # ãã¼ãã«å 容ã®ä¸è¦§è¡¨ç¤º def dump(conn): cur = conn.cursor() try: cur.execute("select idx, val from testtbl order by idx") for row in cur: # ã«ã¼ã½ã«ã¯ãfetchone,fetchallãªã©ã§åå¾ã§ããã»ãã # ããèªèº«ãã¬ã³ã¼ãã®ãªã¹ãã¨ãã¦åæå¯è½ã§ããã print row finally: cur.close() # ã¤ã³ã¡ã¢ãªãã¼ã¿ãã¼ã¹ã¨ãã¦ã³ãã¯ã·ã§ã³ãä½æããã conn = sqlite3.connect(":memory:") # æå®ããªãã°ããã¯ãæåã®SQLæå®è¡ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãããï¼beginã¯å¿ è¦ãªããï¼ # Autocommitã«ããã°ããã¯ãisolation_levelãNoneã«ããã try: # DDLãå®è¡ããã # executescript()ã¯ã»ãã³ãã³ã§åºåãè¤æ°ã®SQLæãæµãè¾¼ããã # (ã³ãã¯ã·ã§ã³ã»ãªãã¸ã§ã¯ãã«å®ç¾©ããã¦ããexecuteã¡ã½ããã¯ãä¸æã«ã¼ã½ã«ãä½æã㦠# å®è¡ãããã³ã³ããã¨ã³ã¹ã¡ã½ããã§ãããï¼ # SQLite3ã¯ãInteger/Real/Text/BLOB(Binary Large Object)ã®ã¿ãµãã¼ãã # ãã以å¤ã®åã¯ãé¡æ¨ãã¦ãããããã«å²ãå½ã¦ãããã conn.executescript("""create table testtbl( idx integer primary key, val varchar2(512));""") # ãã«ã¼ã½ã«ãã¯èªã¿åãã«éãããSQLã®å®è¡ã®ãã¹ã¦ãè¡ãã # ï¼ãã©ã³ã¶ã¯ã·ã§ã³ã¯ãã³ãã¯ã·ã§ã³ã§å¶å¾¡ãããï¼ cur = conn.cursor() finished = False try: for n in range(1, 100): # ãï¼ãã¯ãã¤ã³ãå¤æ°ã§ãå¼æ°ããã¦ã¿ãã«ãªã©ã®åæå¯è½ãªãªãã¸ã§ã¯ãã渡ãã # ï¼ã¿ãã«ã®ã¿ãã«ã渡ãã¨ãè¤æ°è¡ã®æä½ã«ãªããï¼ # SQLite3ã¯ãINTEGER PRIMARY KEYãã®ã«ã©ã ã空ã«ããã¨èªåã§å²å½ã¦ããï¼ROWIDï¼ # http://www.sqlite.org/faq.html#q1 cur.execute("insert into testtbl(val) values(?)", (str(n),)) finished = True conn.commit() except: if not finished: # commitã«å¤±æããå ´åã¯ããã©ã³ã¶ã¯ã·ã§ã³ã¯ãã¼ã«ããã¯ããã¦ç¡å¹ã«ãªã£ã¦ããï¼ã¯ãï¼ conn.rollback() raise finally: cur.close() # å 容ã®ç¢ºèª dump(conn) except Exception, ex: print ex finally: conn.close() # # ã¤ã³ã¡ã¢ãªãã¼ã¿ãã¼ã¹ãããä¸åº¦éãã¦ã¿ãã(ãã¼ãã«ãæªå®ç¾©ã¨è¨ããã ï¼ æ¶ãã¦ããã) conn2 = sqlite3.connect(":memory:") try: dump(conn2) except Exception, ex: print ex finally: conn2.close()
ãã«ãã¹ã¬ããã§ã®ãã¼ã¿ãã¼ã¹ã¸ã®æ¸ãè¾¼ã¿ã¢ã¯ã»ã¹
次ã«ãå®éã«ãã¡ã¤ã«ã«æ¸ãåºããªãããã«ãã¹ã¬ããã§ã¢ã¯ã»ã¹ã試ãã¦ã¿ãã
SQLiteã¯ã¹ã¬ããã»ã¼ãã§ããããconnectåä½ã§ç¬ç«ãããå¿
è¦ãããã¨ã®ãã¨ã
ã¤ã¾ãããã¼ã¿ãã¼ã¹ãã¡ã¤ã«ã«ã¯è¤æ°ã¹ã¬ããããã¢ã¯ã»ã¹ãã¦ããããããã®ã³ãã¯ã·ã§ã³ã¯å
±æãã¦ã¯ãªããªããã¨ãããã¨ã
ã¾ããã³ãã¯ã·ã§ã³ã»ãªãã¸ã§ã¯ããã®ãã®ããã©ã³ã¶ã¯ã·ã§ã³ã管çãã¦ããããã§ããã
ãã®ãããã¯JDBCãåæ§ã§ãããããã¨ãã«é©ãã¯ãªãã
#!/usr/bin/env python # -*- coding: utf-8 -*- # Python2.5以éã®ã¿ import threading import sqlite3 # ãã¼ã¿ãã¼ã¹ãã¡ã¤ã«å # SQLiteã®ãã¼ã¿ã¹ãã¢ã¯åä¸ã®ãã¡ã¤ã«ã§ããã DBNAME = "threadingtest.db" # ãã¼ã¿ãã¼ã¹ã®æºå def init_db(): conn = sqlite3.connect(DBNAME) try: # ãã¼ãã«ãä½æãã conn.executescript("""create table testtbl( idx integer primary key, tname varchar(128), val integer);""") except Exception, ex: # ä½æã«å¤±æããããä½ææ¸ã¿ã¨æ³å®ãã¦ãæ¢åãã¼ã¿ãåé¤ãã conn.execute("delete from testtbl") conn.commit() finally: conn.close() # ã³ãã¯ã·ã§ã³ãæä½ããã¹ã¬ãã class MyThread(threading.Thread): def run(self): # SQLiteã¯ãã³ãã¯ã·ã§ã³åä½ã§ã¹ã¬ããã»ã¼ãã§ããã # ã¤ã¾ããã³ãã¯ã·ã§ã³ãã«ã¼ã½ã«ãã¹ã¬ãããã¾ããã§ä½¿ããã¨ã¯åºæ¥ãªãã # ããã¯ã®ã¿ã¤ã ã¢ã¦ãã¯ã60ç§ã«è¨å®ã conn = sqlite3.connect(DBNAME, timeout=60000) # 1件ã¥ã¤æé»ã§ã³ããããããï¼Autocommitï¼ conn.isolation_level = None try: cur = conn.cursor() try: for n in range(1, 10): cur.execute( "insert into testtbl(tname, val) values(?, ?)", (self.getName(), n)) finally: cur.close() except Exception, ex: # ã¿ã¤ã ã¢ã¦ãæéã¾ã§ã«ããã¯ãç²å¾ã§ããªãã£ãå ´åã¯ä¾å¤ãçºçããã print "thread=%s /exception=%s" % (self.getName(), str(ex)) finally: conn.close() # ã¡ã¤ã³ def main(): try: init_db() # ã¹ã¬ãããä½æãã threads = [] for n in range(1, 10): t = MyThread() t.setName("MyThread:%d" % n) threads.append(t) # ã¹ã¬ãããéå§ãã for t in threads: t.start() # ãã¹ã¦ã®ã¹ã¬ããã®çµäºãå¾ æ©ãã for t in threads: t.join() print "done." except Exception, ex: print ex # æ¸ãè¾¼ã¿çµæã®è¡¨ç¤º def verify_test(): conn = sqlite3.connect(DBNAME) try: cur = conn.cursor() try: cur.execute("select count(*) from testtbl") cnt = cur.fetchone()[0] print "count=%d" % cnt cur.execute("""select tname, count(val) from testtbl group by tname order by tname""") for row in cur: print row finally: cur.close() except Exception, ex: print ex finally: conn.close() # å®è¡ main() verify_test()
å®éã«è©¦ããæ触ã§ã¯ãã©ãããããã©ã³ã¶ã¯ã·ã§ã³ãéå§ãããã¨ã¸ã£ã¼ãã«ãã¡ã¤ã«ãä½æãããã³ãããããã¨ã¸ã£ã¼ãã«ãã¡ã¤ã«ããã¼ã¿ãã¼ã¹ãã¡ã¤ã«ã«ãã¼ã¸ããããã¨åé¤ããããã®ã¸ã£ã¼ãã«ãåå¨ããæéã¯ãå¥ã®ã¹ã¬ããã¯ãããã¯ããã¦ãã¾ããããªã®ã ã
SERIALIZEã¬ãã«ã®åé¢ã¬ãã«ãã¨ãããã¨ï¼
ãããããã¯ãã¼ãã«ããã¯ã©ãããããã¼ã¿ãã¼ã¹å
¨ä½ãããã¯ãã¦ããããã«è¦ããã
ã¾ããããã¯è¦ãæ触ã§ãã£ã¦ããã ã®æ¨è«ã ãã
çµè«
SQLiteã¯ç°¡åã«ä½¿ããããã«ç°¡åã«å£ããããããã¨ã¯ãªãããã ãã
å®æã¨ãã¦ããã«ãã¹ã¬ããã§ã®æ¸ãè¾¼ã¿ãé »çºããç¶æ³ã§ã¯ãããªãé
ãã¨èãã¦ããããã§ããã
ãããã主è¦ãªç¨éãã¯ã©ã¤ã¢ã³ããµã¤ãã®ã¹ã¿ã³ãã¢ãã³ãªã¢ããªã±ã¼ã·ã§ã³ã®ãããã³ã°ã¹ãã¢ã¨ãã¦å©ç¨ããåã«ã¯ããªãã®åé¡ããªãã ããã
ï¼BLOBã®ãµã¤ãºããã¼ã¿ãã¼ã¹ãã¡ã¤ã«ã®ä¸éããããªãã§ãããã¾ãã使ãåããã¨ã¯ãªãã ãããï¼
ãã®ããã§ãå
±æãã¦ãå£ããªãããã¡ã¤ã«ã³ãã¼ã§æã¡éã¹ãæ軽ãã¯é
åçã§ããã
ä»å¾ãPythonã§ããã°ã©ã ãæ¸ãã¨ãã¯ãããã使ãé¸æè¢ãè¦ãã¦ããã¹ãã ããã
C/C++ãã¤ã³ãã§ããããWindowsçã¯DLLä¸åã§åä½ãããããã
ããã«ãmsvcrt.dllã¨ãããVCã®ã©ã³ã¿ã¤ã ã©ã¤ãã©ãªä»¥å¤ã«ä¾åããªããã¨ããããã¾ã£ããæ軽ãªãã®ã§ããã
ãã²ãC/C++ã§SQLiteã使ãæ¹æ³ãç¿å¾ãã¦ã¿ããã¨æããããã
ããã¯ãã¾ã¡ãããªãåªãããã¼ã¿ãã¼ã¹ã®1ã¤ã§ãã£ã¦ãè¦ãã¦æã¯ãªãã ããã