Ps:mysqldb only available for python2.x
Python3 does not support MYSQLDB and is replaced by Pymysql
Run the report: Importerror:no module named ' MySQLdb '
ImportPymysql as PMQ#Connect (ip.user,password,dbname)con = Pmq.connect ('localhost','Root','123456','python_test')#Manipulating CursorsCur =con.cursor ()#Build TableCur.execute ("CREATE TABLE IF not EXISTS writers (Id INT PRIMARY KEY auto_increment,name VARCHAR )")#Insert Data OneCur.execute ("INSERT into writers (Name) VALUES (' Jack London ')") Cur.execute ("INSERT into writers (Name) VALUES (' Honore de Balzac ')") Cur.execute ("INSERT into writers VALUES (1, ' Jack London ')") Cur.execute ("INSERT into Writers VALUES (2, ' Honore de Balzac ')")#submit to Database-------PS: Do not write this line cannot be insertedCon.commit ()#inserting data twoSql="INSERT into writers (Name) VALUES (' Jack London2 ')"Try:#Execute SQL statementcur.execute (SQL)#commit to database executionCon.commit ()except:#Rollback If an error occursCon.rollback ()
1 #Enquiry2Cur.execute ('SELECT * FROM writers')3 4Results =Cur.fetchall ()5 6 forRowinchResults:7Id =Row[0]8Name = row[1]9 Print("id=%s,name=%s"% (Id,name))
1 #Update2Id= 13sql ="Update writers set name= ' Updatejackto ' where Id = {0}". Format (Id)4 Try:5 cur.execute (SQL)6 Con.commit ()7 except:8 Con.rollback ()9 Ten #Delete OneId=7 Asql ="Delete from writers where Id = {0}". Format (Id) - Try: - cur.execute (SQL) the Con.commit () - except: -Con.rollback ()
Python3 Connection mysql--additions and deletions change