Python3.4连接MySQL

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aixp88/article/details/57414778

一、安装pymysql

  1. 去github上下载pymysql的安装包 pymysql
  2. 解压
  3. 打开cmd窗口(win环境下),进入pymysql的根目录下执行命令,python setup.py install

二、使用例子

import pymysql

#数据库连接
db = pymysql.connect('host','user','password','database',3306,'utf8')
#获取一个游标
cursor = db.cursor()
#执行sql语句
cursor.execute("SELECT * from lezhuan")
#获取一行数据
data = cursor.fetchone()

print(data)

数据库操作的API文档连接: http://legacy.python.org/dev/peps/pep-0249/

猜你喜欢

转载自blog.csdn.net/aixp88/article/details/57414778