Home > Database > Mysql Tutorial > body text

Windows下安装MySQLdb, Python操作MySQL数据库的增删改查_MySQL

WBOY
Release: 2016-06-01 13:13:13
Original
1085 people have browsed it

python

这里的前提是windows上已经安装了MySQL数据库,且配置完毕,能正常建表能操作。在此基础上只需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了,只有1M多。这个有点类似jdbc里的那个jar包。

下载链接:http://sourceforge.net/projects/mysql-python/,

 百度云盘 :http://pan.baidu.com/s/1dDgnfpR密码:7bna

接着import MySQLdb就能使用了,下面给出测试代码:

#coding=utf-8#python操作MySQL数据库测试代码 import time, MySQLdb, sysprint "HelloWorld"#连接conn=MySQLdb.connect(host="localhost", user="root", passwd="yanzi", db="mydb", charset="utf8") cursor = conn.cursor()print "连接成功"# #增# sql = "insert into userinfo (username, pswd) values (%s, %s)"# param = ("哈哈", "ha11")# n = cursor.execute(sql, param)# print n# conn.commit()# #更新# sql = "update userinfo set pswd = %s where username = %s"# param = ("999999999999", "张三")# cursor.execute(sql, param)#删sql = "delete from userinfo where username = %s"param = ("张三")n = cursor.execute(sql, param)print nconn.commit()#查sql = "select * from userinfo "n = cursor.execute(sql)for rows in cursor.fetchall():	for cols in rows:		print cols,print ""#关闭指针对象和连接cursor.close()conn.close()
Copy after login

整体的操作跟Jdbc里很类似,传进去一个sql和params就ok了。在操作上引入了cursor的概念,在SQlite数据库里也是Cursor负责操作,都一个意思。注意,在删除和增加后必须执行conn.commit()才有效,否则操作无效。但在Jdbc里却没这回事。关闭数据库时记得释放cursor和conn.

MySQLdb在线文档   相关链接1链接2链接3

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!