12. The MySQL database module of Python advanced function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.