Python MySQL database of pymysql connection module (1)

01Python Login .py

username = input ( "Enter the user name:") 
pwd = the INPUT ( "Please enter your password:")

# IF username == "ERGE" and pwd == "dashabi":
# Print ( "! successful landing")
# the else:
# print ( "roll ~")


with Open ( "userinfo.txt", "R & lt", encoding = "UTF-. 8") AS F:
for in Line F:
# Print (line.strip ())
U, P = . line.strip () Split ( "|")
IF U == == username and the p-pwd:
Print ( "! successful landing")
BREAK
the else:
Print ( "Go OUT ~")


02Python connect to the database login .py
pymysql Import 

username = the INPUT ( "Enter the user name:")
pwd = the INPUT ( "Please enter your password:")

# IF username == "ERGE" and pwd == "dashabi":
# Print ( "successful landing!")
# the else:
# Print ( "roll ~")

# get user name of the user entered password

# determines which database to the user name and password are correct
# 1 connected to the database
Conn = pymysql.connect (
Host = "localhost",
Port = 3306, # port number is numeric
database = "userinfo", # write your own local database name of
the User = "root",
password = "123456",
charset = "utf8" # always remember that no -
)

the Cursor = conn.cursor () # get input SQL statement cursor object
SQL = "info from the SELECT *;"
RET = the cursor.Execute (SQL)
Print (RET)
# close the connection
cursor.close ()
conn.Close ()


2. Analyzing # -> only need to write the sql statement search condition, a database to be performed on a

# with Open ( "userinfo.txt", "R & lt", encoding = "UTF-. 8") AS F:
# f Line in for:
# # Print (line.strip ())
. # U, the p-line.strip = () Split ( "|")
# IF U == == username and the p-pwd:
# Print ( "successful landing ! ")
# BREAK
# the else:
# Print (" Go OUT ~ ")


03 Login check MySQL.py
pymysql Import 

# get user input
username = input ( "Enter the user name:")
pwd = the INPUT ( "Please enter your password:")


# there is no connection to the database to retrieve the user
conn = pymysql.connect (
Host = "localhost",
Port = 3306,
Database = "UserInfo",
the User = "root",
password = "123456",
charset = "utf8"
)

the cursor = conn.cursor () # get the cursor
SQL statement to be executed mosaic #
sql = "select * from info username = WHERE '% S' and password = '% S' "% (username, pwd)
Print (SQL)
Print (" = "120 *)
# execute SQL statements
RET = the cursor.execute (SQL)
IF RET:
Print ( "Login successful")
the else:
Print ( "Login failed!")
# Close a cursor image
cursor.close ()
# close the connection
conn.Close ()

04 Log check MySQL avoid SQL injection .py
pymysql Import 

# get user input
username = input ( "Enter the user name:")
pwd = the INPUT ( "Please enter your password:")


# there is no connection to the database to retrieve the user
conn = pymysql.connect (
Host = "localhost",
Port = 3306,
Database = "UserInfo",
the User = "root",
password = "123456",
charset = "utf8"
)

the cursor = conn.cursor () # get the cursor
SQL statement to be executed mosaic #
sql = 'select * from info username =% S and the WHERE password =% S '
Print (SQL)
Print ( "=" * 120)
# execute SQL statements
ret = cursor.execute (sql, [username , pwd]) # let us help pymysql stitching SQL statements
if RET:
Print ( "Login successful")
the else:
Print ( "Login failed!")
# Close a cursor image
cursor.close ()
# close the connection
conn.Close ()

05pymysql by operation .py
"" " 
Pymysql-increment
" ""

Import pymysql

Conn = pymysql.connect (
Host = "localhost",
Port = 3306,
Database = "UserInfo",
User = "the root",
password = "123456",
charset = "UTF8"
)

Cursor = conn.cursor ()

# splicing statement
SQL = "INSERT INTO info (username, password) the VALUES (% S,% S)"
# execute
the try:
the cursor.execute (SQL, [ "big Xu"])
# write your own for loop (today operating under its own test)
conn.commit ()
the except Exception AS E:
Print ( "error Rights:", str (E))
conn.rollback () rollback #
# write to the database to do a certain operation remember to submit assword

the Cursor.use Close ()
conn.Close ()

06 demo lastrowid what's the use .py
pymysql Import 

Conn = pymysql.connect (
Host = "localhost",
Port = 3306,
Database = "UserInfo",
User = "the root",
password = "123456",
charset = "UTF8"
)

Cursor = conn.cursor ()
# create a class sql statement
SQL1 = "INSERT INTO class (name) VALUES (% S)"
# students create sql statement
SQL2 = "INSERT INTO student (name, cid) VALUES (% S,% S)"


cursor.execute ( sql1, "full-stack 9")
NEW_ID ID = cursor.lastrowid # Get the value of the newly inserted data
cursor.execute (sql2, [ "small Northeast", NEW_ID])

conn.commit ()
cursor.close ()
conn.Close ()

07 bulk insert data .py
pymysql Import 

Conn = pymysql.connect (
Host = "localhost",
Port = 3306,
Database = "UserInfo",
User = "the root",
password = "123456",
charset = "UTF8"
)

Cursor = conn.cursor ()
# create class sql statement
sql = "INSERT INTO info (username, password) the VALUES (% S,% S)"

Data = [( "alex1", "dashabi"), ( "dirty Sir1",), ( "xiaoyima1 " , "nvshen")]
the try:
cursor.executemany (SQL, the Data) # internals for circulating, batch execution insert statement
# for i in the Data:
# cursor.execute (SQL, i)
conn.commit () # submitted once
except AS E Exception:
Print ( "You're wrong!")
conn.ROLLBACK ()

cursor.close ()
conn.Close ()

08 deletes data manipulation .py
import pymysql

conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)

cursor = conn.cursor()

# sql = "delete from info WHERE username=%s"
sql = "delete from info WHERE id=%s"

cursor.execute(sql,8)

conn.commit()
cursor.close()
conn.close()
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/mys6/p/11278758.html