【Python web 开发】 bottle 简单教程 (四)
来源:互联网 发布:中信档案管理系统软件 编辑:程序博客网 时间:2024/06/13 10:11
本来打算做一个简单的CRUD操作
但是发现Python对DB的操作还没有介绍,你懂的。
Python可以操作多种数据库,
仅针对sqlite做简单的操作,其他类同
一:下载sqlite,安装
http://www.sqlite.org/download.html
sqlite-shell-win32-x86-3071100.zip
(253.56 KiB)
下载它,然后解压缩,恭喜你,
sqlite搞定了。
解压缩后文件夹下只有一个文件sqlite3.exe,CMD下,cd到这个目录,执行sqlite3.exe test.db
恭喜你,数据库创建成功了。
你可以在cmd中各种命令搞定sqlite,嘿嘿,下面看看python怎么搞定它。
二:python搞定它
官方doc
http://docs.python.org/py3k/library/sqlite3.html
那么,要是使用sqlite3模块了,
To use the module, you must first create a Connection object that represents the database.
用这个模块,你必须首先建立一个链接来表示db
import sqlite3
You can also supply the special name :memory: to create a database in RAM.
你也可以把数据库建立在内存中,
官方doc写的很好,强烈推荐看看,所以下面的例子,都是很简单的,LZ发现实在没法比官方doc写更好,忍了,本来还想直接上官方doc的,但是发现不行。
官方doc是e文,本想翻一下,但是发现几乎不需要翻译,就能看懂。囧!!!
建表,删表,增加,查询写了,那么删除和更新还能不会搞吗?
import sqlite3#To use the module, you must first create a Connection object that represents the database. def createTable():# You can also supply the special name :memory: to create a database in RAM.# 采用特殊名称:memory:可以建在内存中 conn = sqlite3.connect('sqlite/example') print(conn,'建立成功') #cursor必须要有,否则,你啥也干不了 c = conn.cursor() # 建表 c.execute('create table test (id int, name text)') # 增加一行 c.execute("insert into test values (1,'jack')") # 提交 conn.commit() # 关闭 c.close() conn.close() #查询def find(): conn = sqlite3.connect('sqlite/example') c = conn.cursor() c.execute('select * from test') res = c.fetchall() for row in res: print(row) def dropTable(): conn = sqlite3.connect('sqlite/example') c = conn.cursor() c.execute('drop table test') conn.commit() c.close() conn.close() #要使用绑定变量的方法,不要使用字符串拼接的方式def findById(id): conn = sqlite3.connect('sqlite/example') c = conn.cursor() c.execute('select * from test where id = ? ',(id,)) print(c.fetchall()) #插入 def insert(): conn = sqlite3.connect('sqlite/example') c = conn.cursor() c.execute("insert into test values (2,'tom')") conn.commit() c.close() conn.close() #批量插入,使用变量绑定def insertLarge(): conn = sqlite3.connect('sqlite/example') c = conn.cursor() for r in [(3, 'lily'),(4, 'cati')]: c.execute("insert into test values (?,?)",r) conn.commit() c.close() conn.close() #dropTable()#createTable()#insert()#findById(2)#insertLarge()find()
- 【Python web 开发】 bottle 简单教程 (四)
- 【Python web 开发】 bottle 简单教程 (一)
- 【Python web 开发】 bottle 简单教程 (二)
- 【Python web 开发】 bottle 简单教程 (三)
- [深度分析] Python Web 开发框架 Bottle
- Python web frame:Bottle
- 【Python】Python用Bottle轻量级框架进行Web开发
- Python使用Bottle来提供一个简单的web服务
- python bottle 搭建最简单的 web服务器
- [深度分析] Python Web 开发框架 Bottle(作者 @Sunng)
- Python用Bottle轻量级框架进行Web开发
- 微型 Python Web 框架: Bottle
- 微型 Python Web 框架: Bottle
- Python --- Bottle: 轻量级Web Server
- 微型 Python Web 框架: Bottle
- 【Python Web Framework】Bottle框架
- 微型 Python Web 框架: Bottle
- python web开发 之 flask教程(四)
- 手工编写动态链接库
- c/c++ 文件操作小练
- 很囧的翻译
- 查询某个html中的某个个字符串的个数
- unity3D网络入门教程
- 【Python web 开发】 bottle 简单教程 (四)
- Eclipse上GIT插件使用手册之一_安装EGIT插件
- 使用 Oracle GoldenGate 进行实时数据集成
- 安装tomcat native library提升Tomcat的吞吐性能
- Ubuntu下配置php环境。即安装xampp。
- 与数据库连接地址进行处理中文乱码
- Oracle Golden Gate - Install Oracle GoldenGate 11.2.1.0 on Linux
- LINUX系统下同时安装多个TOMCAT
- handler实现线程间通信的原理-looper