使用Python来连接MySQL
本来以为使用Python安装MySQL是件容易的事,但是发现,不管怎样每次安装编译都不通过。强大的pip install mysql-python 不再管用。报错内容为:
Traceback (most recent call last): File "setup.py", line 15, in <module> metadata, options = get_config() File "D:\MySQL-python-1.2.3\setup_windows.py", line 7, in get_config serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])WindowsError: [Error 2] |
在网上找了很久都没有找到解决方案,于是在QQ群里问了下,找到了一个做好的安装包,迅速的解决了此问题。
安装包下载地址:http://code.google.com/p/soemin/downloads/list
以下为一个简单的使用python向MySQL插入数据的代码,仅供参考:
import MySQLdbdb = MySQLdb.connect(host="localhost", user="root", passwd="root",db="slopeone")cursor = db.cursor()f = open("sample.data", "r")for line in f: content = line.rstrip('\n').split("\t") user_id = content[0] item_id = content[1] rating = content[2] print user_id,item_id,rating cursor.execute("insert into oso_user_ratings values(%s,%s,%s)",(user_id,item_id,rating))f.close()print "over!" |
二月 8th, 2012 at 11:50:03
先去测试看看。。谢谢