今天潜心研究了python 的Mysql驱动包,主要是对Python很有感觉.
下载wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1.0.5.zip
解压、编译、安装一下,然后开始测试驱动包里给的示例文件:
[root@localhost examples]# pwd
/home/xuegang/mysql-connector-python-1.0.5/python2/examples
[root@localhost examples]#
[root@localhost examples]#
[root@localhost examples]# ll
total 44
-rw-r--r--. 1 root root 4214 Aug 23 23:37 client.py
-rw-r--r--. 1 root root 1829 Jul 17 20:51 config.py
-rw-r--r--. 1 root root 3519 Jul 17 20:51 dates.py
-rwxr-xr-x. 1 root root 1840 Jul 17 20:51 engines.py
-rw-r--r--. 1 root root 0 Jul 17 20:51 __init__.py
-rwxr-xr-x. 1 root root 2772 Jul 17 20:51 inserts.py
-rw-r--r--. 1 root root 3324 Jul 17 20:51 microseconds.py
-rw-r--r--. 1 root root 2997 Jul 17 20:51 multi_resultsets.py
-rwxr-xr-x. 1 root root 3883 Jul 17 20:51 transaction.py
-rwxr-xr-x. 1 root root 2921 Jul 17 20:51 unicode.py
-rwxr-xr-x. 1 root root 2036 Jul 17 20:51 warnings.py
[root@localhost examples]# python client.py
Welcome to MySQL Python CLI.
Server version: 5.1.61
MySQL Connector/Python vpython.2.6
Python 2.6.6 (r266:84292, Dec 7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(MySQLConsole)
>>> show databases;
(u'information_schema',)
(u'mysql',)
(u'test',)
>>>
用起来倒像Mysql-client, 不过表示为Python 格式的数据结构。
client.py 这个文件,在centos6.2 上安装默认的mysql,其中有部分代码还是需要修改下:
130 console = MySQLConsole(db)
131 #myconnpy_version = "%s-%s" % (
132 # '.'.join(map(str,mysql.connector.__version__[0:3])),
133 # mysql.connector.__version__[3])
135 myconnpy_version = 'python.2.6'
136 #print "Your MySQL connection ID is %d." % (db.get_server_threadid())
注释掉这些行直接就可以使用了,这估计也是Python 灵活性的代价吧。
然后怎样使用,直接练习example文件夹里的文件..