Python-day11 Pymysql

Source: Internet
Author: User
Tags sql injection

Python operation MySQL

###### #select

Import Pymysql

# Get Data
conn = Pymysql. Connect (host= ' 192.168.12.89 ', port=3306,user= ' root ', password= "123", database= "s17day11db", charset= ' UTF8 ')
cursor = Conn.cursor ()
# Number of rows affected
v = cursor.execute (' SELECT * from student ')
result = Cursor.fetchall ()
# result = Cursor.fetchone ()
# result = Cursor.fetchmany (2)
Print (Result)

Cursor.close ()
Conn.close ()



########## #other

 # get Data 
conn = Pymysql. Connect (host= ' 192.168.12.89 ', port=3306,user= ' root ', password= "123", database= "s17day11db", charset= ' UTF8 ')
cursor = Conn.cursor ()
# Number of rows affected
V = cursor.execute (' INSERT into userinfo (Username,password) VALUES (%s,%s) ', [' Eric ', ' 99999 '])
Conn.commit ()
V = cursor.execute (' Delete from userinfo where username=%s ', [' Eric '])
Conn.commit ()
V = cursor.execute (' Update userinfo set password=%s where username=%s ', [' 999999 ', ' Alex '])
Conn.commit ()

Cursor.close ()
Conn.close ()

################ #需求
Create a new class and create a student to join the class
 Import pymysql 

# get data
conn = Pymysql. Connect (host= ' 192.168.12.89 ', port=3306,user= ' root ', password= "123", database= "s17day11db", charset= ' UTF8 ')
cursor = conn.cursor ()

Cursor.execute (' Insert into Class (caption) values (%s) ', [' New class '])
Conn.commit ()
new_class_id = cursor.lastrowid # get new data self-increment ID

Cursor.execute (' INSERT into student (sname,gender,class_id) values (%s,%s,%s) ', [' Li Jie ', ' Male ', new_class_id])
Conn.commit ()

Cursor.close ()
Conn.close ()

Explanation: You can get the self-increment ID that is executed in this script by Cursor.lastrowid
and then put it into a panic in SQL


#################################### #被sql注入的写法 , and SQL injection
 Import pymysql 

user = input (' Please enter user name: ')
pwd = input (' Please enter password: ')

# get data
conn = Pymysql. Connect (host= ' 192.168.12.89 ', port=3306,user= ' root ', password= "123", database= "s17day11db", charset= ' UTF8 ')
cursor = conn.cursor ()
sql = ' SELECT * from UserInfo where username= '%s ' and password= '%s '% (User,pwd,)
# user = a Lex "--
# pwd= asdf
' select * from UserInfo where username=" Alex "--" and password= "Sdfsdf" '
# user = Asdfasdf " or 1=1--
# pwd= asdf
' select * from userinfo where username= ' asdfasdf ' or 1=1--"and password=" Asdfasdf "'
V = Cursor.execute (sql)
result = Cursor.fetchone ()
Cursor.close ()
Conn.close ()

Print (Result)


################## #防止sql注入
Import Pymysql

user = input (' Please enter user name: ')
PWD = input (' Please enter password: ')

# Get Data
conn = Pymysql. Connect (host= ' 192.168.12.89 ', port=3306,user= ' root ', password= "123", database= "s17day11db", charset= ' UTF8 ')
cursor = Conn.cursor ()
sql = ' SELECT * from UserInfo where username= '%s ' and password= '%s '% (User,pwd,)
# user = Alex "--
# pwd= ASDF
' SELECT * from UserInfo where username= "Alex"--"and password=" SDFSDF "'
# user = Asdfasdf "or 1=1 --
# pwd= ASDF
' SELECT * from UserInfo where username= "Asdfasdf" or 1=1 --"and password=" ASDFASDF "'
v = cursor.execute (sql)
result = Cursor.fetchone ()
Cursor.close ()
Conn.close ()

Print (Result)
















Python-day11 Pymysql

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.