1.安装python2.7或者3.3
2.下载python下mysql驱动
mysql for python 下载地址
http://sourceforge.net/projects/mysql-python/
3.编码
3.1创建数据库
CREATE DATABASE test;
3.2创建数据库table
CREATE TABLE test
(
id INT ,
`name` VARCHAR(50),
create_time INT
)
上代码:
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='test',port=3306)
cur = conn.cursor()
cur.execute('select * from test1')
cur.close()
conn.close()
print('Hello World')
运行即可
这里在运行的时候出了点问题
host不能写localhost,只有用ip才能成功,留下了个疑问,有知道的高手请告诉一下哈。
错误信息:can't connect to mysql server on 'localhost' (10061)