Python MYSQL Operation class

Source: Internet
Author: User
Tags python mysql

Python 2.7

Only so much for the time being, the other supplements will be used later.

#-*-Coding:utf-8-*-import mysqldbimport time ' · MySQL Operation Class · V1.0 "Class Mysqlclass (object): Def __init__ (self,host,user,password,charset=" UTF8 "): Super (Mysqlclass, self). _ _init__ () self.host=host self.user=user Self.password=password self.port=3307 # I'm here MySQL port is 33 07, Default 3306 self.charset=charset try:self.conn=mysqldb.connect (HOST=SELF.HOST,USER=SELF.USER,PASSW D=self.password,port=self.port) Self.conn.set_character_set (self.charset) self.cur=self.conn.cursor ( ) except Mysqldb.error as E:self.printerror (E, ') def getTime (self): return time.st Rftime ("%y-%m-%d%h:%m:%s", Time.localtime (Time.time ())) def printerror (self,e,sql): print ("{0} MySQl Error {1}: {2} ". Format (Self.gettime (), e.args[0],e.args[1]) print (SQL) def setdb (self,db): Try:self.conn  . select_db (db) except Mysqldb.error as E:self.printerror (E, ')  #新增 def insert (self,tablename,dicdata): try:arrparam=[] arrvalue=[] for key In DicData:arrParam.append ('%s ') arrvalue.append (Dicdata[key]) strfield= ' {0} '. Format (",". Join (Dicdata.keys ()))) strparam= ' values ({0}) '. Format (', '. Join (Arrparam)) sql= ' Insert int o {0} ({1}) {2}; '.        Format (Tablename,strfield,strparam) Self.cur.execute (Sql,tuple (Arrvalue)) # monotonic new Self.conn.commit () Except Mysqldb.error as E:self.printerror (e,sql) #self. Conn.close () #批量新增 def insertbatch (            Self,tablename,arrfiled,arrtuplevalue): Try:strfield = ' {0} '. Format (', '. Join (arrfiled)) Arrparam=[] for x in ArrFiled:arrParam.append ('%s ') Strparam = ' values ({0}) '. Format ( ', '. Join (arrparam)) sql = ' insert INTO {0} ({1}) {2}; '. Format (tablename, Strfield, Strparam) self.cur.executEmany (Sql,tuple (arrtuplevalue)) #many many new Self.conn.commit () except Mysqldb.error as E:self             . Printerror (E, ') #self. Conn.close () #更新语句 def update (self,sql): Try:self.cur.execute (SQL) Self.conn.commit () except Mysqldb.error as E:self.conn.rollback () Self.printerro            R (E,sql) #self. Conn.close () #查询单个字段 back to List def executelist (self,sql): try:result=[] Self.cur.execute (SQL) Ds=self.cur.fetchall () for I,row in Enumerate (DS): result. Append (str (row[0))) return result except Mysqldb.error as E:self.printerror (e,sql) DB=MYSQLCL (' 127.0.0.1 ', ' root ', ' 123456 ') db.setdb (' Test ') # arr=db.executelist (' SELECT DISTINCT skuid from Commodity ') # for X in arr:# print (x) # Table=db.insert (' commodity ', {' title ': ' Ten '}) # Db.conn.commit () # arrtr={' title ': ' 1 '}# if ' titl1e ' in arr tr:# print (1) # else:#    Print (0) arrfiled = [' name ', ' Age ']arrvalue=[' 1 ', ' 2 ']arrvalues=[]arrvalues.append ([1, ' 1 ']) db.insertbatch (' Test ', Arrfiled,arrvalues)

Python MYSQL Action class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.