Introduction to PyMySQL
PyMySQL is a library for connecting to MySQL servers in the version and mysqldb in Python2.
Django can also use PyMySQL to connect to MySQL databases.
PyMySQL Installation
Install pymysql in #terminal pip install pymysql
Beginning of the text
Using PyMySQL
1. Installation
sudo pip3 install pymysql
2. Basic use
from pymysql import connect # 1. Create links coon = connect() """ * Parameter host: the connected mysql host, if the localhost is 'localhost'. * Parameter port: port of the connected mysql host, default is 3306 * Parameter user: the user name of the connection * Parameter password: password of the connection * Parameter database: name of the database * Parameter charset: encoding method for communication, utf8 is recommended. """ # 2. Creating a Cursor cur = () sql = 'select * from table_name;' count = (sql) # count is the number of rows of data affected by the sql statement # 3. Getting the data out content = () # fetchone() fetches a row of data # 4. Close the cursor () # 5. Close the connection ()
3. Other methods
- () Submitted by
- (rollback
Use with the try method
4. Anti-injection
Prevent users from submitting data with malicious splicing with sql statements, which can affect the semantics of sql statements and lead to data leakage.
Parameterization: Place the parameters in the sql statement using %s placeholders, store the required parameters in a list, and pass the list as the second parameter to the execute method
par = ['name', 'age'] ('select %s,%s from table_name;', par)
summarize
The above is a small introduction to the use of MySQL in Python - the basic use of PyMySQL, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!