首页 > 数据库 >(python) 数据库一次 Connection 连接,不同 cursor

(python) 数据库一次 Connection 连接,不同 cursor

时间:2023-05-08 16:48:42浏览次数:41  
标签:__ Database python xxx connection cursor Connection

数据库一次 Connection 连接,不同 cursor 的最简洁代码:

import pymysql


class Database(object):
    connection = None

    def __init__(self):
        if not Database.connection:
            Database.connection = pymysql.connect(host="127.0.0.1",
                                                  port=xxx,
                                                  user="xxx",
                                                  password="xxx",
                                                  database="dbtest")

    def query(self, sql):
        cursor = Database.connection.cursor()
        cursor.execute(sql)

if __name__ == '__main__': db = Database() db.query()

标签:__,Database,python,xxx,connection,cursor,Connection
From: https://www.cnblogs.com/bruce-he/p/17382222.html

相关文章

  • Python语言进行代码换行常用的方法详解!
    在编写Python代码时,有效换行可以让代码变得更加美观,那么Python中如何进行代码换行?Python中换行的方法有很多,本文为大家介绍两种比较常用的方法,快来看看吧。Python建议每行代码的长度不超过80个字符。对于过长的代码,建议进行换行。1、在该行代码末尾加上续行符“\”,即......
  • python时间相关
    目录timedatetime参考博客timetime—Timeaccessandconversions—Python3.10.0documentationimporttime#1970-01-0100:00:00到现在经过了多少秒print(time.time())#返回一个时间元组print(time.localtime())#时间转字符串print(time.strftime('%Y-%m-%d......
  • python Django校验表单登录案例
    定义一个视图函数,用于处理登录表单的提交动作。在该视图函数中,使用request.POST.get()方法获取POST请求中提交的用户名和密码数据,具体代码如下:fromdjango.shortcutsimportrender,redirectfromdjango.contrib.authimportauthenticate,logindeflogin_view(requ......
  • python 自动化测试案例
    使用Selenium与unittest框架的自动化测试案例,可以用来模拟浏览器自动打开网站并提交表单:在下述代码中,我们定义了一个继承自unittest.TestCase的测试类TestLoginForm,其中setUp()方法在每个测试方法执行前会调用,用来启动浏览器并打开测试用网站。tearDown()方法在每个测......
  • python selenium 谷歌浏览器指定下载位置
    pythonselenium谷歌浏览器指定下载位置功能pythonselenium点击浏览器的下载按钮,然后下载到指定的目录,而非下载到默认位置代码importos.pathfromtimeimportsleepfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy"""指定默认下载路径,......
  • Python处理文档中的文本
    提取所有Word文档中的指定部分内容,并合并到一个txt文件中:importglobimportdocxdefget_text_between_headings(doc,heading1,heading2):start=-1end=-1foriinrange(len(doc.paragraphs)):ifdoc.paragraphs[i].text==heading1:......
  • C++和Python && 和||运算符之不同点
    ......
  • The connection to the server localhost:8080 was refused - did you specify the ri
    遇到如下问题:[root@k8s-node1~]#kubectlgetpodTheconnectiontotheserverlocalhost:8080wasrefused-didyouspecifytherighthostorport?解决方式:cd/etc/kubernetes/查看到有个文件:kubelet.conf(你们的有可能是admin.conf)执行命令echo"exportKUBECONFIG......
  • How to use Linux command or Python code get Raspberry PI's Temperature All In On
    HowtouseLinuxcommandorPythoncodegetRaspberryPI'sTemperatureAllInOne如何使用Linux命令或Python代码获取RaspberryPI的温度raspberrypicheckthetemperatureimportgpiozeroasgz#readthetemperatureintoavariable:cpu_temp=gz.CPUTe......
  • python 下载并显示图片
    fromPILimportImage,ImageTkimportrequestsimporttkinter#导入Tkinter模块#importtkpaa='aaa.gif'url='http://www.baidu.com/img/baidu_jgylogo3.gif'defdownLoadImage(name,url):r=requests.get(url)#printr.c......