The MySQL database module for Python advanced features
Installing the Python MySQL component
# 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 ') #连接数据库 (connection to the server)
>>> cur = conn.cursor () # Creates a cursor (saved by object (cur)
>>> conn.select_db (' redmine ') #选择要增删改的数据库
>>> Cur.execute ("INSERT into UserInfo (name,age,gender) value (' Loyu ', ' m ') ') # Execute (SQL statement) Send SQL statement
>>> sqli = "Insert Insto userinfo (name,age,gender) value (%s,%s,%s)" #通过用对象的方法留待以后传值
>>> Cur.execute (Sqli, (' A ', 5, ' s '))
>>> Sqlim = "Insert Insto userinfo (Name,age,gender) VALUES (%s,%s,%s)" # Create multiple
>>> Cur.executemany (sqlim,[(' A ', 5, ' s '), (' B ', 3, ' DF '), (' C ', 3, ' C ')] # Executemany by passing multiple values in a list
>>> Cur.execute ("SELECT * from users")
4L
>>> cur.fetchone () #每执行一次查询打印出一条数据 (with pointers)
>>> Cur.fetchone ()
>>> cur.scroll (0, ' absolute ') # The pointer rolls back to the beginning, querying from the beginning
>>> Cur.fetchmany (4) # Fetchmany Print table 4 table data in a list
>>> Cur.fetchmany (Cur.execute ("SELECT * from users") #fetchmany通过列表的方式打印表中所有数据
>>> cur.close () #关闭游标
>>> conn.close () #关闭连接
This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1831788
12. The MySQL database module of Python advanced function