Import pymysql # Open Database Connectivity DB = pymysql.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " ) # using the cursor () method to create a cursor object Cursor Cursor = db.cursor () # use execute ( ) method executes the SQL query the cursor.execute ( " the SELECT VERSION () " ) # use either fetchone () method to obtain a single data. data = cursor.fetchone () Print ( " Database Version:% S " % data) # Close the database connection db.close ()
Import pymysql # Open Database Connectivity DB = pymysql.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " ) # using the cursor () method to create a cursor object Cursor Cursor = db.cursor () # use execute ( ) method executes SQL, if table exists delete cursor.execute ( " DROP tABLE IF eXISTS the EMPLOYEE " ) # create a table using a prepared statement SQL = "" " the cREATE tABLE the EMPLOYEE ( FIRST_NAME CHAR (20) the NOT NULL, LAST_NAME CHAR (20 ), The INT of AGE, CHAR SEX (. 1), INCOME FLOAT) "" " the cursor.execute (SQL) # close the connection db.Close ()
Import pymysql # Open Database Connectivity DB = pymysql.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " ) # using the cursor () method to get the cursor operation Cursor = db.cursor () # the SQL insert statement sql = "" " INSERT INTO the EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ( 'Mac', 'Mohan', 20, 'M', 2000) " "" the try : # execute sql statement cursor. the Execute (SQL) # submitted to the database to perform the db.commit () the except : # If an error occurs rollback db.rollback () # close the database connection db.close ()
Import pymysql # Open Database Connectivity DB = pymysql.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " ) # using the cursor () method to get the cursor operation Cursor = db.cursor () # the SQL insert statement sql = " the INSERT the INTO the EMPLOYEE (FIRST_NAME, \ the LAST_NAME, of AGE, SEX, INCOME) \ the VALUES ( '% S', '% S', S%, '% S', S%) " % \ ( ' the Mac ' , 'Mohan', 20 is, ' M ' , 2000 ) the try : # execute sql statement the cursor.execute (sql) # execute sql statement the db.commit () the except : # rollback when an error occurs db.rollback () # close the connection db.close ()
Import pymysql # Open Database Connectivity db = pymysql.connect ( " localhost " , " testuser " , " test123 " , " TESTDB " ) # using the cursor () method to get the cursor operating the Cursor = db.cursor () # SQL query sql = " the SELECT * the FROM the EMPLOYEE \ the WHERE INCOME>% S " % (1000 ) the try : # execute SQL statements cursor.execute (SQL) # get a list of all the records Results = cursor.fetchall () for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # 打印结果 print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \ (fname, lname, age, sex, income )) except: print ("Error: unable to fetch data") # 关闭数据库连接 db.close()
Import pymysql # Open Database Connectivity DB = pymysql.connect ( " localhost " , " testuser " , " test123 " , " the TESTDB " ) # using the cursor () method to get the cursor operation Cursor = db.cursor () # the SQL update statement sql = " the UPDATE = the EMPLOYEE the SET of AGE. 1 of AGE + SEX = the WHERE '% C' " % ( ' M ' ) the try : # execute SQL statements the cursor.execute (SQL) # submitted to the database to perform the db.commit () the except : # rollback when an error occurs db.rollback () # close the database connection db.close ()
Import pymysql # Open Database Connectivity db = pymysql.connect ( " localhost " , " testuser " , " test123 " , " TESTDB " ) # using the cursor () method to get the cursor operating the Cursor = db.cursor () # SQL delete statement sql = " DELETE the FROM the EMPLOYEE the WHERE AGE>% S " % (20 ) the try : # execute SQL statements cursor.execute (SQL) # commit changes to the db.commit () the except : #Rollback when an error occurs db.rollback () # close the connection db.close ()
# SQL statement to delete records SQL = " DELETE the FROM the EMPLOYEE the WHERE AGE>% S " % (20 ) the try : # execute SQL statements cursor.execute (SQL) # submitted to the database the db.commit () the except : # rollback when an error occurs db.rollback ()