这是通过循环中的 python 脚本在 mysql 中插入值的最佳方法
我有如下的备份脚本
for f in os.listdir(path)
// do backup
现在我想插入mysql值,如文件夹名,日期,大小等
这是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循环中插入完整的代码或使其成为一个函数
I have the backup script like below
for f in os.listdir(path)
// do backup
Now i want to insert in mysql values like foldername , date , size etc
This is mysql script
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()
Now i am not sure whether insert full code in for loop or make a function of it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“在 for 循环中插入完整代码或创建一个函数” - 我们不必为您决定任何事情。这是从上下文中取出的代码片段,只有您才能决定将代码封装到方法中是否有意义……这实际上取决于您。
"insert full code in for loop or make a function" - nothing we must decide for you. It is your snippet taken out of the context and only you can make a decision if encapsulating your code into a method is making or sense or not....this is really up to you .