简介:
PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。
PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。
首先是安装环境,下载pymysql
打开Navicat在其中选择一个数据库进行演示,我这里就选择了projectdatabase进行演示。
导入库
import pymysql
连接数据库的代码
放好自己的配置文件信息,本地就是localhost,其他的地方的就是按照别人的域名来看,然后就是用户名,密码,数据库的名字。
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
except pymysql.Error as e:
print('数据库连接失败'+str(e))
1、声明一个游标
cur = db.cursor()
2、创建表之前先检查是否存在,如果存在则删除
cur.execute('DROP TABLE IF EXISTS Student')
3、编辑sql语句
sqlQuery = "CREATE TABLE Student(Name CHAR(20) NOT NULL ,Email CHAR(20),Age int )"
cur.execute(sqlQuery)
完整代码
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
except pymysql.Error as e:
print('数据库连接失败'+str(e))
# 这里创建游标
cur = db.cursor()
cur.execute('DROP TABLE IF EXISTS Student')
# 这里输入sql语句
sqlQuery = "CREATE TABLE Student(Name CHAR(20) NOT NULL ,Email CHAR(20),Age int )"
cur.execute(sqlQuery)
编写sql语句
sqlQuery=" INSERT INTO Student (Name, Email, Age) VALUE (%s,%s,%s) "
设置具体的值
value=('jake','[email protected]',20)
完整代码
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
# 这里创建游标
cur = db.cursor()
# 这里输入sql语句
sqlQuery = " INSERT INTO Student (Name, Email, Age) VALUE (%s,%s,%s) "
value = ('jake', '[email protected]', 20)
cur.execute(sqlQuery, value)
db.commit()
print('数据插入成功!')
except pymysql.Error as e:
print("数据插入失败:" + str(e))
# 如果数据插入失败的话 就事件回滚
db.rollback()
db.close()
编写sql语句
sqlQuery = "SELECT * FROM Student"
完整代码
python的这个代码有点奇怪,特别需要注意的是这游标需要先执行运行这个sql语句,之后才可以执行fetchall方法,不然会查询失败。
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
# 这里创建游标
cur = db.cursor()
# 这里输入sql语句
sqlQuery = "SELECT * FROM Student"
col = cur.execute(sqlQuery) # 查询之前需要先运行sql语句
results = cur.fetchall() # 只有上面先运行了sql语句这句话才可以运行成功
print(col) # 结果为影响的条数
for row in results:
name = row[0]
email = row[1]
age = row[2]
print('Name:%s,Email:%s,Age:%s'%(name,email,age))
except pymysql.Error as e:
print("数据查询失败:" + str(e))
db.close()
编写sql语句
sqlQuery = "UPDATE Student SET Name= %s WHERE Name=%s"
value = ('John', 'jake')
完整代码
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
# 这里创建游标
cur = db.cursor()
# 这里输入sql语句
sqlQuery = "UPDATE Student SET Name= %s WHERE Name=%s"
value = ('John', 'jake')
cur.execute(sqlQuery, value)
db.commit()
print('数据更新成功!')
except pymysql.Error as e:
print("数据更新失败:" + str(e))
# 发生错误时回滚
db.rollback()
db.close()
编写sql语句
sqlQuery = "delete from student where name=%s"
value = ('John')
完整代码
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
# 这里创建游标
cur = db.cursor()
# 这里输入sql语句
sqlQuery = "delete from student where name=%s"
value = ('John')
cur.execute(sqlQuery, value)
db.commit()
print('数据更新成功!')
except pymysql.Error as e:
print("数据更新失败:" + str(e))
# 发生错误时回滚
db.rollback()
db.close()
编写sql语句
sqlQuery='DROP TABLE IF EXISTS Student'
完整代码
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = '123456'
DBNAME = 'projectdatabase'
try:
db = pymysql.connect(host=DBHOST,user=DBUSER, password=DBPASS, database=DBNAME)
print('数据库连接成功!')
# 这里创建游标
cur = db.cursor()
# 这里输入sql语句
sqlQuery='DROP TABLE IF EXISTS Student'
cur.execute(sqlQuery)
db.commit()
print('数据表删除成功!')
except pymysql.Error as e:
print("数据表删除失败:" + str(e))
# 发生错误时回滚
db.rollback()
db.close()