1. 程式人生 > >python連接數據庫

python連接數據庫

python mysql

# -*- 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




本文出自 “天道酬勤” 博客,請務必保留此出處http://tdcqvip.blog.51cto.com/12995943/1954100

python連接數據庫