数据库查询 in传入数组

import pymysql
from pandas import DataFrame
import numpy as np
import pandas as pd
def findFromViogiData(ebaynos):
    db = pymysql.connect(host='****', user='**', passwd='**', db='*', port=3306, charset='utf8')
    cursor = db.cursor()
    # ebaynos =[ '152329556836', '121698891442', '161719488555', '130982977280', '121801322206']
    arrs =  ', '.join(list(map(lambda x: '%s', ebaynos)))
    sql = ' select p.ebayno, p.category FROM product p where p.ebayno in (%s)'%(arrs)
    cursor.execute(sql,ebaynos)
    data = cursor.fetchall()#元祖中的元素也是元祖
    if len(data) == 0:
        print("viogidata搜索结果为空")
        return
    data = np.array(data) #将元祖转换成数组
    df = DataFrame(data , columns=["ebayno","category"])#创建对象DataFrame,参数值为数组
    df.to_csv("category_viogidata.csv" , index=False) #输出
    db.commit()
    cursor.close()
    db.close()
def findFromViogiDataer(ebaynos):
    db = pymysql.connect(host='**', user='**', passwd='**', db='*', port=3306, charset='utf8')
    cursor = db.cursor()
    # ebaynos =[ '152329556836', '121698891442', '161719488555', '130982977280', '121801322206']
    arrs =  ', '.join(list(map(lambda x: '%s', ebaynos)))
    sql = ' select p.ebayno, p.category FROM product p where p.ebayno in (%s)'%(arrs)
    cursor.execute(sql,ebaynos)
    data = cursor.fetchall()#元祖中的元素也是元祖
    if len(data) == 0:
        print("viogidataer搜索结果为空")
        return
    data = np.array(data) #将元祖转换成数组
    df = DataFrame(data , columns=["ebayno","category"])#创建对象DataFrame,参数值为数组
    df.to_csv("category_viogidataer.csv" , index=False) #输出
    db.commit()
    cursor.close()
    db.close()
ebaynos = [] #读入ebaynos
with open("test1.txt") as fp:
    for line in fp:
        ebaynos.append(line.strip())
findFromViogiData(ebaynos) #从viogidata读取数据,并放入category_viogidata.csv
findFromViogiDataer(ebaynos) #从viogidataer读取数据,并放入category_viogidataer.csv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值