Storing output of SQL Query in Python Variable

2024/10/10 2:18:14

With reference to this, I tried modifying my SQL query as follows:

query2 ="""insert into table xyz(select * from abc where date_time > %s and date_time <= ( %s + interval '1 hour'))"""
cur.execute(query2,(rows,rows))

It gave an error saying

function takes at most 2 arguments(3 given)

Is there any work around for this error?

Answer

There is something fishy about your query, it does not look right:

insert into table xyz
(select * from abc where date_time = %s and %s + interval '1 hour')

I would suggest:

insert into xyz (<columns of xyz>)
select <columns of abc> 
from abc 
where date_time > ?  

The query have been edited so it should be:

insert into xyz (<columns of xyz>)
select <columns of abc> 
from abc 
where date_time > ? and date_time <= ? + interval '1 hour'

Now when you execute your query:

cur.execute(query2,(ts, ts))

The number of parameter markers (?) in the query string should be equal to the number of elements in the tuple .

If you are using %s in your query (not recomended) you can assign values to those variables with:

"""insert into xyz (<columns of xyz>)select <columns of abc> from abc where date_time > %s""" % (value)
https://en.xdnf.cn/q/118506.html

Related Q&A

file modification and creation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

How to share a file between modules for logging in python

I wanted to log messages from different module in python to a file. Also I need to print some messages to console for debugging purpose. I used logger module for this purpose . But logger module will l…

Dont understand how this example one-hot code indexes a numpy array with [i,j] when j is a tuple?

I dont get how the line: results[i, sequence] = 1 works in the following.I am following along in the debugger with some sample code in a Manning book: "Deep Learning with Python" (Example 3.5…

convert nested list to normal list using list comprehension in python [duplicate]

This question already has answers here:How do I make a flat list out of a list of lists?(32 answers)Flatten an irregular (arbitrarily nested) list of lists(54 answers)Closed 6 years ago.How can I do t…

Transformation of pandas DataFrame adds a blank row

My original question was posted here. I have a dataframe as follows:ID START END SEQ 1 11 12 1 1 14 15 3 1 13 14 2 2 10 14 1 3 11 15 1 3 16 17 …

How to find a element after a search click checkbox in selenium Python

After I search for a user in the search field, I get the user I searched Now I need to select this user that shown in the list I tried with xpath and did not find the element ! could you help ? So aft…

Running parameterized queries

Quite new to this google bigquery sql thing so please bear with me. Im trying to build a google standardSQL parameterized query. The following sample was used and ran successfully on Google BigQuery We…

how to do this disparity map in OpenCV

paper "Fast Obstacle Detection Using U-Disparity Maps with Stereo Vision"reference paper: "Fast Obstacle Detection Using U-Disparity Maps with Stereo Vision"I want to ask can opencv…

How do I give out a role when I click on a reaction? It doesnt work for me?

Im trying to make sure that when you enter a command, a message is sent and a reaction is set, and those who click on the reaction get a role, but when you enter a command, nothing happens. Here is the…

Python Pandas DataFrame Rounding of big fraction values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…