Python sqlite3和单元测试
import os
import unittest # 包含单元测试模块
import sqlite3 as sqlite # 包含sqlite3模块
def get_db_path():
return "sqlite_testdb"
class TransactionTests(unittest.TestCase): # 单元测试第一步: 由TestCase派生类
def setUp(self): # 单元测试环境配置
try:
os.remove(get_db_path())
except:
pass
self.con1 = sqlite.connect(get_db_path(), timeout=0.1) # 连接数据库
self.cur1 = self.con1.cursor() # 获取游标
self.con2 = sqlite.connect(get_db_path(), timeout=0.1)
self.cur2 = self.con2.cursor()
def tearDown(self): # 单元测试环境清除
self.cur1.close() # 关闭游标
self.con1.close() # 关闭连接
self.cur2.close()
self.con2.close()
os.unlink(get_db_path())
def CheckDMLdoesAutoCommitBefore(self):
self.cur1.execute("create table test(i)") # 执行SQL查询
self.cur1.execute("insert into test(i) values (5)")
self.cur1.execute("create table test2(j)")
self.cur2.execute("select i from test")
res = self.cur2.fetchall()
self.failUnlessEqual(len(res), 1) # 测试
def CheckInsertStartsTransaction(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
self.cur2.execute("select i from test")
res = self.cur2.fetchall()
self.failUnlessEqual(len(res), 0)
def CheckUpdateStartsTransaction(self):
self.cur1.execute("create table test(i)")
self.cur1.execute("insert into test(i) values (5)")
self.con1.commit()
self.cur1.execute("update test set i=6")
self.cur2.execute("select i from test")
res = self.cur2.fetchone()[0]
self.failUnlessEqual(res, 5)
def CheckDeleteStartsTransaction(self):
self.cur1.exec
相关文档:
1. SQLite Database Browser 是一个SQLite数据库管理工具。是开源的、免费的。
Home Page
http://sqlitebrowser.sourceforge.net/
Download
http://sourceforge.net/project/showfiles.php?group_id=87946
Wiki
http://en.wikipedia.org/wiki/SQLite_Database_Browser
2.  ......
shhgs 发布了关于《 Py 2.5 what's new 之 yield
》
之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一个 generator
不可缺少的一条语句,只要包含它的函数即是一个 generator 。但在2.3中,generator
不能重入,不能在运行过程中修改,不能引发异常,你要么是顺序 ......
一个有趣的网站:
http://www.pythonchallenge.com/
集娱乐与学习于一体,在开动脑筋闯关的过程中,不但扩展了思维,还对Python加深了理解。
一共33关,每闯过一关都可以在提示下查看作者给出的Solution。
第0关(指导关):
出现一幅画面,上面写着2**38,教你如何进入下一关。
&nb ......
今天遇到一个一个问题,是将字符串类型的时间转化为UTC时间。例如time_str = "2009/11/09 12:23:23" 转化为UTC int型。
找了一些资料,发现time模块就能完成这个转换。
import time
time_str = "2009/11/09 12:23:23"
time_s = time.strptime(time_str,"%Y/%m/%d %H:%M:%S")
utc_f = time.mktime(time_s)
utc_i = int ......