Simple encapsulation code instance of pymysql

Time:2021-5-31

This article mainly introduces the simple encapsulation code example of pymysql. The example code introduced in this article is very detailed, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

#coding=utf-8 
#!/usr/bin/python

import pymysql


class MYSQL:
  """
  Simple encapsulation of pymysql
  """
  def __init__(self,host,user,pwd,db):
    self.host = host
    self.user = user
    self.pwd = pwd
    self.db = db

  def __GetConnect(self):
    """
    Get connection information
    Return: conn.cursor ()
    """
    if not self.db:
      Raise (NameError, "no database information set")
    self.conn = pymysql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
    cur = self.conn.cursor()
    if not cur:
      Raise (NameError, failed to connect to database)
    else:
      return cur

  def ExecQuery(self,sql):
    """
    Execute query statement
    What is returned is a list containing tuples. The element of list is the record line, and the element of tuple is the field of each record line

    Call example:
        ms = MYSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
        resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
        for (id,NickName) in resList:
          print str(id),NickName
    """
    cur = self.__GetConnect()
    cur.execute(sql)
    resList = cur.fetchall()

    #The connection must be closed after the query
    self.conn.close()
    return resList

  def ExecNonQuery(self,sql):
    """
    Execute non query statements

    Call example:
      cur = self.__GetConnect()
      cur.execute(sql)
      self.conn.commit()
      self.conn.close()
    """
    cur = self.__GetConnect()
    cur.execute(sql)
    self.conn.commit()
    self.conn.close()

def main():

  mysql = MYSQL(host="192.168.163.36",user="wisdomhr",pwd="wisdomhr",db="WISDOMHR")
  resList = mysql.ExecQuery("SELECT CITY FROM RES_SCHOOL")
  for inst in resList:
    print(inst)
if __name__ == '__main__':
  main()

The usage is as follows:


#!/usr/bin/python
#version 3.4
import wispymysql
mysql = wispymysql.MYSQL(host="192.168.163.36",user="wisdomhr",pwd="wisdomhr",db="WISDOMHR")
selectsql = "SELECT ID, CITY FROM RES_SCHOOL WHERE CITY LIKE '%\r\n%'"
result = mysql.ExecQuery(selectsql)

for (dbid, city) in result:
  rightcity = city.replace('\r\n','')
  updatesql= "UPDATE RES_SCHOOL SET CITY = '" + rightcity + "' WHERE ID = " + str(dbid)
  print(updatesql)
  mysql.ExecNonQuery(updatesql)

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Build HTTP service with C + + Mongoose

Mongoose source code address:https://github.com/cesanta/mo… Mongoose user manual:https://www.cesanta.com/devel… Mngoose set up HTTP service #include <string> #include “mongoose.h” using namespace std; static const char *s_http_port = “8000”; static void ev_handler(mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; if (ev == MG_EV_HTTP_REQUEST) { std::string uri; if (hm->uri.p && hm->uri.p[0] == ‘/’) […]