MySQLdb to manipulate MySQL database classes

Source: Internet
Author: User

has been using TORNDB and sqlalchemy, this time I pay attention to the next mysqldb, I feel that if you know more about T-SQL, or with mysqldb more flexible, and very simple, isn't it? All of its operations are done with one execute () method.

  code is as follows copy code

#-*-coding:utf-8-*-
Import mysqldb
 
conn = MySQLdb.connect (host= ' localhost ', db= ' MyData ', user= ' root ', passwd= ' root ', charset= ' UTF8 ')
cursor = conn.cursor ()
 
#添加数据, other modifications, deletions are also done as follows
 
sql = ' INSERT into authors (email, name) VA Lues (%s,%s) '
param = (' hello@17python.com ', ' 17python ')
N = cursor.execute (sql, param)
print n
 
#查询数据
 
sql = ' SELECT email,name from authors '
Cursor.execute (SQL)
to row in Cursor.fetchall ():
    print ' Email:%s-name:%s '% (Row[0], row[1])
 
Cursor.close ()
Conn.commit () #提交事物, Be sure to write to
Conn.close ()

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.