Play a game of your own. Packaged MySQL

Source: Internet
Author: User
Tags stmt

#Coding:utf-8ImportMySQLdbImportSYSReload (SYS) sys.setdefaultencoding ('Utf-8')classMysqldbhelper:#Get database connection    defGetcon (self):Try: Conn=mysqldb.connect (host= 'localhost ', user= ' admin ', passwd= ' admin ', DB=test ', Port=3306, CharSet= ' UTF8 ')            returnConnexceptmysqldb.error,e:Print "MySQLdb error:%s"%e#Query method, using Con.cursor (MySQLdb.cursors.DictCursor), returns the result as a dictionary    defInput_select (Self,sql):#entering SQL statements manually        Try: Con=Self.getcon () cur=con.cursor (MySQLdb.cursors.DictCursor) cur.execute (SQL) FC=Cur.fetchall ()returnFCexceptmysqldb.error,e:Print "MySQLdb error:%s"%efinally: Cur.close ()defParameter_select (self,sql,params):Try: Con=Self.getcon () cur=con.cursor (MySQLdb.cursors.DictCursor) Cur.execute (sql,params) FC=Cur.fetchall ()returnFCexceptmysqldb.error,e:Print "Mysql Error%s"%efinally: Cur.close ()#updated method with parameters, eg:sql= ' insert into pythontest values (%s,%s,%s,now () ', params= (6, ' C # ', ' Good book ')    defUpdatebyparam (self,sql,params):" "cursor.execute ("select * from t1 WHERE id =%s", (5,))" "        Try: Con=Self.getcon () cur=Con.cursor (MySQLdb.cursors.DictCursor) Count=Cur.execute (Sql,params) con.commit ()returnCountexceptMysqldb.error,e:con.rollback ()Print "MySQLdb error:%s"%efinally: Cur.close () con.close ()#Update method with no parameters    defUpdate (SELF,SQL):Try: Con=Self.getcon () cur=con.cursor () Count=cur.execute (SQL) Con.commit ()returnCountexceptMysqldb.error,e:con.rollback ()Print "MySQLdb error:%s"%efinally: Cur.close () con.close ()#Delete Data    defdelete_information (self,sql):Try: Con=Self.getcon () cur=con.cursor () cur.execute (SQL) Cur.commit ()exceptMysqldb.error,e:cur.rollback ()Print "Mysql Error%s"%efinally: Cur.close ()defDelete_infor_param (self,sql,params):Try: Con=Self.getcon () cur=con.cursor () cur.execute (sql,params) cur.commit ()exceptMysqldb.error,e:cur.rollback ()Print "Mysql Error%s"%efinally: Cur.close ()
  
def circulation_update (SELF,SQL,PARAMS,PARAMS1):
"' Multi-parameter SQL statement
data = [
(' Jane ', ' 555-001 '),
(' Joe ', ' 555-001 '),
(' John ', ' 555-003 ')
]
stmt = "INSERT into employees (name, phone) VALUES ('%s ', '%s ')"
Cursor.executemany (stmt, data)
‘‘‘
Try
con = Self.getcon ()
cur = con.cursor ()
values = []
For I in range (1):
Values.append (params)
Values.append (PARAMS1)
Cur.executemany (Sql,tuple (values))
Cur.commit ()
Except Mysqldb.error,e:
Cur.rollback ()
Print "Mysql Error%d:%s"% (E.args[0],e.args[1])
if __name__=="__main__":
A=mysqldbhelper () SQL="SELECT * from phoneuser WHERE account =%s" #sql= "SELECT * from phoneuser WHERE account = ' [email protected] '"
Par ='[email protected]'
#fd=a.select (SQL)
FD =A.updatebyparam (Sql,par)

PrintFD

#For row in FD:

#Print Row #example of a loop insert

#value=[1, ' Hi Rollen ']

#Cur.execute (' INSERT into test values (%s,%s) ', value) #

#values=[]

#For i in range:

#values.append (i, ' Hi Rollen ' +str (i)))

#Cur.executemany (' INSERT into test values (%s,%s) ', values)

#cur.execute (' Update test set info= ' I am Rollen "where id=3 ')

#Conn.commit ()

#cur.close ()

#conn.close ()

Where there is something wrong, you can point it out.

Play a game of your own. Packaged MySQL

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.