首页 新闻 赞助 找找看

PYthon 连接mysQL

0
悬赏园豆:200 [待解决问题]

求好人,给个python,IDLE上实现对mysql操作的基本语句,连接,建表,增删改查~

TDSwx的主页 TDSwx | 初学一级 | 园豆:0
提问于:2011-07-13 15:38
< >
分享
所有回答(2)
0

http://mobile2008.blogbus.com/logs/28725962.html

安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验了一下挺好用,
不过又发现了烦人的乱麻问题,最后用了几个办法,解决了!

我用了下面几个措施,保证MySQL的输出没有乱麻:
    1 Python文件设置编码 utf-8 (文件前面加上 #encoding=utf-8)
    2 MySQL数据库charset=utf-8 
    3 Python连接MySQL是加上参数 charset=utf8 
    4 设置Python的默认编码为 utf-8 (sys.setdefaultencoding(utf-8)

mysql_test.py

#encoding=utf-8
import sys
import MySQLdb

reload(sys)
sys.setdefaultencoding(
'utf-8')

db
=MySQLdb.connect(user='root',charset='utf8')
cur
=db.cursor()
cur.execute(
'use mydb')
cur.execute(
'select * from mytb limit 100')

f
=file("/home/user/work/tem.txt",'w')

for i in cur.fetchall():
    f.write(str(i))
    f.write(
" ")

f.close()
cur.close()

上面是linux上的脚本,windows下运行正常!

注:MySQL的配置文件设置也必须配置成utf8

设置 MySQL 的 my.cnf 文件,在 [client]/[mysqld]部分都设置默认的字符集(通常在/etc/mysql/my.cnf): 
[client]
default-character-set = utf8
[mysqld]
default-character-set = utf8
---------------------------------------------

#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE  : 2004-12-28
#
# COMMENT: 这是一个python连接mysql的例子
#
#========================================================================= 

"""
 ***** This is a MySQL test *****
 
 select:
  conn=Connection()
  conn.select_db('test')
  cur=conn.cursor()
  cur.execute('select * from user')
  cur.scroll(0)
  row1=cur.fetchone()
  row1[0]
  row1[1]
  row1[2]
  
 insert:
  cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')')
  cur.insert_id()
  
 update:
  cur.execute('update user set passwd=\'123456\' where name=\'benyur\'')
  
 delete:
  cur.execute('delete from user where id=2')
 
 **********************************
"""


from MySQLdb import *


def conn():
 conn=Connection()
 conn.select_db('test')
 cur=conn.cursor()
 cur.execute('select * from user')
 cur.scroll(0)
 row1=cur.fetchone()
 row1[0]
 row1[1]
 row1[2]


def usage():
 print __doc__


if __name__=='__main__':
 usage()



MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/
下载解压缩后放到%Python_HOME%\Lib\site-packages目录中,python会自动找到此包。
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。

其他:

1.   平台及版本
linux 内核2.6,gcc 3.4.4,glibc 2.4
python 2.4.3
mysql 5.0.19
mysql-python 1.2.1-p2
2.   安装mysql-python
tar xvfz MySQL-python-1.2.1_p2.tar.gz
cd MySQL-python-1.2.1_p2
python setup.py build
python setup.py install

3.   使用
import MySQLdb
3.1.   连接
conn =   MySQLdb.Connection(host, user, password, dbname)
3.2.   选择数据库
 conn.select_db(’database name’)
3.3.   获得cursor
cur =   conn.cursor()
3.4.   cursor位置设定
cur.scroll(int, mode)
mode可为相对位置或者绝对位置,分别为relative和absolute。


3.5.   select
cur.execute(‘select clause’)
例如
cur.execute(‘select * from mytable’)


row = cur.fetchall()
或者:
row1 = cur.fetchone()
3.6.   insert
cur.execute(‘inset clause’)
例如
cur.execute(‘insert  into table (row1, row2) values (\’111\’, \’222\’)’)


conn.commit()


3.7.   update
cur.execute(‘update  clause’)
例如
cur.execute(“update  table set   row1 = ‘’  where  row2 = ‘row2 ‘  ”)


conn.commit()


3.8.   delete
cur.execute(‘delete  clause’)
例如
cur.execute(“delete from  table  where   row1 = ‘row1’  ”)


conn.commit()

鹤冲天 | 园豆:2379 (老鸟四级) | 2011-07-13 16:15
纠结,内部网络,大部分对外访问都被屏蔽了,只能访问几个开放的网站~!还是谢谢
支持(0) 反对(0) TDSwx | 园豆:0 (初学一级) | 2011-07-14 08:53
我把那页面的内容粘贴在答案中了。
支持(0) 反对(0) 鹤冲天 | 园豆:2379 (老鸟四级) | 2011-07-14 08:56
0

这个在windows下需要安装MySQL-python可执行版本,linux下可直接下载tar包进行安装...

然后在根据上面回答的代码才能跑起来

西越泽 | 园豆:10775 (专家六级) | 2011-07-14 13:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册