pymysql 连接 Mysql 数据库及 简单的增删改查操作

python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作

标签: pythonmysqlpymysqlpippython增删改查
15728人阅读 评论(2) 收藏 举报
分类:

目录(?)[+]

折腾好半天的数据库连接,由于之前未安装 pip ,而且自己用的python 版本为3.6. 只能用 pymysql 来连接数据库,(如果有和我一样未安装 pip 的朋友请 点这里http://blog.csdn.net/qq_37176126/article/details/72824404  ),下边 简单介绍一下 连接的过程,以及简单的增删改查操作。

1.通过 pip 安装 pymysql

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

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

2.测试连接

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

简单的增删改查操作

示例表结构

2.1查询操作

  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插入操作

  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更新操作

  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删除操作

  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()  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于使用pymysql库进行MySQL数据库增删的基本操作。以下是一个简单的示例代码: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='your_password', db='your_database') # 创建游标对象 cursor = conn.cursor() # 插入数据 sql_insert = "INSERT INTO your_table (name, age, gender) VALUES (%s, %s, %s)" data = ('John', 25, 'male') cursor.execute(sql_insert, data) conn.commit() # 更新数据 sql_update = "UPDATE your_table SET age = %s WHERE name = %s" data = (30, 'John') cursor.execute(sql_update, data) conn.commit() # 删除数据 sql_delete = "DELETE FROM your_table WHERE name = %s" data = ('John',) cursor.execute(sql_delete, data) conn.commit() # 询数据 sql_select = "SELECT * FROM your_table WHERE name = %s" data = ('John',) cursor.execute(sql_select, data) results = cursor.fetchall() # 打印结果 for row in results: name = row[0] age = row[1] gender = row[2] print(f"name: {name}, age: {age}, gender: {gender}") # 关闭游标和连接 cursor.close() conn.close() ``` 需要注意的是,以上代码中的参数需要根据您自己的数据库配置进行修,包括数据库地址、端口、用户名、密码、数据库名、数据表名等。同时,您需要安装pymysql库,可以通过以下命令进行安装: ```bash pip install pymysql ``` 希望这能帮助到您。如果您有其他问题,欢迎随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值