Get the App
SLTechnology News&Howtos  ›  Database  › 

12. Mysql database module with advanced functions of Python

Shulou Source: shulou.com Published: 2022-06-01 03:34:47 10月03日 Update

Mysql database module for advanced functions of Python

Install python mysql components

# yum-y install MySQL-python.x86_64

The following is illustrated by an example:

> import MySQLdb

> conn = MySQLdb.connect (user='root',passwd='2wdc%RDX',host='localhost') # Connect to the database (connection to the server)

> cur = conn.cursor () # create a cursor (saved through an object (cur)

> conn.select_db ('redmine') # Select the database to be added, deleted and modified

> cur.execute ("insert into userinfo (name,age,gender) value ('loyu',20,'m')") # execute (sql statement) sends sql statements

> sqli = "insert insto userinfo (name,age,gender) value (% sdepartment% s)" # leave the value to be passed later by using the object method

>

> sqlim = "insert insto userinfo (name,age,gender) values (% SCHI% SJO% s)" # create multiple

> cur.executemany (sqlim, [('axiomagen5 records`), (' baggage page3 recordingdf'), ('cedarmine3df')]) # executemany passes multiple values by using lists

> cur.execute ("select * from users")

4L

> cur.fetchone () # print a piece of data (with pointer) each time a query is executed

> > cur.fetchone ()

>

> cur.fetchmany (4) # fetchmany prints 4 table data in a table by list

> cur.fetchmany (cur.execute ("select * from users")) # fetchmany prints all the data in the table as a list

> cur.close () # close the cursor

> conn.close () # close the connection

Tags: Data database multiple objects pointers methods cursors statements queries advanced functional modules ab initio instances beginnings methods servers components services selection Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Redmi Shulou Information OPPO Reno