Python连接SQLite3数据库

在开发Python程序的过程中,可以使用apsw模块实现和SQLite数据库的连接和操作。apsw模块是一个第三方库,实现对SQLite的封装。开发者可以通过如下命令安装apsw。

pip install apsw

如图所示:
Python连接SQLite3数据库

下面的实例文件apsw01.py演示了使用apsw模块创建并操作SQLite数据库数据的过程。

import apsw
con=apsw.Connection(":memory:")
cur=con.cursor()
for row in cur.execute("create table foo(x,y,z);insert into foo values (?,?,?);"
                     "insert into foo values(?,?,?);select * from foo;drop table foo;"
                     "create table bar(x,y);insert into bar values(?,?);"
                     "insert into bar values(?,?);select * from bar;",
                     (1,2,3,4,5,6,7,8,9,10)):
                          print(row)

执行后会输出添加到SQLite数据库中的数据。

(1, 2, 3)
(4, 5, 6)
(7, 8)
(9, 10)

下面的实例文件apsw02.py演示了使用apsw模块在SQLite数据库中同时批处理上千条数据的过程。

import threading, apsw
import queue
import sys
class TestThr(threading.Thread):
  def __init__(self):
   threading.Thread.__init__(self)
   self.IQ = queue.Queue()
   self.OQ = queue.Queue()
  def run(self):
   try:
        print("*THREAD: Thread started")
        while self.IQ.empty(): pass
        self.IQ.get()
        print("*THREAD: <<< Prepare database")
        con = apsw.Connection('test.db')
        c = con.cursor()
        try:
             c.execute('create table a(a integer)')
             c.execute('end')
        except:
            pass
        c.execute('begin')
        c.execute('delete from a')
        c.execute('end')
        print("*THREAD: >>> Prepare database")
        self.OQ.put(1)
        while self.IQ.empty(): pass
        self.IQ.get()
        print("*THREAD: <<< Fillup 1000 values")
        c.execute('begin')
        print("*THREAD: Trans. started")
        for i in range(1000):
             c.execute('insert into a values(%d)' % i)
        print("*THREAD: >>> Fillup 1000 values")
        self.OQ.put(1)
        while self.IQ.empty(): pass
        self.IQ.get()
        c.execute('end')
        print("*THREAD: Trans. finished")
        self.OQ.put(1)
        while self.IQ.empty(): pass
        self.IQ.get()
        print("*THREAD: <<< Fillup 1000 values")
        c.execute('begin')
        print("Trans. started")
        for i in range(1000, 2000):
             c.execute('insert into a values(%d)' % i)
        print("*THREAD: >>> Fillup 1000 values")
        c.execute('end')
        print("*THREAD: Trans. finished")
        self.OQ.put(1)
        while self.IQ.empty(): pass
        self.IQ.get()
        print("*THREAD: Thread end")
        self.OQ.put(1)
   except:
        print(sys.exc_info())
        sys.exit()
con = apsw.Connection('test.db')
c = con.cursor()
t = TestThr()
t.IQ.put(1)
t.start()
while t.OQ.empty(): pass
t.OQ.get()
# c.execute('begin')
def ReadLastRec():
    rec = None
    for rec in c.execute('select * from a'): pass
    print("- MAIN: Read last record", rec)
ReadLastRec()
t.IQ.put(1)
while t.OQ.empty(): pass
t.OQ.get()
ReadLastRec()
t.IQ.put(1)
while t.OQ.empty(): pass
t.OQ.get()
ReadLastRec()
t.IQ.put(1)
while t.OQ.empty(): pass
t.OQ.get()
ReadLastRec()
t.IQ.put(1)
while t.OQ.empty(): pass
# c.execute('end')
print("\n- MAIN: Finished")

执行后会输出:

*THREAD: Thread started
*THREAD: <<< Prepare database
*THREAD: >>> Prepare database
- MAIN: Read last record None
*THREAD: <<< Fillup 1000 values
*THREAD: Trans. started
*THREAD: >>> Fillup 1000 values
- MAIN: Read last record None
*THREAD: Trans. finished
- MAIN: Read last record (999,)
*THREAD: <<< Fillup 1000 values
Trans. started
*THREAD: >>> Fillup 1000 values
*THREAD: Trans. finished
- MAIN: Read last record (1999,)
*THREAD: Thread end
- MAIN: Finished

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!