Python/mysql (third, pymysql use) the so-called Pymysql is through the Pycharm import pymysql module for remote connection to the MySQL server for data management operations.
first, import the Pymysql module in Pycharm:
Search and import at the end
Second, connect the MYSQ server through the pycharm statement (MySQL server must start first)
1 ImportPymysql2 Import Pymysql3Conn=pymysql.connect (host='localhost', user='Root', password='guobaoyuan123', database='User', charset='UTF8')4To connect to the database server (host Access server side of the Ip,user access service side of the user, password access to the server user password, database access to the server server, CharSet access to the encoding method)
Pymysql Modifying a database
1 ImportPymysql2Conn=pymysql.connect (host='localhost', user='Root', password='guobaoyuan123', database='Lian')3 ## Connect the MySQL database and create a handle (host connected machine, user connection, password connection secret = password, database connected to databases)4Coursor=conn.cursor ()5 ##创建游标6Ste=coursor.execute ('INSERT into student (SID) VALUES (2)')7 ##通过游标执行SQL命令语句8 Conn.commit ()9 ##进行提交 (when you make changes to the content in the database, you need to submit the content to save it)Ten coursor.close () One ##关闭游标执行操作 A conn.close () - ##断开连接
Pymysql View operations on a database
1 ImportPymysql2Conn=pymysql.connect (host='localhost', user='Root', password='guobaoyuan123', database='Lian')3 ## Connect the MySQL database and create a handle (host connected machine, user connection, password connection secret = password, database connected to databases)4Coursor=conn.cursor ()5 ##创建游标6 ##查询数据库7row=Coursor.fetchone ()8 ##查询数据库表中的第一行数据9 #Row_i=coursor.fetchmany ()Ten ## #查询数据库表中的第 (you want to query) row data can only fill in one One #Row_d=coursor.fetchall () A ## #查询数据库表中的所有行数据 - Print(Row) - ##打印查询的数据内容 (shown as a tuple) the coursor.close () - ##关闭游标执行操作 - conn.close () - ##断开连接
Note: In the use of Pymysql, the top is two templates, in the Pycharm remote MySQL used statements are SQL statements, all the statements are placed in the Coursor.execute () to the server, the MySQL server received after the statement parsing , then execute!
Python/mysql (third, pymysql use)