Python连接MySQL
版权声明
:转载时请以超链接形式标明文章原始出处和作者信息及本声明
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=
相关文档:
在Python中有一个非常重要也非常好用的模块re,在import re后,就能够在Python中使用正则表达式,源于此次项目要用正则表达式对html代码提取一定的字符,所以在这也就用些小例子来熟悉一下正则表达式
现在就用最简单的例子
import re
s='<title>http://www.baidu.com</title>'
print re.findall(r'&l ......
下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符。 全角符号转半角符号。 unicode字符串归一化等工作。 还有一个能处理多音字的汉字转拼音的程序,还在整理中。
#!/usr/bin/env python
# -*- coding:GBK -*-
"""汉字处理的工具:
判断unicode是否是汉字,数字,英 ......
作者:taowen, billrice
http://www.cnblogs.com/taowen/articles/11239.html
Lesson 1 准备好学习Python的环境
下载的地址是:
www.python.org
为了大家的方便,我在校内作了copy:
http://10.1.204.2/tool/compiler&IDE/Python-2.3.2-1.exe
linux版本的我就不说了,因为如果你能够使用linux并安装好说明你可以 ......
>>> class objA:
... pass
...
>>> A = objA()
>>> B = 'a','V'
>>> B
('a', 'V')
>>> C = 'a string'
>>> print instance(A,objA)
Traceback (most recent call last):
File "<stdin>", line 1, in < ......
import pymssql
#connect database
conn=pymssql.connect(host="192.168.1.28",user="boomink",password="boomink",
database="boomink")
cur=conn.cursor()
print '========================================'
cur.execute("exec Orders_GetTest1 @Valu ......