Python 2.7 学习笔记 访问mysql数据库

时间:2023-03-09 16:20:50
Python 2.7 学习笔记 访问mysql数据库

一、基本概念

使用python操作数据库,其基本的流程如下(其实所有开发语言访问数据库的流程都是这样)。

1、第一,引入相应数据库的python数据库接口模块,针对不同的数据库类型,有不同的数据库访问接口模块。可以理解这些接口模块提供了一些api接口,让python代码可以访问数据库。

2、获取数据库的连接

3、执行sql语句

4、关闭数据库连接

二、下载python数据库接口包

比如对应mysql数据库,首先要下载相应的python mysql包。

可以到 https://pypi.python.org/pypi/MySQL-python/1.2.5 这个链接下下载,

对于windows系统,会有exe安装包,安装后,会在 Python27\Lib\site-packages 目录下生成mysql的包

三、代码编写

表结构如下: create table test(id int,info varchar(100))

#coding=utf-8
import time
import MySQLdb
try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('insert into test values(0,"x0")')
conn.commit()
cur.close()
conn.close()
print "finish insert direct"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('insert into test values(%s,%s)',(1,"x1"))
cur.execute('insert into test values(%s,%s)',[2,"x2"])
conn.commit()
cur.close()
conn.close()
print "finish insert by para"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
data=[];
data.append((3,"x3"));
data.append((4,"x4"));
cur.executemany('insert into test values(%s,%s)',data)
data=[];
data.append([5,"x5"]);
data.append([6,"x6"]);
cur.executemany('insert into test values(%s,%s)',data)
conn.commit()
cur.close()
conn.close()
print "finish muti insert by para"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('select * from test where id=%s',('')) #数字参数?
res=cur.fetchall()
for re in res:
print "id=%s,info=%s" % (re[0],re[1])
cur.close()
conn.close()
print "finish query where"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('select * from test')
res=cur.fetchall()
for re in res:
print "id=%s,info=%s" % (re[0],re[1])
cur.close()
conn.close()
print "finish query fetchall"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('select * from test') #游标指向第一条记录
re = cur.fetchone() #获取当前游标记录,同时游标指向下一条记录
print "id=%s,info=%s" % (re[0],re[1])
re = cur.fetchone()
print "id=%s,info=%s" % (re[0],re[1])
res=cur.fetchall() #获取当前游标记录及后续所有记录
for re in res:
print "id=%s,info=%s" % (re[0],re[1])
cur.close()
conn.close()
print "finish query fetchone"
except MySQLdb.Error,e:
print e.args[1] try:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306)
cur=conn.cursor()
cur.execute('delete from test')
conn.commit()
cur.close()
conn.close()
print "finish delete"
except MySQLdb.Error,e:
print e.args[1]

需要说明的是,如果python脚本的字符集编码与数据库的字符集不一致,中文会出现乱码。

解决方法是在获取链接时指定数据库的字符集。如:

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306,charset='gbk')

上面语句通过 增加 charset='gbk' 指定数据库的字符集。