博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【mysql-python】安装+基本使用
阅读量:7241 次
发布时间:2019-06-29

本文共 2518 字,大约阅读时间需要 8 分钟。

安装:从SourceForge.net上下载最新的MySQLdb, 运行exe文件

使用

From:http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html

import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)
cur=conn.cursor()
cur.execute('select * from user')
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
  请注意修改你的数据库,主机名,用户名,密码。

 

 

import MySQLdb

try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306)
cur=conn.cursor()
cur.execute('create database if not exists python')
conn.select_db('python')
cur.execute('create table test(id int,info varchar(20))')
value=[1,'hi rollen']
cur.execute('insert into test values(%s,%s)',value)
values=[]
for i in range(20):
values.append((i,'hi rollen'+str(i)))
cur.executemany('insert into test values(%s,%s)',values)
cur.execute('update test set info="I am rollen" where id=3')
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
  请注意一定要有conn.commit()这句来提交事务,要不然不能真正的插入数据。

 

 

 

import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306)
cur=conn.cursor()
conn.select_db('python')
count=cur.execute('select * from test')
print 'there has %s rows record' % count
result=cur.fetchone()
print result
print 'ID: %s info %s' % result
results=cur.fetchmany(5)
for r in results:
print r
print '=='*10
cur.scroll(0,mode='absolute')
results=cur.fetchall()
for r in results:
print r[1]
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
  运行结果就不贴了,太长了。

查询后中文会正确显示,但在数据库中却是乱码的。经过我从网上查找,发现用一个属性有可搞定:

在Python代码

conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python') 中加一个属性:

改为:
conn = MySQLdb.Connect(host='localhost', user='root', passwd='root', db='python',charset='utf8')
charset是要跟你数据库的编码一样,如果是数据库是gb2312 ,则写charset='gb2312'。

 

下面贴一下常用的函数:

然后,这个连接对象也提供了对事务操作的支持,标准的方法

commit() 提交
rollback() 回滚

cursor用来执行命令的方法:

callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数
execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数
executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数
nextset(self):移动到下一个结果集

cursor用来接收返回值的方法:

fetchall(self):接收全部的返回结果行.
fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.
fetchone(self):返回一条结果行.
scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果 mode='absolute',则表示从结果集的第一行移动value条.

 

你可能感兴趣的文章
键盘的扫描码虚拟码概念 常见Windows键盘按键虚拟码
查看>>
webcast
查看>>
PHP实现跨域自动登录
查看>>
java多线程系列(四)---ReentrantLock的使用
查看>>
PHP问题 —— Warning: PHP Startup: Unable to load dyna
查看>>
XMLHttpRequest Post参数
查看>>
1.2.方法的参数
查看>>
oracle 数据库(表)的逻辑备份与恢复
查看>>
Windows程序设计【001】第一个Windows程序
查看>>
5.1 priority_queue使用
查看>>
如何提高企业云ERP系统生产计划执行率
查看>>
php数据序列化测试
查看>>
errno的陷阱
查看>>
CentOS 7 主机名的修改
查看>>
troubleshooting shuffle reduce端缓冲大小以避免OOM
查看>>
全球1.7万台Mac电脑感染新恶意软件iWorm
查看>>
SpringMVC bean validator 自定义注解
查看>>
每周跑步锻炼
查看>>
Java反射:object is not an instance of declaring c...
查看>>
GIT 远程仓库:添加远程库、从远程库克隆
查看>>