zoukankan      html  css  js  c++  java
  • python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作

    1.通过 pip 安装 pymysql

    进入 cmd  输入  pip install pymysql  
    回车等待安装完成;

    安装完成后出现如图相关信息,表示安装成功。

    2.测试连接

    import pymysql  #导入 pymysql ,如果编译未出错,即表示 pymysql 安装成功

    简单的增删改查操作

    示例表结构

    2.1查询操作

    [python] view plain copy
    1. import pymysql  #导入 pymysql  
    2.   
    3. #打开数据库连接  
    4. db= pymysql.connect(host="localhost",user="root",  
    5.     password="123456",db="test",port=3307)  
    6.   
    7. # 使用cursor()方法获取操作游标  
    8. cur = db.cursor()  
    9.   
    10. #1.查询操作  
    11. # 编写sql 查询语句  user 对应我的表名  
    12. sql = "select * from user"  
    13. try:  
    14.     cur.execute(sql)    #执行sql语句  
    15.   
    16.     results = cur.fetchall()    #获取查询的所有记录  
    17.     print("id","name","password")  
    18.     #遍历结果  
    19.     for row in results :  
    20.         id = row[0]  
    21.         name = row[1]  
    22.         password = row[2]  
    23.         print(id,name,password)  
    24. except Exception as e:  
    25.     raise e  
    26. finally:  
    27.     db.close()  #关闭连接  

    2.2插入操作

    [python] view plain copy
    1. import pymysql  
    2. #2.插入操作  
    3. db= pymysql.connect(host="localhost",user="root",  
    4.     password="123456",db="test",port=3307)  
    5.   
    6. # 使用cursor()方法获取操作游标  
    7. cur = db.cursor()  
    8.   
    9. sql_insert ="""insert into user(id,username,password) values(4,'liu','1234')"""  
    10.   
    11. try:  
    12.     cur.execute(sql_insert)  
    13.     #提交  
    14.     db.commit()  
    15. except Exception as e:  
    16.     #错误回滚  
    17.     db.rollback()   
    18. finally:  
    19.     db.close()  

    2.3更新操作

    [python] view plain copy
    1. import pymysql  
    2. #3.更新操作  
    3. db= pymysql.connect(host="localhost",user="root",  
    4.     password="123456",db="test",port=3307)  
    5.   
    6. # 使用cursor()方法获取操作游标  
    7. cur = db.cursor()  
    8.   
    9. sql_update ="update user set username = '%s' where id = %d"  
    10.   
    11. try:  
    12.     cur.execute(sql_update % ("xiongda",3))  #像sql语句传递参数  
    13.     #提交  
    14.     db.commit()  
    15. except Exception as e:  
    16.     #错误回滚  
    17.     db.rollback()   
    18. finally:  
    19.     db.close()  

    2.4删除操作

    [python] view plain copy
    1. import pymysql  
    2. #4.删除操作  
    3. db= pymysql.connect(host="localhost",user="root",  
    4.     password="123456",db="test",port=3307)  
    5.   
    6. # 使用cursor()方法获取操作游标  
    7. cur = db.cursor()  
    8.   
    9. sql_delete ="delete from user where id = %d"  
    10.   
    11. try:  
    12.     cur.execute(sql_delete % (3))  #像sql语句传递参数  
    13.     #提交  
    14.     db.commit()  
    15. except Exception as e:  
    16.     #错误回滚  
    17.     db.rollback()   
    18. finally:  
    19.     db.close() 
  • 相关阅读:
    Chapter 03Using SingleRow Functions to Customize Output(03)
    Chapter 03Using SingleRow Functions to Customize Output(01)
    Chapter 04Using Conversion Functions and Conditional ExpressionsNesting Functions
    Chapter 04Using Conversion Functions and Conditional ExpressionsGeneral Functions
    Chapter 11Creating Other Schema Objects Index
    传奇程序员John Carmack 访谈实录 (zz.is2120)
    保持简单纪念丹尼斯里奇(Dennis Ritchie) (zz.is2120.BG57IV3)
    王江民:传奇一生 (zz.is2120)
    2011台湾游日月潭
    2011台湾游星云大师的佛光寺
  • 原文地址:https://www.cnblogs.com/navysummer/p/8449191.html
Copyright © 2011-2022 走看看