And a database .python
1. preventing data injection
Note additions and deletions must be submitted to (commit ()
import pymysql us=input("请输入用户名:") pw=input("请输入密码:") db=pymysql.connect("localhost","root","root","db1") conn=db.cursor() sql="select * from aa where username='%s' and password='%s'"%(us,pw,) conn.execute(sql) res=conn.fetchone() conn.close() db.Close () IF RES: Print ( " Success " ) the else : Print ( " failure " ) # preventing data injection three kinds of packages written # conn.execute (SQL, US, PW) # # conn.execute (SQL, [US, PW]) # # conn.execute (SQL, { "U": US, "P":} PW) US = iNPUT ( " Please enter your name: " ) PW = iNPUT ( " enter password: " ) db = pymysql.connect ( " localhost " , "root","root","db1") conn=db.cursor() sql="select * from aa where username=%(u)s and password=%(p)s" conn.execute(sql,us,pw) # conn.execute(sql,[us,pw]) # conn.execute(sql,{"u":us,"p":pw}) res=conn.fetchone() conn.close() db.close() if res: print("成功") else: print("失败")
Several methods insert data
# insert data must be submitted to the db.commit () Importpymysql db= pymysql.connect ("localhost","root","root","A") #02 objects to create a cursor cursor =db. Cursor () #insert data SQL ='iNSERT INTO BB values (5000)' the cursor.execute (SQL) the db.commit () #insert must submit data #06 oFF cursor.close () #07 off db.Close ()
# Prevent insertion of data injected US= the INPUT ("Please enter your user name:") pw= the INPUT ("Please enter your password:") #insert data must be submitted to the db.commit () Importpymysql db= pymysql.connect ("localhost","the root","the root","a") #02 to create a cursor object cursor =db.cursor () #insert data SQL ='iNSERT INTO BB (username, password) values (% S,% S)' cursor.execute (SQL, (US, pw,)) the db.commit () # insert data must be submitted # 06 off cursor.close () # 07 Close db.close ()
# Prevent insertion of data injection # can simultaneously execute multiple statements, executed as many statements comparable execute () is much faster, using executemany when executing multiple statements strongly recommended that US = the INPUT ("Please enter your user name:") pw= the INPUT ("enter password:") #insert data must be submitted to the db.commit () Importpymysql db= pymysql.connect ("localhost","root","root","a") #02 objects to create a cursor cursor =db .cursor () #insert data SQL ='INTO BB INSERT (username, password) values (% S,% S) ' cursor.executemany (SQL, [( "Egon " : "AAA"), ( "AA W " : "EEEE")]) the db.commit ( ) # insert data must be submitted # 06 off cursor.close () # 07 Close db.close ()
2. Query
Import pymysql # cursor = pymysql.cursors.DictCursor) represents the results of the query are returned in the form of dots db = pymysql.connect ( " localhost " , " root " , " root " , " A " ) # 02 create a cursor object cursor db.cursor = (Cursor = pymysql.cursors.DictCursor) # Cursor = pymysql.cursors.DictCursor) shows the results of the query are returned in the form of dots # inserted data SQL = ' SELECT * from CC ' the cursor.execute (SQL) RES = cursor.fetchone () Print (RES) cursor.close () # 07 Close db.close ()