Python's basic operation for MySQL

Source: Internet
Author: User

Import MySQLdb
db = MySQLdb.connect (host= ' localhost ', user= ' root ', passwd= ' 123456 ', db= ' database name ')
# Host: The database hostname. By default, the local host
# User: Database login name. The default is the current user
# passwd: The Secret of database landing. Default is Empty
# DB: The name of the database to use. No default value

db.select_db (' database name ')
# 2 Getting Cursors

cursor = Db.cursor ()
# 3 Manipulating Cursors
Try

# sql = "" "INSERT into student (ID, name, sex) VALUES (%s,%s,%s);" "
sql = "" "SELECT * from student;" "
Cursor.execute (SQL) # Execute SQL statement

# db.commit () # Modify the data in the table to be submitted
Print Cursor.fetchall ()
Except Exception as E:
Print E
Db.rollback ()
Finally
Cursor.close () # 4 Close cursor
Db.close ()  # 5 Close the database connection

In general, Python operation on MySQL 5 steps, first write, if there is insufficient please correct me

Python's basic operation for MySQL

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.