Trying to create a query using python and mariadb - python

Sorry, this may be really easy but I am new at learning code,
I am trying to return all
yesterday = ((dt.date.today()-dt.timedelta(days=1)))
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)"(yesterday))
I am getting this back
Traceback (most recent call last):
File "/home/pi/Documents/openoptions.py", line 86, in <module>
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)"(yesterday))
TypeError: 'str' object is not callable

yesterday = ((dt.date.today()-dt.timedelta(days=1)))
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)", (yesterday,))
note the 2 commas. You may need to convert yesterday to str.

Related

Type error in python 2.7

Traceback (most recent call last):
File "C:\Python27\meanshift.py", line 15, in <module>
roi = frame[r:r+h, c:c+w],
TypeError: 'NoneType' object has no attribute '__getitem__'
Please help me to resolve this error, I am trying to make a region of interest on the first frame to track an object.
Usually I get this kind of errors if I forgot to put a return statement at the end of a function:
def create_array(n):
a = np.array((n,5))
b = create_array(10)
print b[5,1]
Without an explicit return a at the end of the function, it will return None.

pickle, 'str' does not support buffer interface in python

Ok so I have the following code:
def rate_of_work(self):
global rate
rate = (turtle.textinput("Your Work Rate","What is your hourly work rate in US Dollars?"))
outFile_rate = pickle.dumps(rate)
rate1 = pickle.loads(rate)
rate2 = ((hours*rate1) + (minutes*rate1)*0.0167 + (seconds*rate1)*0.000278) #isnt necessary information
rate3 = round(rate2, 2) #isnt necessary information
im getting the error:
rate1 = pickle.loads(rate)
TypeError: 'str' does not support the buffer interface
please help
I assume you are working under python 3.x
For the specific error, pickle.loads() only accepts bytes, and you are trying to give a plain string to it, that's why it fails.
>>> pickle.loads("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
>>> pickle.loads(b"")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError

Syntax for changing a python script from sqlite3 to mysql

I have a working python script which retrieves data from a sqlite3 db. I need to convert it to talk to a mysql db though, and here's the first line that is giving me grief:
PRODUCT_CODE_ID = "SELECT id FROM localsite_productcode WHERE localsite_productcode.product_code=?"
cur.execute(PRODUCT_CODE_ID,(product_code,))
When I try that with mysql I get the following error:
Traceback (most recent call last):
File "gen-csv.py", line 85, in <module>
cur.execute(PRODUCT_CODE_ID,(product_code,))
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
What am I doing wrong here?
I don't think the DB API for MySQL supports the ? as a place holder - just try %s instead.

SQLAlchemy Error when filtering

I'm using using SQLAlchemy to fiter but getting an error:
user = session.query.filter(User.id == 99).one()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'filter'
Does someone know how to filter because on SQLAlchemy Page, I saw this:
query = session.query(User).filter
query is a function, you need to pass in the User class to call it:
user = session.query(User).filter(User.id == 99).one()
^^^^^^
SQLAlchemy cannot divine from the filter alone what type of object you want returned otherwise.

storing result of MySQL query into python variable

when I execute the following code using python programming language and MySQL database
cursor.execute("select max(propernoun_SRNO) from tblauto_tagged")
starting_index = cursor.fetchone()
ending_index = starting_index +len(s)
I get following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
batch(1,1)
File "C:\Users\vchauhan\Dropbox\Code\proper_noun_function_batch_file_mysql_sept_12.py", line 97, in batch
ending_index = starting_index +len(s)
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'int'
Problem
The problem here is that you are assigning pyodbc.Row instance (returned by .fetchone()) to starting_index, which makes it impossible to add it to the integer (thus the "TypeError: unsupported operand type(s)" error).
Solution
Try to replace this line:
starting_index = cursor.fetchone()
with this line:
starting_index = cursor.fetchone()[0]
More reading
PEP 249 - Python Database API Specification v2.0 (especially part about fetchone())

Categories