mysql for python

为了安装mysql for python,从sourceforge下载了三遍安装文件,每次进入目录中执行 python setup.py install,总是报错

如下:

serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_ke
y'])
WindowsError: [Error 2]

,google了下,一般解释是regedit中没有设置,在SO中也有人提出这样的问题(link),看来是个不小的bug。

各种修改无果。继续google之,看到还是有解决方案的呀(link),果断从codegood下载到for python 2.7的安装文件,安装后,测试OK

简单连接mysql,测试通过

 1 #-*- encoding:gb2312  -*-
2 import os,sys,string
3 import MySQLdb
4
5 try:
6 conn = MySQLdb.connect(host = "localhost",user="root",passwd = "123",db="information_schema")
7 except Exception,e:
8 print e
9 sys.exit()
10
11 cursor = conn.cursor()
12
13 #sql = "create table if not exists test1(name varchar(120)primary key,age int(4))"
14 #cursor.execute(sql)
15 #中文
16 #sql = "if exists test 1 drop table test1"
17 #cursor.execute(sql)
18
19 #sql = "truncate table firsttable"
20 #cursor.execute(sql)
21
22 #for i in [1,2,4,5,6]:
23 # sql = """insert into firsttable (name) values('liu')"""
24 # cursor.execute(sql)
25
26 cursor.execute("select * from character_sets");
27 resultset = cursor.fetchall();
28 print resultset # pass, python is good at linking to mysql
29
30 cursor.close()
31 conn.close();
32

测试通过,下面用django把框架搭起来,菜鸟准备继续前进。

原文地址:https://www.cnblogs.com/crafet/p/2436395.html