Python DB sqlite3
来源:互联网 发布:西安研究所待遇 知乎 编辑:程序博客网 时间:2024/05/16 08:10
SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.-------摘自 官方文档
1.简单的建表
#!/usr/bin/python3
# databases.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Group, LLC
import sqlite3
def main():
db=sqlite3.connect('text.db')
db.execute('drop table if exists test')
db.execute('create table test (t1 text,i1 int)')
db.execute('insert into test (t1,i1) values(?,?)',('one',1))
db.execute('insert into test (t1,i1) values(?,?)',('tow',2))
db.execute('insert into test (t1,i1) values(?,?)',('three',3))
db.execute('insert into test (t1,i1) values(?,?)',('four',4))
db.commit()
cursor=db.execute('select * from test order by t1')
for row in cursor:
print(row)
if __name__ == "__main__": main()
('four', 4)
('one', 1)
('three', 3)
('tow', 2)
2.返回row对象 用row_factory
#!/usr/bin/python3
# databases.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Group, LLC
import sqlite3
def main():
db=sqlite3.connect('text.db')
db.row_factory=sqlite3.Row #row factory 来决定行是怎么返回的
db.execute('drop table if exists test')
db.execute('create table test (t1 text,i1 int)')
db.execute('insert into test (t1,i1) values(?,?)',('one',1))
db.execute('insert into test (t1,i1) values(?,?)',('tow',2))
db.execute('insert into test (t1,i1) values(?,?)',('three',3))
db.execute('insert into test (t1,i1) values(?,?)',('four',4))
db.commit()
cursor=db.execute('select * from test order by t1')
for row in cursor:
#print(dict(row)) #已字典的形式输出返回的行 dict()构造方法
print(row['t1'],row['i1'])
if __name__ == "__main__": main()
- Python DB sqlite3
- [db]SQLITE3 --详解
- 13.13 sqlite3 -- DB-API 2.0 SQLite数据库接口[Python参考库翻译]
- sqlite3 -- DB-API 2.0 SQLite数据库接口[Python参考库翻译
- sqlite3 -- DB-API 2.0 SQLite数据库接口[Python参考库翻译] (转)
- 使用Sqlite3操作db文件
- python sqlite3
- python-sqlite3
- python sqlite3
- python sqlite3
- pb9 通过ole db连接sqlite3数据库
- pb9 通过ole db连接sqlite3数据库
- python与sqlite3分析
- Python sqlite3和单元测试
- Python 之SQLite3
- python 使用sqlite3
- python sqlite3 报错
- python 远程操作Sqlite3
- 工程结算与Timelogs在Microsoft CRM亮点
- 古典加密算法的C实现
- 对照黑莓开发者成功的关键因素
- 一次ORA-28000: the account is locked用户锁定的问题排查
- 医疗时鲜资讯:医疗行业未来的变革(续前篇)
- Python DB sqlite3
- 对于网站设计和开发公司日益增长的需求
- 富士通ScanSnap的S1500文档扫描仪 - 最佳的方式去无纸化
- 定义结果作为有效的领导
- 大平原集成示例进口的MS CRM小时,GP服务发票
- 大多数下载WordPress的图片画廊插件
- 关于 aix 6103 升级到 aix6109 的种中不顺
- STL vector<bool>中的flip方法(3)
- Diary 2014.8.19&2014.8.20