首页 > 数据库 >pymysql 连接、关闭、查询数据库代码

pymysql 连接、关闭、查询数据库代码

时间:2022-10-16 19:57:42浏览次数:45  
标签:return sql pymysql cursor 关闭 查询数据库 close conn

 1 def get_conn():
 2     """
 3     :return: 连接,游标
 4     """
 5     # 创建连接
 6     conn = pymysql.connect(host="127.0.0.1",
 7                     user="root",
 8                     password="你的数据库密码",
 9                     db="要使用的数据名称",
10                     charset="utf8")
11     # 创建游标
12     cursor = conn.cursor()  # 执行完毕返回的结果集默认以元组显示
13     return conn, cursor
14 
15 def close_conn(conn, cursor):
16     if cursor:
17         cursor.close()
18     if conn:
19         conn.close()
20 """
21 -----------------------------------------------------------
22 """
23 """
24 ------------------------------------------------------------------------------------
25 """
26 def query(sql,*args):
27     """
28     通用封装查询
29     :param sql:
30     :param args:
31     :return:返回查询结果 ((),())
32     """
33     conn , cursor= get_conn()
34     print(sql)
35     cursor.execute(sql)
36     res = cursor.fetchall()
37     close_conn(conn , cursor)
38     return res

 

标签:return,sql,pymysql,cursor,关闭,查询数据库,close,conn
From: https://www.cnblogs.com/rainbow-1/p/16796906.html

相关文章

  • xp_cmdshell的启用与关闭
    1、xp_cmdshell简介:xp_cmdshell扩展存储过程将命令字符串作为操作系统命令shell执行,并以文本行的形式返回所有输出。一般情况下默认处于关闭状态  2、开启xp_cmd......
  • 定时启动及关闭IIS服务
     关闭IIS服务.bat (注意:文本格式为ANSI)@echoofftaskkill/f/imw3wp.exeiisreset/STOPtaskkill/f/imw3wp.exe开启IIS服务.bat (注意:文本格式为ANSI)@ec......
  • 关闭VirtualBox虚拟机的时钟同步
    在VirtualBox的虚拟机上默认虚拟机的时间是会和物理机同步的,但可以通过下面的命令来关闭1.首先查看虚拟机列表VBoxManagelistvms2.使用下面的命令来关闭虚拟机的时......
  • Kafka暴力关闭导致日志被锁
    ERRORShutdownbrokerbecausealllogdirsind:\work\data\kafka-logshavefailed现象:在windows的命令行里启动kafka之后,当关闭命令行窗口时,就会强制关闭kafka。这种......
  • pymysql封装
    1importpymysql2fromconfigs.global_dataimportMysql3frompymysql.cursorsimportDictCursor456classMysqlHandler:78def__i......
  • uni-app 4.13开发弹出层组件(二)弹出关闭功能
    index.nuve页面<template><viewclass=""><free-nav-bar:title="'微信'":noreadnum="1":fixed='true'@openExtend="openExtend"><templatev-slot="title"......
  • Element 关闭图标区分关闭、取消、ESC
    使用distinguishCancelAndClose属性关闭:close取消:cacelESC:closeElement.MessageBox.confirm('是否要离开?','提示',{confirmButtonText:'......
  • js关闭二维码
    关闭二维码<style>.box{width:300px;height:100px;margin:100pxauto;position:relative;......
  • Gitlab关闭停用其他不需要的组件
    默认运行的组件[root@gitlabopt]#gitlab-ctlstatusrun:alertmanager:(pid3474)1289193s;run:log:(pid1915)1260609srun:gitaly:(pid3581)1289192s;ru......
  • ASP中查询数据库记录写入XML文件示例
    把下面代码保存为Asp_XML.asp运行即可:<%'By Dicky 2005-03-22 21:52:18 AM QQ:25941 E-mail:[email protected] IsSql = ......