Both methods use simple python code and data insertion is connected MySQL

MySQL connection code is as follows:

pymysql Import 
# Open Database Connectivity parameters were as follows: 
Conn = pymysql.connect (Host = 'localhost', = User 'the root', password '1234', Database = 'pymysql_demo', Port = = 3306) 
Method # using cursor () obtaining cursor operation 
cursor = conn.cursor () 
. 
. 
. 
. 
# close the connection 
conn.Close ()

 

Insert data:

1)

pymysql Import 
Conn = pymysql.connect (Host = 'localhost', = User 'the root', password = '1234', Database = 'pymysql_demo', Port = 3306) 
Cursor = conn.cursor () 

# insert data format is as follows: insert into table (Inserts header) value (corresponding to data) 
SQL = "" " 
iNSERT into User (ID, username, Age, password) value (. 1, 'Rose',. 19, '12345') 
" "" 
# sql statement submitted mapped to the database. 
the cursor.execute (SQL) 
conn.commit () 

# close the connection 
conn.Close ()

  

  

2)

pymysql Import 
Conn = pymysql.connect (Host = 'localhost', = User 'the root', password = '1234', Database = 'pymysql_demo', Port = 3306) 
Cursor = conn.cursor () 


# id selected if auto-increment and primary key can be set to null, it automatically increase. 
= SQL "" " 
INSERT INTO User (ID, username, Age, password) value (null,% S,% S,% S) 
" "" 
username = 'Lily' 
Age = 20 is 
password = '666666' 
the cursor.execute ( SQL, (username, Age, password)) 
conn.commit () 


conn.Close ()

  

  In many cases, you need to insert the value is not fixed but variable, so we can use this method to insert data.

Guess you like

Origin www.cnblogs.com/zyde-2893/p/11267232.html