POP Article V2.0 设为主页
收藏本站
首 页交流论坛留 言
您现在的位置:首 页 >> Python 与 XML >> Python开发 >> 查看文章
用python访问sqlite
作者:Zebin的专栏  来源:www.sqlite.com.cn  时间:2007-12-17  【 字体: 】 〖 双击滚屏 〗
1.首先去www.sqlite.org下载一个sqlite,它是一个嵌入式数据库,没有服务器的概念,windows版的就是一个exe,自己把它放到一个合适的目录里,然后把这个目录加入系统的path变量.
2.然后去找个pysqlite,这是python访问sqlite的接口,地址在这里 : http://initd.org/tracker/pysqlite
目前针对不同的python版本,pysqlite有两个版本:2.3和2.4,请根据自己的python版本选用.
3.然后就可以打开自己喜欢的编辑器,写一段测试代码了.
4.中文处理要注意的是sqlite默认以utf-8编码存储.
5.另外要注意sqlite仅支持文件锁,换句话说,它对并发的处理并不好,不推荐在网络环境使用,适合单机环境.
import pysqlite2.dbapi2 as sqlite
    
def runTest():
    cx 
= sqlite.connect('test.db')
    cu 
= cx.cursor()
    
    
#create
    cu.execute('''create table catalog(
        id integer primary key,
        pid integer,
        name varchar(10) unique
        )
''')

    
#insert
    cu.execute('insert into catalog values(0,0,"张小山")')
    cu.execute(
'insert into catalog values(1,0,"hello")')
    cx.commit()
    
    
#select
    cu.execute('select * from catalog')
    
print '1:',
    
print cu.rowcount
    rs 
= cu.fetchmany(1)
    
print '2:',
    
print rs
    rs 
= cu.fetchall()
    
print '3:',
    
print rs
    
    
#delete
    cu.execute('delete from catalog where id = 1 ')
    cx.commit()
    
    
    cu.execute(
'select * from catalog')
    rs 
= cu.fetchall()
    
print '4:',
    
print rs
    
    
#select count
    cu.execute("select count(*) from catalog")
    rs 
= cu.fetchone()
    
print '5:',
    
print rs
    
    
    cu.execute(
"select * from catalog")
    cu.execute(
'drop table catalog')

if __name__ == '__main__':
    runTest()


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=987675

浏览次数:   【 打 印 】【 关 闭
上一篇:Oracle Berkeley DB 指南(2)
下一篇:Python sqlite3和单元测试
 论坛登陆
用户名:
密  码:
验证码: 
Cookie 选项:
正常登陆 隐身登陆
   
没有注册?
 文章搜索
 推荐文章
 酷站推荐
 热门文章
 网站统计
关于我们 | 网站地图 | 联系我们 | 网站历史 | 友情链接 | TOP
Copyright© 2006 Sqlite中文社区  程序开发: mistletoe  站长: 林轩 陈文成