Database operations pymysql packaging process (Intermediate)

 

The previous is the foundation, which is a bit simple packaging, general inquiries only used here only to write a query, the other can continue to add

code show as below:

 

Import pymysql
 from pymysql.cursors Import DictCursor 


class DBHandler (Object):
     "" " 
    initialize database 
    " "" 
    # can also be inherited Connection There is no choice to inherit 
    DEF  __init__ (Self, 
                 Host = None,   # connection name 
                 Port = 3306,   # port 
                 user None =,   # username 
                 password None =,   # password 
                 charset None =,   # can not write utf-8 utf-8 would write an error in MySQL inside 
                 database None =,   # database library name
                 = cursorClass DictCursor,
                  ** kwargs): 
        self.connect = pymysql.connect ( 
            Host = Host,   # connection name 
            Port = Port,   # port of 
            the User = the User,   # username 
            password = password,   # password 
            charset = charset,   # can not write utf-8 in MySQL written inside utf-8 will complain 
            database = database,   # database library name 
            cursorClass = cursorClass,   # data into a dictionary format 
            ** kwargs 
        ) 
        #The main object is to create a cursor ** ** 
        self.cursor = self.connect.cursor () 

    DEF query_one (Self, Query, args = None):
         "" " 
        queries a database data 
        : param query: execute MySQL statements 
        : param args: and parameters passed with a query (statement to pass parameters) tuples, lists and dictionaries 
        "" " 
        self.cursor.execute (query, args) 
        # commit changes to the database 
        self.connect.commit ()
         return self.cursor.fetchone () 

    DEF query_all (Self, query, args = None):
         "" " 
        query the database for all data 
        : param query: execute MySQL statements 
        : param args: parameter query passed along (to the statement pass argument) tuples, lists and Dictionary 
        "" "
        self.cursor.execute (Query, args) 
        # commit changes to the database 
        self.connect.commit ()
         return self.cursor.fetchall () 

    DEF Query (Self, Query, args = None, One = True):
         "" " 
        the main query data 
        : param query: execute MySQL statements 
        : param args: parameter query passed along (to the statement pass argument) tuples, lists and dictionaries 
        : param one: one is True when the execution query_one, otherwise the implementation of query_all 
        "" " 
        IF One:
             return self.query_one (Query, args)
         return self.query_all (Query, args) 

    DEF Close (Self):
         "" " 
        Close 
        : return: 
        " ""
        #Close the cursor 
        self.cursor.close ()
         # disconnect from the database 
        self.connect.close () 


IF  the __name__ == ' __main__ ' : 
    DB = DBHandler ( 
        Host = ' 127.0.0.1 ' ,   # connection name 
        Port = 3306,   # port 
        = the user ' root ' ,   # username 
        password = ' root ' ,   # password 
        charset = ' utf8 ' ,  # Can not write utf-8 write utf-8 will get an error in MySQL inside 
        Database = ' pymysql_test '   # database library name 
    )
     # query
     SQL = ' the SELECT * from in the authors ' 
    SQL1 = " the SELECT * from in the authors the WHERE AUTHORID =% S; " 
    Print (db.query (SQL, One = False))
     Print (db.query (Query = SQL1, args = [. 1]))
  # Close the connection 
  db.Close ()

Guess you like

Origin www.cnblogs.com/yongzhuang/p/12229003.html