def insert(table,**kw):
cols, args = zip(*kw.iteritems())
sql = 'insert into `%s` (%s) values (%s)' % (table, ','.join(['`%s`' % col for col in cols]), ','.join(['?' for i in range(len(cols))]))
return _update(sql, *args)
if __name__=='__main__':
#连接数据库
create_engine('root', '123456', 'test')
insert('pics',{'img_url':'helo','author_id':'aid','quenstion_id':'12','ctime':time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())})
这是一段封装了数据库插入操作的代码,一直报错,不知道问题出在哪
第一个insert方法明明有两个参数,而且这个也不是在类中的方法,所以第一个参数不需要写self.
错误信息如下:
其实这段代码是廖雪峰的python教程中的那个项目的代码,查看完整版可以点击
还有一个我不理解的地方,insert第二个参数是**kw,为什么在方法体内使用的是*kw