Use of python database package pymysql

Use pymysql module
1. Import pymysql module
2. Create a connection
3. Run the cursor acquisition
4. cursor to execute SQL statements
The SQL statement the execution result acquired
6. Close the cursor
7. Close the connection

# 1. Import module pymysql 
Import pymysql 
User = INPUT ( ' Enter Username: ' ) 
pwd = INPUT ( ' Enter password: ' ) 

# 2. Create a connection 
Conn = pymysql.connect (Host = ' 127.0.0.1 ' , = 3306 Port, User = ' the root ' , password = '' , DB = ' DB8 ' , charset = ' UTF8 ' ) 

# 3. Run acquired cursor 
cursor = conn.cursor () 

# note% s requires quotes 
sql = "* from UserInfo WHERE username SELECT = '% S' and pwd = '% S' " % (User, pwd)
 Print (sql) 

# 4. vernier to sql statement execution, 5 and obtains an execution result. 
the cursor.execute (sql) 

Result = the cursor.execute (sql) # execute sql statement, sql query returns the number of successful recording 
Print (Result) 

# 6. the cursor off 
cursor.close ()
 # 7. the close connection 
conn.Close () 

IF Result:
     Print ( ' successful landing ' )
 the else :
     Print ( ' Login failed ' )

 

Guess you like

Origin www.cnblogs.com/hercules-chung/p/12457775.html