python连接数据库

# -*- coding: utf-8 -*-

import MySQLdb

try:
	conn =MySQLdb.connect(host='localhost',user='root',passwd='123456',db='python_test',port=3306)
	cur = conn.cursor() #光标
	cur.execute('use python_test;')
	cur.execute('CREATE TABLE test(id char(20));')
	cur.close()
	conn.close()
except MySQLdb.Error,e:
	print '连接失败。',e



安装MySQLdb模块:


yum install MySQL-python -y