通过循环中的python脚本在mysql中插入值的最佳方法是什么

通过循环中的python脚本在mysql中插入值的最佳方法是什么,python,mysql,Python,Mysql,我有如下的备份脚本 for f in os.listdir(path) // do backup 现在我想在mysql中插入foldername、date、size等值 这是mysql脚本 sql = "INSERT INTO pythontest(name, \ name2, name3) \ VALUES ('%s', '%s', '%s')" % \ ('Mac', 'Mohan', '') try: # Execute the SQL

我有如下的备份脚本

for f in os.listdir(path)
  // do backup
现在我想在mysql中插入foldername、date、size等值

这是mysql脚本

sql = "INSERT INTO pythontest(name, \
       name2, name3) \
       VALUES ('%s', '%s', '%s')" % \
       ('Mac', 'Mohan', '')
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   db.commit()
except:
   # Rollback in case there is any error
   db.rollback()

现在我不确定是在for循环中插入完整代码还是生成函数

在for循环中插入完整代码还是生成函数-我们不需要为您做任何决定。它是从上下文中提取的代码片段,只有您才能决定将代码封装到方法中是否可行……这取决于您自己