zoukankan      html  css  js  c++  java
  • Python数据存储 — MySQL数据库操作

    本地安装MySQL

      调试环境python3.6,调试python操作mysql数据库,首先要在本地或服务器安装mysql数据库。

      安装参考:https://mp.csdn.net/postedit/80856033

    PyMysql库的安装

      在python3.6下我们使用pymysql库:点击下载PyMySQL库

      将PyMySQL-0.7.11-py2.py3-none-any.whl文件放在E:Anaconda3-5.0.1Scripts文件夹下

      先cd到目录(在cmd中输入) 
        cd Anaconda3-5.0.1Scripts
      再安装包
        pip install PyMySQL-0.7.11-py2.py3-none-any.whl

    Python中MySQL数据库操作

      安装成功后就可以编程代码实现python对mysql数据库的操作了

    #python3使用PyMySQL操作MySQL
    print("================MySQL数据库=================")
    
    import pymysql.cursors
    
    # 连接数据库
    connect = pymysql.Connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        passwd='123456',
        db='note',
        charset='utf8'
    )
    
    # 获取游标
    cursor = connect.cursor()
    
    #删除表
    sql = 'DROP TABLE IF EXISTS student'
    cursor.execute(sql)
    connect.commit()
    print('如果存在表就删除表格')
    
    #创建表格
    sql = "CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)"
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        print("表已存在")
    print('成功创建表格')
    
    # 插入数据
    sql = "INSERT INTO student VALUES(%d,'%s')"
    data = (1, 'student1')
    cursor.execute(sql % data)
    connect.commit()
    print('成功插入', cursor.rowcount, '条数据')
    
    # 修改数据
    sql = "UPDATE student SET name = '%s' WHERE id = %d "
    data = ('student2', 1)
    cursor.execute(sql % data)
    connect.commit()
    print('成功修改', cursor.rowcount, '条数据')
    
    # 查询数据
    sql = "SELECT * FROM student WHERE id=%d"
    data = (1,)
    cursor.execute(sql % data)
    for row in cursor.fetchall():
        print("%s" % str(row))
    print('共查找出', cursor.rowcount, '条数据')
    
    # 删除数据
    sql = "DELETE FROM student WHERE id = %d LIMIT %d"
    data = (1, 1)
    cursor.execute(sql % data)
    connect.commit()
    print('成功删除', cursor.rowcount, '条数据')
    
    # 事务处理
    sql_1 = "UPDATE student SET name = name + '1' WHERE id = 1 "
    
    try:
        cursor.execute(sql_1)
    except Exception as e:
        connect.rollback()  # 事务回滚
        print('事务处理失败', e)
    else:
        connect.commit()  # 事务提交
        print('事务处理成功', cursor.rowcount)
    
    # 关闭连接
    cursor.close()
    connect.close()
    
    
    pymysql.Connect()参数说明
    参数说明
    host(str) MySQL服务器地址                            
    port(int) MySQL服务器端口号
    user(str) 用户名
    passwd(str) 密码
    db(str) 数据库名称
    charset(str)

    连接编码

    connection对象支持的方法
    方法说明
    cursor() 使用该连接创建并返回游标       
    commint() 提交当前事务
    rollback() 回滚当前事务
    close() 关闭连接
    cursor对象支持的方法
    方法说明
    execute(op) 执行一个数据库的查询命令                                
    fetchone() 取得结果集的下一行
    fetchmany(size) 获取结果集的下几行
    fetchall() 获取结果集中的所有行
    rowcount() 返回数据条数或影响条数
    close() 关闭游标对象

     

  • 相关阅读:
    工作中遇到的令人头疼的bug
    Cookie的简单用法
    C#之#if #endif的简单用法
    我们一起学习WCF 第十篇Wcf中实现事务
    一次性搞定Session
    设计模式-观察者模式
    类的扩展之 DataReader的扩展
    C#之Ref,Out以及TryParse()的用法
    C#之Lambda不得不说的用法
    C#之Action和Func的用法
  • 原文地址:https://www.cnblogs.com/lsqin/p/9342927.html
Copyright © 2011-2022 走看看