python 从excel取出数据 作为sql 查询参数方法

data=pd.read_excel(r'C:\Rep012.xlsx',header=0) sql=select * from user where code1 in(%s)
cur.execute(sql,data)
报错:
valueerror:The truth value of aDataFrame is ambiguous. use a.empty,a.bool(),a.item(),a.any()
可有其它已经实践可用方法

从表格中读取数据,要取某列作为查询字段,修改sql查询语句,参数要用元组形式。示例供参考:

import pandas as pd 
import sqlite3

conn=sqlite3.connect('data/1.db')
cur=conn.cursor()
df=pd.read_excel('data/1.xlsx')
code1=df['查询2'].dropna()
sql = f'''select * from students where name in {tuple(code1)}''' 
for x in cur.execute(sql):
    print(x)

你print(data)和print(type(data))看看数据和数据类型,然后print(sql)看看数据就知道什么问题了

thanks,我按照执行时有两个问题请教下,
1sqlite3连接方式,不是太懂。我是用的pymssql,
2.execute执行时报错AttributeError: 'NoneType' object has no attribute 'fetchall'

@CSDN专家-HGJ 我的excel中只有一列数据,你可以看下面图片,现在说对象不能迭代

img

看下面图 其实已经通过sql 得到list 中值,但ata=cur.fetchall()执行时错误,是因为需要在每个list 值两边加''号吗?查资料好像无这要求。麻烦看下

2

改成fetchone 已经可以 但是通过查询条件值能查出一条记录,要怎样根据所有查询条件重新取得值呢,一定要用fetchall吗

img

将excel整理的表结构生成insert建表sql:https://download.csdn.net/download/gongjin28_csdn/85486975