找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 2009|回复: 5

[零碎知识点] python操作MySQL的一些知识点

[复制链接]

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
发表于 2022-6-19 16:38:40 | 显示全部楼层 |阅读模式
一、建立连接
  1. import pymysql
  2. host = 'localhost'
  3. port = 3308
  4. user = 'root'
  5. psd = '123456'
  6. db = 'faren'
  7. charset = 'utf8'
  8. db = pymysql.Connect(host=host,port=port,user=user,passwd=psd,db=db,charset=charset)
  9. print('OK--连接成功')
  10. db.close()   #关闭数据库连接,不然会一直占用计算机资源
复制代码
如果能正常打印连接成功,就代表已经连接上数据库了,不然会报错,可以试着修改前几行的信息,如果有错,运行程序会报错。



回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
 楼主| 发表于 2022-6-19 16:47:05 | 显示全部楼层
二、python创建数据库表
  1. import pymysql
  2. host = 'localhost'
  3. port = 3308
  4. user = 'root'
  5. psd = '123456'
  6. db = 'faren'
  7. charset = 'utf8'
  8. db = pymysql.Connect(host=host,port=port,user=user,passwd=psd,db=db,charset=charset)
  9. print('OK')

  10. cursor = db.cursor()   #创建游标对象,用于数据库查询结果以及执行SQL语句
  11. sql = 'CREATE TABLE test(id int,name varchar(10),age int);'
  12. cursor.execute(sql)   #执行sql语句
  13. db.commit()    #提交
  14. cursor.close()    #关闭游标对象

  15. db.close()
复制代码
创建效果如下图:
2022-06-19_164533.png

回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
 楼主| 发表于 2022-6-19 16:55:07 | 显示全部楼层
三、插入单条数据
  1. import pymysql
  2. host = 'localhost'
  3. port = 3308
  4. user = 'root'
  5. psd = '123456'
  6. db = 'faren'
  7. charset = 'utf8'
  8. db = pymysql.Connect(host=host,port=port,user=user,passwd=psd,db=db,charset=charset)
  9. print('OK')

  10. cursor = db.cursor()    #创建游标对象,用于数据库查询结果以及执行SQL语句
  11. sql = "insert into test values(1,'张三',18);"
  12. cursor.execute(sql)    #执行sql语句
  13. db.commit()         #提交
  14. cursor.close()       #关闭游标对象

  15. db.close()
复制代码
向数据表中插入了一条数据,效果如下图:
2022-06-19_165337.png

回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
 楼主| 发表于 2022-6-19 17:13:48 | 显示全部楼层
四、用pandas读取excel内容,批量写入MySQL数据库
2022-06-19_171147.png
excel表格格式和数据库一致,读取的时候忽略表头
  1. import pandas as pd
  2. df = pd.read_excel("001.xlsx", engine="openpyxl", sheet_name="Sheet1")
  3. import pymysql
  4. host = 'localhost'
  5. port = 3308
  6. user = 'root'
  7. psd = '123456'
  8. db = 'faren'
  9. charset = 'utf8'
  10. db = pymysql.Connect(host=host,port=port,user=user,passwd=psd,db=db,charset=charset)
  11. print('OK')
  12. cursor = db.cursor()
  13. for i in range(0,len(df)):
  14.     row_data = df.loc[[i]].values
  15.     for cell in row_data:
  16.         id = cell[0]
  17.         name = cell[1]
  18.         age = cell[2]
  19.     sql = "insert into test values(" + str(id) + ",'" + name + "'," + str(age) + ");"
  20.     cursor.execute(sql)
  21.     db.commit()
  22. cursor.close()
  23. db.close()
复制代码
完成后,查看数据库,如下图:
2022-06-19_171319.png

回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
 楼主| 发表于 2022-6-19 18:04:27 | 显示全部楼层
四、几个简单的sql语句的执行
删除符合条件的数据
delete from test where id = 1   
delete from test where  name = '星期一'   

修改符合条件的数据
update test set age =age+2 where name ='星期二'   
update test set name = '周末' where name = '星期日'   

删除全部
delete from test


回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
 楼主| 发表于 2022-6-19 18:16:25 | 显示全部楼层
五、查询数据库内容,打印出来


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2024-4-20 21:36 , Processed in 0.122919 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表