Python Operation MySQLdb

Source: Internet
Author: User

Learn a little every day, C # is tired of writing, recently in writing Node,python,swift, enrich their own development of the world;

Python is more as a glue language, do some automated scripts, network tasks, database tasks, scheduled tasks;

Installing MYSQLDB

To enable Python to operate MySQL requires the Mysql-python driver, which is a necessary module for Python to operate MySQL.

: https://pypi.python.org/pypi/MySQL-python/

>>python setup.py Install

Then you can operate the database;

Inquire

Check: Fetchone,fetchmany,fetchall

Also make the charset and the database encoding format consistent:

Insert:

#插入一条数据sqli = "INSERT into student values (%s,%s,%s,%s)" Cur.execute (Sqli, (' 3 ', ' Huhu ', ' 2 year 1 class ', ' 7 '))

BULK INSERT:

#一次插入多条记录sqli = "INSERT into student values (%s,%s,%s,%s)" Cur.executemany (sqli,[    (' 3 ', ' Tom ', ' 1 Year 1 class ', ' 6 '),    (' 3 ', ' Jack ', ' 2 year 1 class ', ' 7 '),    (' 3 ', ' Yaheng ', ' 2 Year 2 class ', ' 7 '),    ])
    value=[1, ' Hi Rollen ']    cur.execute (' INSERT into test values (%s,%s) ', value)         values=[] for    i in range (20):        Values.append ((i, ' Hi Rollen ' +str (i)))             Cur.executemany (' INSERT into test values (%s,%s) ', values)     Cur.execute (' Update test set info= ' I am Rollen "where id=3 ')

  

Python Operation MySQLdb

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.