Operation of MySQL database with Python (simple "insert data" action)

Source: Internet
Author: User
#!/usr/bin/env python#-*-coding:utf-8-*-' ' 1, here the database environment for the local database 2, I want to operate through Python database is TEST_DB3, the database has a table, table name: User4, Table fields: ID, name, ADDRESS5, where the ID field is self-incremented, is also the primary key 6, because the ID field has been since, so when inserting data will increase the number itself, so there is no additional data inserted here "import mysqldb# to establish a connection conn = MySQLdb.connect (host= ' 127.0.0.1 ', user= ' root ', passwd= ' 1qaz#edc ', db= ' test_db ') cur = conn.cursor () #对数据进行操作sql = " Insert into User (name,address) VALUES (%s,%s) "#定义一条sql语句,%s is the placeholder params = (' Tantianran ', ' GuangZhou ') #定义参数, These two parameters are corresponding to the placeholder res = Cur.execute (sql,params) #执行sql语句conn. Commit () #提交请求, otherwise the data is not written to the database # Close database connection Cur.close () Conn.close () Print res #打印出执行这条sql语句后影响的条数


This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1876204

Operation of MySQL database with Python (simple "insert data" action)

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.