storing result of MySQL query into python variable - python

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())

Related

Trying to create a query using python and mariadb

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.

TypeError: 'float' object has no attribute '__getitem__' in Python

I am working on a project in Python. I am a beginner and I am getting this error when I am running the program.
Traceback (most recent call last):
File "E:/Python/1616/checkProfile.py", line 104, in <module>
p.getResults()
File "E:\Python\1616\Profile.py", line 67, in getResults
for i in range(2): self._s[1] += e.getS[1]
TypeError: 'float' object has no attribute '__getitem__'
Error in Line 67
http://pastebin.com/HXvppfmU
to check what are the methods allowed for datatypes,
dir(datatype)
# As for float, try dir(float)
#This will suggest you that there is no method called __getitem__ for float.
# You might be trying to get some data or you are using []/()/{} which is not correct.
Try to post ur code.

Python 3: Is there a way to get the line number of an error and the error statement when using exec [duplicate]

This question already has an answer here:
how to get the line number of an error from exec or execfile in Python
(1 answer)
Closed 7 years ago.
Is there a way to get the line number of the error through executing a file? Let's say that I have the following code:
exec(open("test.py").read())
And the file has an error, in which IDLE pops up the following:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:\Users\henrydavidzhu\Desktop\Arshi\arshi.py", line 349, in runFile
exec(open(self.fileName).read())
File "<string>", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
I want to know the error statement (TypeError: unsupported operand type(s) for +: 'int' and 'str'), and the line number. This is not a duplicate because I am using Python 3, and not Python 2.
You can try this code:
import sys
import traceback
try:
exec(open('test.py').read())
except Exception as e:
exc_type, ex, tb = sys.exc_info()
imported_tb_info = traceback.extract_tb(tb)[-1]
line_number = imported_tb_info[1]
print_format = '{}: Exception in line: {}, message: {}'
print(print_format.format(exc_type.__name__, line_number, ex))

Python error message

How can I fix this error:
Traceback (most recent call last):
File "C:\Users\Tony\Desktop\Python\4.py", line 64, in
print "YOU PAY: $",(pc-total)
TypeError: unsupported operand type(s) for -: 'str' and 'float'
One of the two, pc or total, is a float and the other a string.
As python is strongly typed you would need to cast the string to a float, e.g.:
print "YOU PAY $",(float(pc) - total)

mongoengine datetime field and python datetime

I am facing the following issue which I didnt came across in my other application.
My Sprint datamodel:
import mongoengine as me
class Sprint(me.Document):
start_date = me.DateTimeField(),
end_date = me.DateTimeField(),
sequence = me.IntField(required=True, default=0)
In shell I tried following:
sprint = Sprint.objects.get(sequence=1)
sprint
<Sprint: Sprint object>
sprint.start_date - datetime.utcnow()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'tuple' and 'datetime.datetime'
then I printed sprint.start_date
it returned a tuple instead of datetime object as follows:
sprint.start_date
(<mongoengine.fields.DateTimeField object at 0x22b7dd0>,)
So I did
sprint.start_date[0] - datetime.utcnow()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'DateTimeField' and 'datetime.datetime'
I didn't get this issue any of my earlier project. My current mongoengine version is 0.6.20. I didn't upgarded it for my current project and has been using this for all of my projects.
I am using tornado as the web server
How to convert mongoengine datetime field to compatible to python datetime instance.
Thanks in advance for your help.
The error is in the datamodel definition itself. A type caused all these. :(
start_date and end_date have a ',' in the end which is creating the noise.
After removing it, it rolls. :)

Categories