12. Mysql database module with advanced functions of Python
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