#!/usr/bin/env python# -*- coding: utf_ -*-import MySQLdbimport datetimeimport timeconn = MySQLdb.connect( unix_socket='/tmp/mysql.sock', host='127.0.0.1', port=3306, user='root', passwd='root', db='mytest',)
def operate_test1(id,list): aa = int(list[1]) bb = list[2] cc =int( list[7])#string转int dd = list[9] sql="insert into test (aa,bb,cc,dd,ee) values ('%d','%s','%d','%s',%d) "%( aa,bb,cc,dd,id) print sql return sql
cur = conn.cursor()
sql = "select * from test"# 获得表中有多少条数据a = cur.execute(sql)print ainfo = cur.fetchmany(a)
#info为多条查询后的结果,类型为tuple类型,i为查询后的单条结果,类型也为tuple类型for i in info: sql = operate_test(i) # print sql cur.execute(sql) #conn.insert_id()为刚插入的数据的id print int(conn.insert_id()) sql=operate_test1(int(conn.insert_id()), ii) # print sql cur.execute(sql)cur.close()conn.commit()conn.close()