码迷,mamicode.com
首页 > 数据库 > 详细

Python连接MySQL数据库(pymysql的使用)

时间:2018-08-13 00:42:51      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:engine   ros   create   font   连接mysql   返回字典   tin   utf-8   any   

本文Python版本3.5.3,mysq版本5.7.23

基本使用

 

# 导入pymysql模块 
import pymysql

#连接数据库
conn = pymysql.connect(
    database="数据库名",
    user="用户名",
    password="密码",
    host="数据的地址,
    port=3306, # 端口3306是固定的
    charset="utf8")  # 只能是utf8,千万不能是utf-8.在数据库中只能是utf8

#得到一个可以执行SQL的游标对象
cursor = conn.cursor()

#定义需要执行的SQL语句
sql="""
create table user(
id int auto_increment primary key,
name char(10) not null unique,
age tinyint not null
)engine=innodb default charset=utf8;
"""
#执行语句
cursor.execute(sql)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()

 

对数据进行增删改查

# 导入pymysql模块 
import pymysql

#连接数据库
conn = pymysql.connect(
    database="db",
    user="root",
    password="123456",
    host="localhost",
    port=3306,
    charset="utf8")

#得到一个可以执行SQL的游标对象
cursor = conn.cursor()

#定义需要执行的SQL语句
sql="""
insert into user(name,age) values(%s,%s);
"""
name = "曹操",
age = 34
data = [("aaa", 18), ("bbb", 20), ("cccc", 21)]

try:
    print(sql)
    #插入一条数据SQL语句,只能插入单条
    # cursor.execute(sql,(name,age)) # 防止SQL注入

    #批量插入多天SQL语句
    cursor.executemany(sql,data)  #第二个参数可以是列表或数组
    # 提交事务)
    conn.commit()
except Exception as e:
    # 有异常,回滚事务
    conn.rollback()
    print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()

  删

# 导入pymysql模块 
import pymysql

#连接数据库
conn = pymysql.connect(
    database="db",
    user="root",
    password="123456",
    host="localhost",
    port=3306,
    charset="utf8")

#得到一个可以执行SQL的游标对象
cursor = conn.cursor()

#定义需要执行的SQL语句
sql="""
delete from user where id=%s
"""

try:
    cursor.execute(sql,[4])
    # 提交事务
    conn.commit()
except Exception as e:
    # 有异常,回滚事务
    conn.rollback()
    print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()

  改

# 导入pymysql模块 
import pymysql

#连接数据库
conn = pymysql.connect(
database="db",
user="root",
password="123456",
host="localhost",
port=3306,
charset="utf8")

#得到一个可以执行SQL的游标对象
cursor = conn.cursor()

#定义需要执行的SQL语句
sql="""
update user set age=%s where name=%s;
"""
name="刘备"
age=18

try:
cursor.execute(sql,(age,name))
# 提交事务
conn.commit()
except Exception as e:
# 有异常,回滚事务
conn.rollback()
print(e)
#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()

  

# 导入pymysql模块 
import pymysql

#连接数据库
conn = pymysql.connect(
    database="db",
    user="root",
    password="123456",
    host="localhost",
    port=3306,
    charset="utf8")

# 得到一个可以执行SQL的游标对象
# 增加参数后结果返回字典格式的数据
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

#定义需要执行的SQL语句
sql="""
select * from user where id=2;
"""

sql1="""
select * from user;
"""

# 执行查询数据的SQL语句
cursor.execute(sql)
# 获取单条查询数据
ret = cursor.fetchone()

# 获取多条查询数据
cursor.execute(sql1)
ret1 = cursor.fetchall()

#关闭游标对象
cursor.close()
#关闭数据库连接
conn.close()

# 打印查询结果
print(ret)
print(ret1)

  得到的数据为字典类型的数据,在数据的传输过程中比较好

# 可以获取指定数量的数据

cursor.fetchmany(4)

 

Python连接MySQL数据库(pymysql的使用)

标签:engine   ros   create   font   连接mysql   返回字典   tin   utf-8   any   

原文地址:https://www.cnblogs.com/materfont/p/9465457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!