#!/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)