鱼C论坛

 找回密码
 立即注册
查看: 163|回复: 2

请问各位:数据库数据要怎么用python提取?

[复制链接]
发表于 2024-3-15 10:42:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问各位:MySql数据库数据要怎么用python提取?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-15 16:15:15 | 显示全部楼层
一段封装的mysql代码,可以借鉴下

  1. import pymysql


  2. class MySqlModel(object):
  3.     def __init__(self, host, user, password, dbName):
  4.         self.host = host
  5.         self.user = user
  6.         self.password = password
  7.         self.dbName = dbName

  8.     def connet(self):
  9.         self.db = pymysql.connect(self.host, self.user, self.password, self.dbName)
  10.         self.cursor = self.db.cursor()

  11.     def close(self):
  12.         self.cursor.close()
  13.         self.db.close()

  14.     def get_on(self, sql):
  15.         """
  16.         instance_sql:说明
  17.             从数据的table_name表,选择numeric字段最小的一条数据
  18.         """
  19.         instance_sql = f"SELECT MIN(numeric) FROM table_name ;"
  20.         res = None
  21.         try:
  22.             self.connet()
  23.             self.cursor.execute(sql)
  24.             res = self.cursor.fetchone()
  25.             self.close()
  26.         except:
  27.             print("查询失败")
  28.         return res

  29.     def get_all(self, sql):
  30.         res = ()
  31.         try:
  32.             self.connet()
  33.             self.cursor.execute(sql)
  34.             res = self.cursor.fetchall()
  35.             self.close()
  36.         except:
  37.             print("查询失败")
  38.         return res

  39.     def insert(self, sql):
  40.         """
  41.         instance_sql:说明
  42.             插入一条value数据到table_name表中
  43.         """
  44.         value = (0, "ein", "tom", 10, 0)
  45.         table_name = "ILOVEFISHC"
  46.         instance_sql = f"insert into {table_name} values {value};"
  47.         return self._deit(sql)

  48.     def update(self, sql):
  49.         """
  50.         update 表名 set 字段=新值,… where 条件
  51.         instance_sql说明:
  52.             更新table_name表的numeric字段,条件是numeric等于result
  53.         """
  54.         numeric = ""
  55.         result = "6"
  56.         instance_sql = f"update table_name set numeric ={numeric} where numeric ={result};"
  57.         return self._deit(sql)

  58.     def delete(self, sql):
  59.         return self._deit(sql)

  60.     def create(self, sql):
  61.         return self._deit(sql)

  62.     def _deit(self, sql):
  63.         count = 0
  64.         try:
  65.             self.connet()
  66.             count = self.cursor.execute(sql)
  67.             self.db.commit()
  68.             self.close()
  69.         except:
  70.             print("事务提交失败")
  71.             self.db.rollback()
  72.         return count
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-15 16:34:52 | 显示全部楼层
Stubborn 发表于 2024-3-15 16:15
一段封装的mysql代码,可以借鉴下

谢谢~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 06:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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