utilize Navicat Premium 15 Software connection mysql database , newly build testdb database , And add 2 Tables usertest and userinfo.

 

main.py
#!/usr/bin/python3 # -*- coding: utf-8 -*- import re import pymysql # Import module
myConn = pymysql.connect( host='127.0.0.1', # Host module port=3306, # Port number
user='root', # user name password='root', # password database='testdb', # Database to connect to
charset='utf8' # Specify the encoding utf8 ) if __name__ == '__main__': # myCursor =
myConn.cursor() # Get cursor , The default cursor type is tuple myCursor =
myConn.cursor(pymysql.cursors.DictCursor) # The obtained query results are more standardized Easy to distinguish sql = "select *
from userTest;" row_count = myCursor.execute(sql) # row_count Number of rows affected for x in
myCursor.fetchall(): # Take out all of them print(x) try: # === insert record === sql = "INSERT INTO
userTest(id,name,age) VALUES(%s, %s, %s)" val = (6, "John", 23)
myCursor.execute(sql, val) # implement sql sentence """ val = [(7, "Lily", 30), (8, "Martin",
35), (9, "Sally", 32)] myCursor.executemany(sql, val) # When inserting multiple rows of data , implement sql sentence """ #
=== Modification record === # Write as a string SQL sentence sql = "update userTest set name = ' Liu Qi ' where name =
' Han Han '; " # implement SQL sentence row_count = myCursor.execute(sql) # === Delete record === #
Write as a string SQL sentence sql = "delete from userTest where name= ' Wang Wu ' ;" # implement SQL sentence
row_count = myCursor.execute(sql) myConn.commit() # When adding, deleting or modifying , Submission required except
Exception as err: # Check if the cause of the exception is of interest result1 = re.search('Duplicate
entry.*key.*PRIMARY', str(err)) # If it is , You don't have to do anything # otherwise ( I don't know why ), Then roll back if result1
is None: myConn.rollback() raise myCursor =
myConn.cursor(pymysql.cursors.DictCursor) # The obtained query results are more standardized Easy to distinguish sql = "select *
from userTest;" row_count = myCursor.execute(sql) # row_count Number of rows affected for x in
myCursor.fetchall(): # Take out all of them print(x) # ===== Use data to authenticate user name and password ======
myCursor = myConn.cursor(pymysql.cursors.DictCursor) usr =
input(' enter one user name :').strip() pwd = input(' Please input a password :').strip() # When the user name is known Crack password for (feng'--
dfadasdad) # When the user name and password are unknown Crack password for (fsdf' or 1=1 -- fdsfsdfs) try: sql = "select *
from userinfo where username='%s' and password='%s';" % (usr, pwd) #
res We say it's the number of rows we get , If the number of rows is not zero , Indicates that the user name and password entered by the user exist , If 0 Description does not exist row_count =
myCursor.execute(sql) # pymysql The module will delete the special characters automatically print(row_count) #
If you enter the wrong user name and password , The result is 0, If it's right , The result is 1 if row_count: print(' Successful login ') else:
print(' Wrong user name and password !') except Exception as err: # Check if the cause of the exception is of interest result1 =
re.search('Duplicate entry.*key.*PRIMARY', str(err)) # If it is , You don't have to do anything #
otherwise ( I don't know why ), Then roll back if result1 is None: myConn.rollback() raise
myCursor.close() # Close cursor myConn.close() # Close the connection
 

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree