求助:用python写一段与mysql语句相关的简单程序。cur.execute中用迭代的问题。

陈景伟-英语 陈小妞 2018-08-13 09:11:51
楼主想求大侠用python中将下面mysql语句自动输出。


cur.execute( "select contents from table_materialshighschool where id=1 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m1.txt';")
cur.execute( "select contents from table_materialshighschool where id=2 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m2.txt';")
cur.execute( "select contents from table_materialshighschool where id=3 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m3.txt';")
cur.execute( "select contents from table_materialshighschool where id=4 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m4.txt';")
cur.execute( "select contents from table_materialshighschool where id=5 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m5.txt';")
cur.execute( "select contents from table_materialshighschool where id=6 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m6.txt';")
cur.execute( "select contents from table_materialshighschool where id=7 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m7.txt';")
cur.execute( "select contents from table_materialshighschool where id=8 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m8.txt';")
cur.execute( "select contents from table_materialshighschool where id=9 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m9.txt';")
cur.execute( "select contents from table_materialshighschool where id=10 into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m10.txt';")
--------------------------------------------------------------------------------------------------------------------------
cur.execute( "select contents from table_materialshighschool where id=n into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/mn.txt';")
...全文
288 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不好意思,我没说清楚。不是print,而是我想用python批量生成txt空文件。它们的名字是m1.txt, m2.txt, m3.txt,...mn.txt。麻烦各位。
爱吃黄瓜的喵 2018-08-14
  • 打赏
  • 举报
回复
有点没太懂啥意思……
陈年椰子 2018-08-14
  • 打赏
  • 举报
回复
帖子里没说清楚,是这个意思吗?

sql_str = '''cur.execute( "select contents from table_materialshighschool where id={0} into outfile 'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/m{0}.txt';")'''
for i in range(1,20):
print(sql_str.format(i))

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧