用Python开发MySQL脚本
来源:百度文库 编辑:神马文学网 时间:2024/04/28 13:25:19
复杂的数据库操作
下面通过一个脚本animal.py来演示如何解决复杂的数据库操作,这里给出完整的animal.py代码。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
#animal.py:创建动物表并从中检索信息
import sys
import MySQLdb
#连接到MySQL服务器
try:
conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
#创建动物表并填充内容
try:
cursor = conn.cursor ()
cursor.execute ("DROP TABLE IF EXISTS animal")
cursor.execute ("""
CREATE TABLE animal
(
name CHAR(40),
category CHAR(40)
)
""")
cursor.execute ("""
INSERT INTO animal (name, category)
VALUES
('snake', 'reptile'),
('frog', 'amphibian'),
('tuna', 'fish'),
('racoon', 'mammal')
""")
print "Number of rows inserted: %d" % cursor.rowcount
#使用fetchone()进行循环提取
cursor.execute ("SELECT name, category FROM animal")
while (1):
row = cursor.fetchone ()
if row == None:
break
print "%s, %s" % (row[0], row[1])
print "Number of rows returned: %d" % cursor.rowcount
#使用fetchall()进行循环提取
cursor.execute ("SELECT name, category FROM animal")
rows = cursor.fetchall ()
for row in rows:
print "%s, %s" % (row[0], row[1])
print "Number of rows returned: %d" % cursor.rowcount
#发出修改名称的语句,共两种方式,第一种是在语句字符串
#中使用数据值的字面值,第二种是使用位置标识符
cursor.execute ("""
UPDATE animal SET name = 'turtle'
WHERE name = 'snake'
""")
print "Number of rows updated: %d" % cursor.rowcount
cursor.execute ("""
UPDATE animal SET name = %s
WHERE name = %s
""", ("snake", "turtle"))
print "Number of rows updated: %d" % cursor.rowcount
#创建一个字典游标,这样就可以使用位置而非名称来访问数据列中的值了。
cursor.close ()
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
cursor.execute ("SELECT name, category FROM animal")
result_set = cursor.fetchall ()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])
print "Number of rows returned: %d" % cursor.rowcount
cursor.close ()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
conn.commit ()
conn.close ()
该脚本使用一个表来存储动物的名称和类别:
下面通过一个脚本animal.py来演示如何解决复杂的数据库操作,这里给出完整的animal.py代码。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
#animal.py:创建动物表并从中检索信息
import sys
import MySQLdb
#连接到MySQL服务器
try:
conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
#创建动物表并填充内容
try:
cursor = conn.cursor ()
cursor.execute ("DROP TABLE IF EXISTS animal")
cursor.execute ("""
CREATE TABLE animal
(
name CHAR(40),
category CHAR(40)
)
""")
cursor.execute ("""
INSERT INTO animal (name, category)
VALUES
('snake', 'reptile'),
('frog', 'amphibian'),
('tuna', 'fish'),
('racoon', 'mammal')
""")
print "Number of rows inserted: %d" % cursor.rowcount
#使用fetchone()进行循环提取
cursor.execute ("SELECT name, category FROM animal")
while (1):
row = cursor.fetchone ()
if row == None:
break
print "%s, %s" % (row[0], row[1])
print "Number of rows returned: %d" % cursor.rowcount
#使用fetchall()进行循环提取
cursor.execute ("SELECT name, category FROM animal")
rows = cursor.fetchall ()
for row in rows:
print "%s, %s" % (row[0], row[1])
print "Number of rows returned: %d" % cursor.rowcount
#发出修改名称的语句,共两种方式,第一种是在语句字符串
#中使用数据值的字面值,第二种是使用位置标识符
cursor.execute ("""
UPDATE animal SET name = 'turtle'
WHERE name = 'snake'
""")
print "Number of rows updated: %d" % cursor.rowcount
cursor.execute ("""
UPDATE animal SET name = %s
WHERE name = %s
""", ("snake", "turtle"))
print "Number of rows updated: %d" % cursor.rowcount
#创建一个字典游标,这样就可以使用位置而非名称来访问数据列中的值了。
cursor.close ()
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
cursor.execute ("SELECT name, category FROM animal")
result_set = cursor.fetchall ()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])
print "Number of rows returned: %d" % cursor.rowcount
cursor.close ()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
conn.commit ()
conn.close ()
该脚本使用一个表来存储动物的名称和类别:
用Python开发MySQL脚本
Python脚本如何保证游戏正常开发 - 51CTO.COM
Mysql自动备份脚本
mysql下如何执行sql脚本【转】 - 网络服务器软件开发 - C++博客
Python开发环境的搭建
每天备份 mysql 数据库的脚本
每天备份 mysql 数据库的脚本
开源空间 网络安全工具开发函数库Libnet -- Linux,C,C ,Java,Ajax,XML,perl,php,python,ruby,MySQL,Gnome,KDE,Qt,Gtk,bash,shell,嵌入式,网络,信息安全,操作系统,数据结构,编译原理
每天备份 mysql 数据库的脚本 - Unix爱好者家园
网站开发框架的选择,我们为什么选择Django/Python?|Django|Python|架构|
网站开发框架的选择,以及我们为什么选择Django/Python|架构|技术|Python|
Google将限制Python语言的应用 开发社区热议 - Python编程 - Java...
eclipse+jboss+mysql开发环境设置
网站开发日志: mysql命令行 - 删除表
Python 数据库开发 - 天生我材必有用,千金散尽还复来 - JavaEye技术网站
【mysql集群】mysql集群配置,本人已测试 - 其他数据库开发 / MySQL/Postgresql
用Python玩CreateWindow-1
Loadrunner脚本编程(2)-VuGen脚本文件的开发过程
开发基于Eclipse+Jboss+Mysql的EJB/J2EE应用系统
VertrigoServ -- 免费网络开发环境(可以舍弃Apache PHP MySQL...
mysql
用脚本自动检测硬件信息,再用脚本Excel输出
用脚本给文件夹加密
用C/C 扩展Python语言