mysql记录分条导出为txt文件

本文记录了一位开发者在使用Python的pymysql模块从数据库导出数据到文件时遇到的问题及解决方案。最初,由于字符串格式化问题导致SQL执行错误,通过调整双引号的使用成功解决了文件路径识别问题,实现了数据导出。
摘要由CSDN通过智能技术生成

表结构如下

NoAbstract
1人生无常,大肠包小肠

导出结果:

第一次写的是这样的,运行报错。

import pymysql
db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders')
cursor= db.cursor()
for i in range(1,2686):
    sql='select Abstract into outfile 'D:/install/desktop/absman/{}.txt' lines terminated by'/r/n' from absman where No ={}'.format(i,i)
    cursor.execute(sql)
    db.commit()

sql= 这一行看起来也奇奇怪怪,看颜色也知道文件路径和enter符没有被识别到

突然想到是不是双引号单引号的问题,于是改为如下

import pymysql
db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders')
cursor= db.cursor()
for i in range(1,2686):
    sql="select Abstract into outfile 'D:/install/desktop/absman/{}.txt' lines terminated by'/r/n' from absman where No ={}".format(i,i)
    cursor.execute(sql)
    db.commit()

成功

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值