Linux 下Python 安装使用mysql


1、安装mysql 

2、安装python的 MySQLdb模块。

地址:https://pypi.python.org/pypi/MySQL-python/

1.2.5 下载:

wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip  --no-check-certificate

解压进入目录

<span style="font-family:Microsoft YaHei;font-size:14px;">python  setup.py install   </span>

安装提示成功即可。安装过程中出现了两个错误:

1)没有setuptools

yum install  python-setuptools

2)error: command 'gcc' failed with exit status 1

<span style="font-family:Microsoft YaHei;font-size:14px;">yum install python-devel</span>

再次安装提示成功。

3、mysql操作:

1)使用MySQLdb驱动

import MySQLdb

2)连接数据库:

try:
    conn=MySQLdb.connect(host='localhost',user='root', passwd='1111', db='dbname', port=3306<span style="color: rgb(51, 51, 51); line-height: 25.2000007629395px;">, charset='utf8'</span>)
    cur=conn.cursor()
    <code class="python plain" style="line-height: 21.6000003814697px; white-space: nowrap; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background: none !important;">conn.select_db(</code><code class="python string" style="line-height: 21.6000003814697px; white-space: nowrap; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; color: blue !important; background: none !important;">'python'</code><code class="python plain" style="line-height: 21.6000003814697px; white-space: nowrap; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: inherit !important; background: none !important;">)</code>
    cur
.close()
    conn.close()
except MySQLdb.Error,e:
     print "Mysql Error %d: %s" % (e.args[0], e.args[1])

3)查询:

#获得表中有多少条数据
aa=cur.execute("select * from student")
print aa

#打印表中的多少数据
info = cur.fetchmany(aa)
for ii in info:
    print ii


4)插入:

    value=[1,'hi rollen']
  cur.execute('insert into test values(%s,%s)',value)

5)更新:

    cur.execute('update test set info="I am rollen" where id=3')
常用函数:

conn.commit() 提交
conn.rollback() 回滚

conn.select_db('dbname')

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条.
















猜你喜欢

转载自blog.csdn.net/wdearzh/article/details/46122213