Packaging tools Python3 JDBC (first package to be improved)

Tool requirements:
(1) In order to adapt the operation of the multi-connection multiple databases, connected with the multi-database operation, the same connection, the operation in the plurality of databases. 

Buyer design:
the location parameter database is arranged, connected to the port, the password set to the default parameters , account normally root write dead. As a result, each operation is generally passed to the database name, and then add parameters to override the default values are subject to change


Import
pymysql class Util (Object): '' ' to initialize the database connection, note that each connection obtained, their address is not the same, so reservations first return address and then return to get a cursor, the cursor objects directly connected and write _ _init__ which function mainly to ensure that two objects can be instantiated to be completed after the operation with the address database current defects: unable to complete the insertion of the test batch of data, the pre-compiler related unresolved temporarily '' ' DEF the __init__ ( Self, Database, Host = ' 127.0.0.1 ' , Port = 3306, password = ' 23 is ' ): self.conn = pymysql.connect (= Host Host, Port = Port, User = ' the root ' , password = password, Database = Database ) self.cur = self.conn.cursor () # get a cursor, throughout the lifetime of the object using the process, if not written in the initialization inside, separate from the connection object, then each address will different # increase DEF iNSERT (Self, SQL): rows = self.cur.execute (SQL) self.conn.commit () Print ( ' inserted in the data successfully, the affected rows: {} ' .format (rows)) # delete DEF the delete (Self, SQL): rows = self.cur.execute (SQL) self.conn.comit () Print ( ' deletion of data is successful, the affected line: {} ' .format (rows)) # inquiry last record DEF find_one (Self, SQL): self.cur.execute (SQL) DATAS = self.cur.fetchone () Print (DATAS) # pure display return DATAS # query multiple DEF find_all (Self, sql): rows = self.cur.execute (sql) # execute sql statement it returns rows affected number DATAS self.cur.fetchall = () # remove all data Print ( ' has to query of data {} '.format (rows)) for I in DATAS: Print(I) return DATAS DEF Modify (Self, SQL): rows = self.cur.execute (SQL) self.conn.commit () Print ( ' {} of data has been updated ' .format (rows)) return rows # released and connection resources cursors DEF Release (Self): self.conn.close () self.cur.close () IF the __name__ == ' __main__ ' : Test = Util ( 'test1 ' )

 

Guess you like

Origin www.cnblogs.com/zhongzhouyun/p/11223624.html