所以我使用下面的代码从SQL Server中的某个表中选择列。
sus_base = pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE]", con)
df1 = pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%car%'", con)
df2= pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%plane%'", con)
df3= pd.read_sql_query("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%quality%'", con)
但是,我必须为很多字符串进行匹配。我在Python中尝试了以下功能:
test = pd.read_sql_query(("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%s%'", input()), con)
在向我询问输入后,它给出了以下错误:
DatabaseError: Execution failed on sql '("SELECT screen_name, user_id, text FROM [dbo].[TABLE_ONE] WHERE text like '%s%'", "'car'")': bytes or integer address expected instead of tuple instance.
是的,我也尝试过没有'引用汽车。
然后df1看起来像:
JAN | 20938 | Hi I am looking for a car somewhere
BOB | 38470 | Guys, where is my plane?
ELSA| 49850 | Never know what to do next..
CARL| 08923 | The quality of this product is amazing!
ETC.
你可以想象创建它会有很多工作要做。因此,如果最终用户只需键入他在文本字符串中查找的单词就会很好,并且它会像我在上面的示例中键入时那样返回那些文本。然后我会确保它进入数据帧和/或Excel文件。
答案 0 :(得分:0)
输入未被翻译成pandas需要发送查询的文本。在该行:
pd.read_sql_query(("SELECT ... WHERE text like '%s%'", input()), con)
实际喂食的是pd.read_sql_query((str, str), con)
。换句话说,您发送了一个2元组的字符串和连接对象,它只需要一个字符串和连接对象。请尝试格式化字符串:
pd.read_sql_query("SELECT ... WHERE text like %{}%".format(input()), con)