Import Pymysql
conn = Pymysql.connect (host= ' 211.149.218.16 ', user= ' jxz ', password= ' 123456 ', db= ' jxz ', port=3306, charset= ' UTF8 ')
cur = conn.cursor (cursor=pymysql.cursors.dictcursor)
name = ' ZDQ '
Sex = 0
Cur.execute (' select * from Bt_stu where real_name=%s and sex=%s ', (name, sex) # can prevent SQL injection
Print (Cur.fetchall ())
def test (A, B):
Print (A, B)
Li = [1, 2]
Test (*li)
D = {' A ': ' 123 ', ' B ': ' 456 '}
Test (**D)
def op_mysql_new (SQL1, *data):
# with *data variable parameters, you can prevent SQL injection
Print (SQL1)
Print (data)
Cur.execute (SQL1, data)
Print (Cur.fetchall ())
sql = ' SELECT * from user where username=%s and id=%s '
name = ' Haha '
ID1 = 140
Op_mysql_new (SQL, name, ID1)
# Execute multiple SQL Executemany at the same time
sql = ' INSERT into seq (blue,red,date) VALUES (%s,%s,%s) '
All_res = [
[' 16 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 15 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 14 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
[' 13 ', ' 01,02,03,05,09,06 ', ' 2018-01-28 '],
]
Cur.executemany (SQL, All_res)
Conn.commit ()
Python--sql Injection