python--pymysql

前言:关于pymysql是python与数据库的连用,在pychrm中将mysql应用(增删改查)。

基本操作:
  1、pychrm中引入数据库

    首先在pychrm中的View中找到Tool Windows中的Database,点击Database后pychrm右边找到Database,在点击打开Database,在左上方有加号符点击找到Data Success找到Mysql并点击,在出现的页面上(这里是在有数据库的情况下)就能输入数据库名和user(通常:root)及password(123456),完了以后点击Test Connection(测试连接是否成功)有变出现success时表名连接成功。在点击OK即可。

    或者在没有数据库的情况下:当点击Mysql后直接点击Test Coonection 弹出输入用户名和密码,此时输入root用户名和123456密码(自己设置的mysql密码)点击ok再点击OK生成以和location右击鼠标Table是新建表,Schema是新建数据库新建表。
pymysql应用

Python连接MySQL数据库

一,Python3连接Mysql

1,pyMysql介绍

  Pymysql是在python3.x版本中用于连接MySQL服务器的一个库,Python2中则使用mysqldb。

  Django中也可以使用PyMSQL连接MuSQL数据库。

2、PyMSQL安装

<1>在cmd中先输入命令python -V(大写)或pip -V查看pychrm版本。

<2>安装:pip install pymysql

3,连接数据库

  <1>注意事项

    在进行文本一下内容之前需要注意:

  有一个Mysql数据库,并且已经启动。

  可以连接该数据库的用户名和密码

  有一个有权限操作的database。

  <2>基本使用

# 导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '本地地址',user = ‘用户名’,password = ‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行的SQL语句的光标对象
cursor = conn.cursor()
#定义要执行的SQL语句
sql = ‘create table user1(id int auto_increment primary key,
                          name char(10) not null unique(唯一索引),
                          age tinyint(最小整形) not null,
                          password char(20) not null
                         )engine = innodb default charset= utf8;
      ’                   
#执行SQL语句
cursor.execute(sql)
#关闭光标对象
cursor.close()
#断开数据连接
conn.close()

4,增删改查操作

<1>增

#导入模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database= ‘数据库名’,charset= ‘utf8’)
#得到一个可以执行的SQL语句的光标对象
cursor = conn.cursor()
sql = 'insert into user1(name,age) vaules (%s,%s)'
username = ‘Alex’
age = 18
#执行SQL语句
crusor.execute(sql,[username,age])
#提交事件
conn.commit()
cursor.close()
conn.close()

插入数据失败回滚

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = 'insert into user1(name,age) values(%s,%s);'
username = 'Alex'
age = 18
try:
    #执行SQL语句
    cursor.execute(sql,[username,age])
    #提交事务
    conn.commit()
except Exception as e:
    #有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

获取插入数据的ID(关联操作是会用到)

#导入模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database= ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = 'insert into(name,age) values(%s,%s);'
usernamee = 'Alex'
age = 18
try:
    #执行SQL语句
    cursor.execute(sql,[username,age])
    #提交事务
    conn.commit()
    #提交之后,获取刚插入的数据ID
    last_id = cursor.lastrwid
except Exceprion as e:
    #有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

批量执行

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user= ‘用户名’,password =‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = 'insert into user1(name,age) values(%s,%s);'
data = [('Alex'.18),('Egon',20),('Yuan',20)]
try:
    #批量执行多条插入SQL语句
    cursor.exectemany(sql,data)
    #提交事务
    conn.commit()
except Exception as e:
    #有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

<2> 删

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
sql = 'delete from user1 where id = %s;'
try:
    cursor.execute(sql,[4])
    #提交事务
    conn.commit()
execpt Exception as e:
    #有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

<3>改

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database= ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行的SQL语句的光标对象
cursor = conn.cursor()
#修改数据库的语句
sql = 'updata user1 set age = %s where name = %s;'
username = 'Alex'
age = 80
try:
    #执行SQL语句
    cursor.execute(sql,[username])
    #提交事务
    conn.commit()
except Exception as e:
    #有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

<4> 查

查询单条数据

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行SQl语句的光标对象
cursor = conn.cursor()
#查询数据的SQL语句
sql = 'select id,name,age from user! where id = 1;'
#执行SQl语句
cursor.execute(sql)
#获取单条查询数据
ret = cursor.fetchone()
cursor.close()
conn.close()
#打印下查询结果
print(ret)

查询多条数据

#导入pymysql模块
import pymysql
#连接database
conn = pymysql.connect(host = '你的数据库地址',user = ‘用户名’,password = ‘密码’,database = ‘数据库名’,charset = ‘utf8’)
#得到一个可以执行Sql语句的光标对象
corsor = conn.corsor()
#查询数据的SQL语句
sql = 'select id ,name,age from user1;'
#执行SQl语句
cursor.execte(sql)
#获取多条查询数据
ret = cursor.fetchall()
cursor.close()
conn.close()
#打印下查询结果
print(ret)

进阶用法

#可以获取指定数量的数据
cursor.fetcmany(3)
#光标按绝对位置移动1
cursor.scroll(1,mode = 'absolute')
#光标按照相对位置(当前位置)移动1
cursor.scroll(1,mode = 'relative')

 

posted on 2018-01-10 23:19  一万年  阅读(2850)  评论(0编辑  收藏  举报