Again python3 the module bit pymysql that connects MySQL, we can install by PIP:
1 Install Pymysql
This allows us to use the module to connect to MySQL.
Connect MySQL to Pymysql's connect () module,
1Connect (*args, **kwargs)#Connect Method2 establish a connection to the MySQL database. Accepts several3 arguments:4 5Host:host where the database server islocated6User:username to loginch as7 Password:password to use.8Database:database to use, None to notUse a particular one.9Port:mysql port to use, default isUsually OK. (default:3306)TenBind_address:when the client has multiple network interfaces, specify the interface fromWhich to connect to the host. Argument can be a hostnameorAn IP address. OneUnix_socket:optionally, you can use a UNIX socket rather than tcp/IP. ACharset:charset you want.
You need to set a cursor (cursor) to perform operations on the database after you connect to the database Conn.cursor ():
1 # Creating Cursors 2 inch Module Pymysql.connections: 3 4 cursor (cursor=None) method of Pymysql.connections.Connection instance5 Create a new Cursor to execute queries with
After you set the cursor, you can write the SQL statement, use the cursor instance to execute, and execute the Execute () method:
1>>> Help (Cursor.execute)#Execute Method2Help on Method ExecuteinchModule Pymysql.cursors:3 4Execute (query, args=None) method of Pymysql.cursors.Cursor instance5 Execute a query6 7 :p Aram Str query:query to execute.8 9 :p Aram Args:parameters used with query. (optional)Ten: Type Args:tuple, ListorDict One A:return: Number of affected rows - : Rtype:int - theIf args isA listorTuple,%s can be used as a placeholderinchThe query. -If args isA dict,% (name) s can be used as a placeholderinchThe query.
After executing the SQL statement, the query returns results that can be viewed using the cursor's Fetchall () function, which returns a tuple with the elements of each tuple cursor.fetchall ():
1 >>> help (cursor.fetchall)2 in module pymysql.cursors:3 4Fetchall () method of Pymysql.cursors.Cursor instance5 Fetch all the rows
You can use the For loop to view the results individually.
The following is a complete example:
1 ImportPymysql2 3Conn=pymysql. Connect (host='192.168.37.130', port=3306,user='Root', password='123456', database='User')4Cursor=conn.cursor (pymysql.cursors.DictCursor)5Sql='SELECT * from Users'6res_num=cursor.execute (SQL)7 Print(Res_num)8Res_content=Cursor.fetchall ()9 forIinchres_content:Ten Print(i)
View Code
The above simulates a simple database query operation. Here we are writing a simple example:
1 ImportPymysql2 3Conn=pymysql. Connect (host='192.168.37.130', port=3306,user='Root', password='123456', database='User')4Cursor=conn.cursor (pymysql.cursors.DictCursor)5Name=input ('Input Name:'). Strip ()6Sql='SELECT * from users where name is like%s'7res_num=Cursor.execute (sql,name)8 ifRes_num>0:9Res_content=Cursor.fetchall ()Ten forIinchres_content: One Print(i) A Else: - Print('Not in database')View Code
Python connection MySQL