def test2():
32 db = util.DBUnit('mysql_ab')
33 params = [[8000080,'8000080','8000080','8000080',8000080]]
34 params1 = [['aaaaa','bbbbb']]
35 sqls ='''insert into TESTLOG(CASE_ID,TESTTASK_ID,SYNOPSIS,TESTSCENE_ID,PROJECT_ID) values (%s,%s,%s,%s,%s)'''
36 sqls1 ='''insert into LogParam(ParamName,ParamAlias) values (%s,%s)'''
37
38 n = db.write(sql=sqls,param=params)
39 print str(n)
32 db = util.DBUnit('mysql_ab')
33 params = [[8000080,'8000080','8000080','8000080',8000080]]
34 params1 = [['aaaaa','bbbbb']]
35 sqls ='''insert into TESTLOG(CASE_ID,TESTTASK_ID,SYNOPSIS,TESTSCENE_ID,PROJECT_ID) values (%s,%s,%s,%s,%s)'''
36 sqls1 ='''insert into LogParam(ParamName,ParamAlias) values (%s,%s)'''
37
38 n = db.write(sql=sqls,param=params)
39 print str(n)
原来int性也是用%s,我用%d搞了半天
write函数源代码
def write(self,sql,param,iscommit=True):
153 # animal_categories = [['snake', 'reptile'],
154 # ['frog', 'amphibian'],
155 # ['tuna', 'fish'],
156 # ['racoon', 'mammal']]
157 # cursor.executemany('''insert into animal (name, category)
158 # values (%s, %s)''', animal_categories)
159 # '''sql="insert into cdinfo values(%s,%s,%s,%s,%s)"
160 # param should be tuple or list
161 # param=((title,singer,imgurl,url,alpha))
162 # return update row num
163 # can exec update or delete opation
164 # batch insert or update ,param like this:
165 # param=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))
166 # '''
167 try:
168 cursor = self.connection.cursor()
169 n = cursor.executemany(sql,param)
170 # raise
171 # print n
172 if iscommit :
173 self.connection.commit()
174 return n
175 except:
176 print 'Write MysqlDB Error!'
177 self.connection.rollback()
178 return -1
153 # animal_categories = [['snake', 'reptile'],
154 # ['frog', 'amphibian'],
155 # ['tuna', 'fish'],
156 # ['racoon', 'mammal']]
157 # cursor.executemany('''insert into animal (name, category)
158 # values (%s, %s)''', animal_categories)
159 # '''sql="insert into cdinfo values(%s,%s,%s,%s,%s)"
160 # param should be tuple or list
161 # param=((title,singer,imgurl,url,alpha))
162 # return update row num
163 # can exec update or delete opation
164 # batch insert or update ,param like this:
165 # param=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))
166 # '''
167 try:
168 cursor = self.connection.cursor()
169 n = cursor.executemany(sql,param)
170 # raise
171 # print n
172 if iscommit :
173 self.connection.commit()
174 return n
175 except:
176 print 'Write MysqlDB Error!'
177 self.connection.rollback()
178 return -1
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/283265,如需转载请自行联系原作者