ImportPymysqlImportDatainfoImport Time#Get ParametersHost=Datainfo.hostusername=Datainfo.usernamepassword=Datainfo.passworddatabase=datainfo.dbPrint()#Test Database Connectiondeftestconnect ():#Open Database LinkDB=Pymysql.connect (host,username,password,database)#Create a Cursor object using the cursor () method cursorcursor=db.cursor ()#Execute SQL query using the Execute () methodCursor.execute ("Select version ()") #using Fetchone () to get a single piece of dataData=Cursor.fetchone ()Print(data) db.close ()#Insert Databasedefinsertdate ():#Open Database LinkDB= Pymysql.connect (host,username,password,database,charset='UTF8') #Create a Cursor object using the cursor () method cursorcursor=db.cursor () create_time= Time.strftime ('%y-%m-%d%h:%m:%s') Update_time= Time.strftime ('%y-%m-%d%h:%m:%s') Start_time= Time.strftime ('%y-%m-%d%h:%m:%s') End_time= Time.strftime ('%y-%m-%d%h:%m:%s') Remark="Test Insert Information" Print("Start") #SQL INSERT statementsql ="INSERT INTO demo (Start_time,end_time,creat_time,update_time,remark)" "VALUES ('%s ', ' %s ', '%s ', '%s ', '%s ')" %(Start_time,end_time,create_time,update_time,remark)Try: #Execute SQL Print("Perform an Insert") TT=cursor.execute (SQL)Print(TT) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs Print(e) db.rollback () Db.close ( )#Query Operationsdefselectdata (): DB= Pymysql.connect (host, username, password, database, charset='UTF8') #Create a Cursor object using the cursor () method cursorcursor=db.cursor () SQL="SELECT * FROM demo where ID >= '%d '"% (1) Try: #Execute SQL Print("Execute Query") cursor.execute (SQL) Results=Cursor.fetchall () forRowinchResults:id=Row[0] Start_time= Row[1] End_time= Row[2] Create_time= Row[3] Update_time= Row[4] Remark= Row[5] #Print Results Print("id =%d,start_time=%s,end_time=%s,create_time=%s,update_time=%s,remark=%s"%(Id,start_time,end_time,create_time,update_time,remark)) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs Print(e) db.close ()#Update Actiondefupdate_data (): DB= Pymysql.connect (host, username, password, database, charset='UTF8') #Create a Cursor object using the cursor () method cursorcursor=db.cursor () update_time= Time.strftime ('%y-%m-%d%h:%m:%s') SQL="Update demo Set update_time = '%s ' where ID >= '%d '"% (update_time,1) Try: #Execute SQL Print("Perform the update") cursor.execute (SQL) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs Print(e) db.rollback () Db.close ( )#Delete Operationdefdelete_date (): DB= Pymysql.connect (host, username, password, database, charset='UTF8') #Create a Cursor object using the cursor () method cursorcursor=db.cursor () SQL="Delete from demo where ID < '%d '"% (1) Try: #Execute SQL Print("Perform the delete") cursor.execute (SQL) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs Print(e) db.rollback () Db.close ( )if __name__=='__main__': Testconnect () insertdate () Selectdata () Update_data () delete_date ()
Python 3 Mysql Additions and deletions