1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import  MySQLdb
 
 
#建立连接
conn  =  MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1qaz#EDC' ,db = 'test_db' )
cur  =  conn.cursor()
 
#对数据进行操作
li  =  [( 'tanzhenx' , 'shaoguan' ),( 'huangmengdie' , 'shaoguan' )]  #定义一个列表,列表中含多个元组,等会批量插入每个元组中的数据
cur.executemany( 'insert into user (name,address) values(%s,%s)' ,li)  #批量插入数据
conn.commit()    #提交请求,否则数据是不会写入数据库里
#关闭数据库连接
cur.close()
conn.close()