Python adds, deletes, modifies, and queries mysql, and pythonmysql
No database is available for BS or CS projects.
This article is a summary of python's operations on mysql. It is suitable for developers who have a certain degree of foundation. It is best to read it by people who study step by step. Because I think that life cannot always start from scratch, it is a disaster.
For installation or use of python2.7 and mysql5.7.11, check the official website documents whenever possible.
There are two main ways for python to command mysql to work: mysql. connector and MySQLdb.
I personally think that mysql. connector is more reliable. After all, it is official.
We do not recommend that you configure mysql_config and mysql environment variables for third-party MySQLdb installation. I won't use him either.
The download link for installing mysql. connector is as follows. The installation is simple.
Http://dev.mysql.com/downloads/connector/python/
There are two main types of database operations: Query and update. Updates are divided into addition, deletion, and modification.
Python operates mysql in five steps: connect, create a cursor, splice SQL statements, execute SQL statements, and obtain results.
1. Connection
Mysql_config = {'user': 'database username', 'Password': 'database password', 'host': 'database address', 'Port': Database port number, 'database': 'database name' 'charset': 'database Code'} conn = mysql. connector. connect (** mysql_config)
2. Create a cursor
cursor=conn.cursor()
3. concatenate SQL statements
SQL = "SQL statement to be executed"
4. Execute SQL
cursor=conn.cursor()cursor.execute(sql)
5. Get Results
result_set = cursor.fetchall()
If you do not understand the SQL statements, go to the SQL tutorial.
The format of the retrieved result set is incorrect. Go to the basic python tutorial.