1. increase
# Import library Import pymysql # create a connection Conn = pymysql.connect (Host = ' localhost ' , = User ' the root ' , password = ' fuqian1314 ' , Database = ' WDC ' ) # get SQL statement can execute a cursor object cursor = conn.cursor () # SQL statements defined to execute SQL = " INSERT INTO the User (username, password) values ( 'root', '321') " # execute SQL statements cursor.execute (SQL) # commit the transaction (increase / delete / change the time required, when the query does not need) conn.commit () # Close cursor object cursor.close () # close the connection conn.Close ()
# Import library Import pymysql # create a connection Conn = pymysql.connect (Host = ' localhost ' , = User ' the root ' , password = ' fuqian1314 ' , Database = ' WDC ' ) # get SQL statement can execute a cursor object cursor = conn.cursor () # defined in the SQL statement to be executed # name = 'QQQ' # pwd = 'WWW' SQL = " iNSERT INTO User (username, password) values (% S,% S) " # execute SQL statements # inserted hop #the cursor.execute (SQL, [name, pwd]) # plurality of insertion cursor.executemany (SQL, [( ' ZXC ' , 123), ( ' ASD ' , 123 )]) conn.commit () # Close cursor object cursor .close () # close the database connection conn.close ()
2. Check
# Import library Import pymysql # create a connection Conn = pymysql.connect (Host = ' localhost ' , = User ' the root ' , password = ' fuqian1314 ' , Database = ' WDC ' ) # get SQL statement can execute a cursor object cursor = conn.cursor () # SQL statements defined to execute SQL = " the SELECT * from the User " # execute SQL statements cursor.execute (SQL) # all Article / fetchall (): show all / fetchmany (5): once take five data Result = cursor.fetchone () # print query to the content Print (Result) # Close cursor object cursor.close () # close the connection conn.Close ()
# Sets a list of dictionary queries way
# Get a cursor object can execute SQL statements (a list of ways sets the dictionary query) the Cursor = conn.cursor (the Cursor = pymysql.cursors.DictCursor)
3.