Tag:blog val Statement mit import url utf8 query generate
Import pymysql# connect to database connect = Pymysql. Connect ( host= ' localhost ', port=3306, user= ' root ', passwd= ' root ', db= ' Python3 ', charset= ' UTF8 ') # gets cursor cursor = connect.cursor () # SQL Operation # Add data operation Sql_1 = "INSERT into Url_file (url,file) VALUES (%s,%s)" data = (' AA ', ' BB ') cursor.execute (sql_1, data) # Generate an Add SQL statement connect.commit () # to confirm the permanent execution of the Add # query data operation (only the query is full of cursors, the other 3 operations, To commit with a connection commit) Sql_2 = "SELECT * from Url_file" Cursor.execute (sql_2) # Generate query SQL statement ret = Cursor.fetchall () # Execute query print ("Done") print (Ret) print (type (ret)) # Modify data Operation Sql_3 = "Update url_file set url= ' bbbbbbbbbbbb ' WHERE id = 2" Cursor.execute (sql_3) # Generate Modify SQL Statement connect.commit () # Confirm permanently execute Modify # delete data operation Sql_4 = "Delete from Url_file where ID =12 "R1 = Cursor.execute (sql_4) # Generates modified SQL statement, temporarily executes r2 = Connect.commit () # confirm Permanent Execution Delete print (r1) print (R2)
Working with MySQL in Python