python -- MySQLdb连接mysql数据库

1. python安装mysql

$ pip install mysql-python

2. 数据库连接程序:

import MySQLdb

# 打开数据库连接
db = MySQLdb.connect(host="vpca-1.vm.elenet.me",port=9707, user="vpca_user",passwd="123456",db="vpca_db")

# 使用cursor()方法获取操作游标
cursor = db.cursor()

# 使用execute方法执行SQL语句
cursor.execute("SELECT * FROM tb_name limit 1;")

# 使用 fetchone() 方法获取一条数据库。
data = cursor.fetchone()

print data

# 关闭数据库连接
db.close()

原文地址:https://www.cnblogs.com/sunshine2016/p/6187880.html