one , Preparation module

python connect SQL The database needs to be used first ”pymysql“ modular , Used here pip install Instructions to install the following steps :

1, On installed python Found under the path of Scripts Folder and open , Write it on the path “cmd” Back enter

2, Enter after entering this interface pip install pymysql Start installation

  two , Connect to the database

use pymysql In the module connect Connect to the database , Simultaneous use cursor() Function to create a cursor , Used to receive returned results .

code :
import pymysql # --------- connect -------------- connect =
pymysql.connect(host='localhost', # Local database user='root', password='123456',
db='sys', charset='utf8') # server name , account , password , Database name cur = connect.cursor() print(cur)
notes :user And password Is the user name and password of your database ,db It is not required to fill in the database name .

A cursor instance object is returned here , It means that it has been successful .

three , Operate on the database

It can be used after the previous connection is completed cur.execute() Function to perform an operation on the database

Several cases are attached below :
# -------------------- Create table ----------------- try: create_sqli = "create table
sys (id int, name varchar(30),phone int);" cur.execute(create_sqli) except
Exception as e: print(" Failed to create data table :", e) else: print(" Data table created successfully ;") #
--------------- insert --------- try: insert_sqli = "insert into sys values(001,
'xiaoming',123456789);" cur.execute(insert_sqli) except Exception as e:
print(" Failed to insert data :", e) else: # If inserting data , Be sure to submit data , Otherwise, the data to be inserted cannot be found in the database ; connect.commit()
print(" Insert data successfully ;")
After running successfully, you can find the corresponding changes in the database on the computer , Finally, remember to close the connection to the database
# --------------------- close database cur.close() # Close cursor connect.close() # Close database connection
Complete code :
import pymysql # --------- connect -------------- connect =
pymysql.connect(host='localhost', user='root', password='123456', db='sys',
charset='utf8') # server name , account , password , Database name cur = connect.cursor() #
-------------------- Create table ----------------- try: create_sqli = "create table sys
(id int, name varchar(30),phone int);" cur.execute(create_sqli) except
Exception as e: print(" Failed to create data table :", e) else: print(" Data table created successfully ;") #
--------------- insert --------- try: insert_sqli = "insert into sys values(001,
'xiaoming',123456789);" cur.execute(insert_sqli) except Exception as e:
print(" Failed to insert data :", e) else: # If inserting data , Be sure to submit data , Otherwise, the data to be inserted cannot be found in the database ; connect.commit()
print(" Insert data successfully ;") # --------------------- close database cur.close() # Close cursor
connect.close() # Close database connection

Technology
Daily Recommendation
views 6
views 5