zoukankan      html  css  js  c++  java
  • mysql基本操作

    昨天在李哥帮忙检验我学习效果的时候
    我使用pymysql出现了以下的错误

    1
    python-module 'pymysql' has no attribute 'connect'

    一出错 我本能的想去看下是不是我没连接成功 然后 pip3 install pymysql
    不要起import的包名作为文件名啊!!!

    因此,我总结了下pymysql的基本使用

    一、PyMySQL介绍
    PyMySQL是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中是使用mysqldb。

    PyMySQL安装

    1
    pip3 install pymysql

    创建链接的基本使用 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    # 导入pymysql模块
    import pymysql
     
    # 连接database
    conn = pymysql.connect(
        host=“你的数据库地址”,
        user=“用户名”,password=“密码”,
        database=“数据库名”,
        charset=“utf8”)
     
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
    # 得到一个可以执行SQL语句并且将结果作为字典返回的游标
    #cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
     
    # 定义要执行的SQL语句
    sql = """
    CREATE TABLE USER1 (
    id INT auto_increment PRIMARY KEY ,
    name CHAR(10) NOT NULL UNIQUE,
    age TINYINT NOT NULL
    )ENGINE=innodb DEFAULT CHARSET=utf8;  #注意:charset='utf8' 不能写成utf-8
    """
     
    # 执行SQL语句
    cursor.execute(sql)
     
    # 关闭光标对象
    cursor.close()
     
    # 关闭数据库连接
    conn.close()

    在建链接之前,我们需要做好一些前期工作:建库建表

    下面例子中  我将使用我建好的库:db= 'xing'

    建好的userinfo表

    简单验证功能

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    # pip3 install pymysql
    import pymysql
     
    user=input('user>>: ').strip()
    pwd=input('password>>: ').strip()
     
    # 建立链接
    conn=pymysql.connect(
        host='192.168.0.103',#我的IP地址
        port=3306,   # 不是字符串不需要加引号。
        user='root',
        password='123',
        db='xing',
        charset='utf8'
    )
     
    # 拿到游标
    cursor=conn.cursor()
     
    # 执行sql语句
     
    sql='select * from userinfo where user = "%s" and pwd="%s"' % (user, pwd)
    print(sql)
    res=cursor.execute(sql)
    print(res)
     
    cursor.close()
    conn.close()
     
    # 进行判断
    if res:
        print('登录成功')
    else:
        print('登录失败')

      

    输出结果:

    但是会有以下问题:输入的SQL 语句被注释了

    或者是

    这个时候之后 我们可以这样解决

    1
    2
    3
    4
    5
    6
    7
    8
    9
    execute帮我们做字符串拼接
     
    # 将以下代码
    sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
    res=cursor.execute(sql)
    # 改为
    sql="select * from userinfo where name=%s and password=%s" #%s需要去掉引号,pymysql会自动加上
     
    res=cursor.execute(sql,[user,pwd])

     输出结果:

    二、增删改查操作

    添加多条数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import pymysql
     
    conn = pymysql.connect(
        host='192.168.0.103',
        port=3306,
        user='root',
        password='123',
        database='xing',
        charset='utf8'
    )
    # 获取一个光标
    cursor = conn.cursor()
     
    # 定义要执行的sql语句
    sql = 'insert into userinfo(user,pwd) values(%s,%s);'
    data = [
        ('july''147'),
        ('june''258'),
        ('marin''369')
    ]
    # 拼接并执行sql语句
    cursor.executemany(sql, data)
     
    # 涉及写操作要注意提交
    conn.commit()
     
    # 关闭连接
    cursor.close()
    conn.close()

    输出结果:

    插入单条数据
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import pymysql
    conn =pymysql.connect(
        host ='192.168.0.103',
        port = 3306,
        user = 'root',
        password ='123',
        database ='xing',
        charset ='utf8'
    )
    cursor =conn.cursor()  #获取一个光标
    sql ='insert into userinfo (user,pwd) values (%s,%s);'
     
    name = 'wuli'
    pwd = '123456789'
    cursor.execute(sql, [name, pwd])
    conn.commit()
    cursor.close()
    conn.close()

     输出结果:

     

    获取最新插入数据 (最后一条)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import pymysql
     
    # 建立连接
    conn = pymysql.connect(
        host="192.168.0.103",
        port=3306,
        user="root",
        password="123",
        database="xing",
        charset="utf8"
    )
    # 获取一个光标
    cursor = conn.cursor()
    # 定义将要执行的SQL语句
    sql = "insert into userinfo (user, pwd) values (%s, %s);"
    name = "wuli"
    pwd = "123456789"
    # 并执行SQL语句
    cursor.execute(sql, [name, pwd])
    # 涉及写操作注意要提交
    conn.commit()
    # 关闭连接
     
    # 获取最新的那一条数据的ID
    last_id = cursor.lastrowid
    print("最后一条数据的ID是:", last_id)
     
    cursor.close()
    conn.close()

    输出结果为:(因为我之前插入多条记录时,多运行了两次,所有结果下面的这个)

      删除操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import pymysql
     
    # 建立连接
    conn = pymysql.connect(
        host="192.168.0.103",
        port=3306,
        user="root",
        password="123",
        database="xing",
        charset="utf8"
    )
    # 获取一个光标
    cursor = conn.cursor()
    # 定义将要执行的SQL语句
    sql = "delete from userinfo where user=%s;"
    name = "june"
    # 拼接并执行SQL语句
    cursor.execute(sql, [name])
    # 涉及写操作注意要提交
    conn.commit()
    # 关闭连接
     
    cursor.close()
    conn.close()

    输出结果是:

    更改数据
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import pymysql
     
    # 建立连接
    conn = pymysql.connect(
        host="192.168.0.103",
        port=3306,
        user="root",
        password="123",
        database="xing",
        charset="utf8"
    )
    # 获取一个光标
    cursor = conn.cursor()
    # 定义将要执行的SQL语句
    sql = "update userinfo set pwd=%s where user=%s;"
    # 拼接并执行SQL语句
    cursor.execute(sql, ["july""july"])
     
    # 涉及写操作注意要提交
    conn.commit()
     
    # 关闭连接
    cursor.close ()
    conn.close ()




    查询数据
    fetch数据
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import pymysql
     
    conn = pymysql.connect (
        host='192.168.0.103',
        port=3306,
        user='root',
        password='123',
        database='xing',
        charset='utf8'
    )
    # 获取一个光标
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  # 返回字典数据类型
     
    # 定义将要执行的sql语句
    sql = 'select user,pwd from userinfo;'
    # 拼接并执行sql语句
    cursor.execute(sql)
     
    # 取到查询结果
    ret1 = cursor.fetchone()  # 取一条
    ret2 = cursor.fetchmany(3)  # 取三条
    ret3 = cursor.fetchone()  # 取一条
     
    cursor.close()
    conn.close()
     
    print(ret1)
    print(ret2)
    print(ret3)  
    
    
    1
    2
    3
    4
    5
    6
    # 可以获取指定数量的数据
    cursor.fetchmany(3)
    # 光标按绝对位置移动1
    cursor.scroll(1, mode="absolute")
    # 光标按照相对位置(当前位置)移动1
    cursor.scroll(1, mode="relative")

     
    数据回滚
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    import pymysql
     
    # 建立连接
    conn = pymysql.connect(
        host="192.168.0.103",
        port=3306,
        user="root",
        password="123",
        database="xing",
        charset="utf8"
    )
    # 获取一个光标
    cursor = conn.cursor()
    # 定义将要执行的SQL语句
    sql1 = "insert into userinfo (user, pwd) values (%s, %s);"
    sql2 = "insert into hobby (id, hobby) values (%s,%s);"
    user = "july1"
    pwd = "july1"
    id = "我是错误的id"  #id = "3"
    hobby = "打游戏"
    try:
        # 拼接并执行SQL语句
        cursor.execute(sql1, [user, pwd])
        print(sql1)
        cursor.execute(sql2, [id, hobby])  # 报错的SQL语句
        # 涉及写操作注意要提交
        conn.commit()
    except Exception as e:
        print(str(e))
        # 有异常就回滚
        conn.rollback()
     
    # 关闭连接
    cursor.close()
    conn.close()
    
    
    
    

  • 相关阅读:
    proguard-rules.pro、混淆、导jar包
    百度地图相关开发
    开发apicloud模块遇到的几个梗
    Android相关概念
    file-downloader相关问题
    Android 线程
    Android Studio Tip of the Day
    NAudio的使用说明
    IT回忆录-2
    IT回忆录-1
  • 原文地址:https://www.cnblogs.com/baoshilin/p/13138815.html
Copyright © 2011-2022 走看看