pymysql

Installation of pymysql:

method one:

1. Open the local dos window:
 2. Enter: cmd
 3. Enter: pip install pymysql

Method 2:

1. Open the python installation directory: find the \Scripts\ file
After opening: easy_install.exe
                easy_install-3.6.exe
                pip.exe
                pip3. 6 .exe
                pip3.exe
 After seeing several applications like this, it means that you have found the right one.

2. Hold down the keyboard Shift + right mouse button in the Scripts file to see the command window open here:

       Enter: pip3. install pymysql to install

        View currently existing modules:
          What are the installed modules of pip3 -list

 

Link: pymysql

1. Basic use:

#Import pymysql module: 
import pymysql

#Connect database: 
conn = pymysql.connect(host="your database address", user="user name", password="password", database="database name, charset="utf8") #Get 

the cursor of the database:
cursor = conn.cursor()

# Define the SQL statement to be executed
 sql = """


# Execute the above SQL statement 
cursor.execute(sql)
# close the cursor object
cursor.close()
# Close the database connection 
conn.close()

 

Return data content in dictionary format:

#Get a cursor that can execute the SQL statement and return the result as a dictionary 
cursor = conn.cursor(cursor= pymysql.cursors.DictCursor) #Execute
 
the SQL statement cursor.execute(sql)

                   python-mysql CRUD

increase:

#Get a cursor 
cursor = conn.cursor()

#The insert sql statement to be executed 
sql = " insert into userinfo(name,pwd) VALUES (%s,%s) " 
name = " Yoshizawa Akio " 
pwd = " 3714 "

#Splice and execute the sql statement 
cursor.execute(sql,[name,pwd])

#Submit write data 
conn.commit()

#Close the connection 
cursor.close()
conn.close()

Note: When writing data, try not to directly add the written data to VALUES. If the customer logs in, the user name and password need to be judged

If there is such a statement: select * from userinfo where name ='%s' and passwd='%s';

This will introduce SQL injection problems when entering account passwords 

name: alexd "" -- or 1=1, no matter what the account password is, the login will be successful


Failed to insert data and rollback:

in colleague' 

import
pymysql conn =pymysql.connect(host='127.0.0.1',port=3306,user="root",password ="admink",database="db1",charset='utf8') #Get a cursor cursor = conn.cursor() sql1 = " insert into userinfo(user, pwd) VALUES (%s, %s); "
sql2 = "insert into userinfo (user) values(%s)"
user = " Alex " pwd ="123"

book_tite = 'alex da shuaibi '
try : #Execute the SQL statement cursor.execute(sql1, [user, pwd])
   cursor.execute(sql2) #If you do not pass a value to the sql2 statement, it will not be executed
  
#Commit the transaction conn.commit() except Exception as e : #There is an exception, rollback the transaction conn.rollback () cursor.close() conn.close()

Get the ID of the inserted data (used in the association operation

! In fact, it is submitted by two data table colleagues, and the correct statements are executed directly, and the execution statements with errors are rolled back

import pymysql

conn =pymysql.connect(host='127.0.0.1',port=3306,user="root",password ="admink",database="db1",charset='utf8')

#Get a cursor 
cursor = conn.cursor()

sql1 = "insert into userinfo(user, pwd) VALUES (%s, %s);"
sql2 = "insert into userinfo (user) values(%s)"
user = "Alex" pwd = "123"

book_tite = 'alex da shuaibi '

try : # Execute the SQL statement cursor.execute(sql1, [user, pwd])
   cursor.execute(sql2) # If you don't pass a value to the sql2 statement, it will not execute
   # Submit the transaction conn.commit() #Get
  the last successfully inserted content
  
last_id = cursor.lastrowid
except Exception as e: # There is an exception, rollback the transaction  conn.rollback() cursor.close() conn.close() 


 

 

Batch execution:

 
sql = "insert into userinfo(name,pwd) VALUES (%s,%s);" 
date ={
("High Talent","youqian"),
("changjiang","huanghe"),
("alex"," dashuaibi")
}

cursor.executemany(sql,date) #Commit

to write data
conn.commit() #Close


the connection
cursor.close()
conn.close()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324928933&siteId=291194637