I to be trying to pull data from a database and assign castle to different lists.This particular error is giving me a the majority of trouble "TypeError: tuple indices have to be integers, not str" ns tried converting it to float and also etc, yet to no success.

You are watching: Tuple indices must be integers or slices, not str

The password goes together below

conn=MySQLdb.connect(*details*)cursor=conn.cursor()ocs=oltv=query="select pool_number, average_credit_score together waocs, average_original_ltv together waoltv from *tablename* where as_of_date= *date*"cursor.execute(query)result=cursor.fetchall()for row in result: print row ocs>=int(row<"waocs">) oltv>=int(row<"waoltv">)Sample calculation of publish statement is as adheres to :

("MA3146", 711L, 81L)("MA3147", 679L, 83L)("MA3148", 668L, 86L)And this is the precise error ns am getting:

ocs>=int(row<"waocs">)TypeError: tuple indices should be integers, not strAny assist would it is in appreciated! many thanks people!


python sql database
share
enhance this question
follow
edited Apr 25 "20 at 4:42
user12969751
request Feb 12 "16 at 10:16
*

Harsha JastiHarsha Jasti
91411 yellow badge88 silver- badges2323 bronze title
0
add a comment |

8 answer 8


active oldest Votes
71
Like the error says, row is a tuple, so you can"t carry out row<"pool_number">. You need to use the index: row<0>.


re-publishing
boost this answer
follow
reply Feb 12 "16 in ~ 10:20
*

Daniel RosemanDaniel Roseman
552k5757 yellow badges781781 silver- badges805805 bronze title
2
include a comment |
32
I think you should do

for index, row in result: If girlfriend wanna access by name.


re-superstructure
improve this price
follow
answer Nov 7 "19 at 2:28
*

Sitti Munirah Abdul RazakSitti Munirah Abdul Razak
54955 silver- badges99 bronze title
include a comment |
11
TL;DR: add the parameter cursorclass=MySQLdb.cursors.DictCursor in ~ the finish of her MySQLdb.connect.

I had a functioning code and the DB moved, I had to adjust the host/user/pass. After ~ this change, my code quit working and also I started gaining this error. ~ above closer inspection, i copy-pasted the link string ~ above a location that had an extra directive. The old code read like:

conn = MySQLdb.connect(host="oldhost", user="olduser", passwd="oldpass", db="olddb", cursorclass=MySQLdb.cursors.DictCursor)Which was replaced by:

conn = MySQLdb.connect(host="newhost", user="newuser", passwd="newpass", db="newdb")The parameter cursorclass=MySQLdb.cursors.DictCursor in ~ the finish was do python permit me to access the rows utilizing the tower names as index. However the negative copy-paste got rid of that, yielding the error.

So, together an alternate to the solutions currently presented, you deserve to also include this parameter and access the rows in the means you initially wanted. ^_^ i hope this helps others.


re-publishing
boost this prize
follow
edited might 24 "18 in ~ 17:50
answered may 7 "18 in ~ 23:48
*

msbmsb
2,87111 yellow badge2626 silver badges3838 bronze title
2
include a comment |
4
The problem is just how you accessibility row

Specifically row<"waocs"> and also row<"pool_number"> of ocs>=int(row<"waocs">)

If friend look up the official-documentation that fetchall() girlfriend find.

The technique fetches every (or all remaining) rows the a query an outcome set and returns a list of tuples.

Therefore you have actually to accessibility the values of rows v row<__integer__> prefer row<0>


re-publishing
enhance this prize
follow
reply Feb 12 "16 in ~ 10:21
*

JDurstbergerJDurstberger
3,79744 gold badges2828 silver badges6363 bronze badges
add a comment |
3
Just adding a parameter choose the listed below worked because that me.

cursor=conn.cursor(dictionary=True)I expect this would be advantageous either.


re-superstructure
enhance this price
follow
answer Oct 18 "20 at 5:19
MironMiron
89522 gold badges1414 silver badges3030 bronze title
include a comment |
2
I recognize it is not particular to this question, however for anyone coming in indigenous a Google search: this error is likewise caused by a comma behind things that creates a tuple rather than a dictionary

Tuple

Dictionary

share
boost this prize
follow
edited Apr 24 in ~ 3:24
chemicalwill
85611 yellow badge66 silver- badges1515 bronze title
answered Apr 23 at 14:42
martin CronjeMartin Cronje
4866 bronze badges
add a comment |
0
SQlite3 has actually a an approach named row_factory. This an approach would allow you to accessibility the worths by column name.

https://www.kite.com/python/examples/3884/sqlite3-use-a-row-factory-to-access-values-by-column-name


re-publishing
enhance this price
monitor
answered Feb 22 at 1:56
Raccon32Raccon32
1
1
include a comment |
0
I watch that you"re make the efforts to determine by the name of a row. If friend are searching for a particular column in ~ the row, you can do

For example, to iterate with each row and only pull the end the worth from the row through the shaft header the "pool number", you have the right to do this:

for row in df_updated.iterrows(): cell = row<1><"pool number"> print(cell)The code will then iterate through each row however only print out the value that matches the "pool number" column


re-superstructure
boost this price
monitor
answered may 5 at 18:51
Robin DuongRobin Duong
4111 silver- badge99 bronze badges
add a comment |

your Answer


Thanks because that contributing response to stack Overflow!

Please be sure to answer the question. Carry out details and also share your research!

But avoid

Asking because that help, clarification, or responding to various other answers.Making statements based upon opinion; back them increase with references or an individual experience.

To find out more, check out our advice on writing good answers.

See more: Call Of Duty Modern Warfare Pc Black Friday, Want To Play Call Of Duty


Draft saved
Draft discarded

Sign up or log in in


sign up using Google
authorize up utilizing Facebook
authorize up making use of Email and Password
send

Post as a guest


surname
email Required, yet never shown


Post as a guest


name
email

Required, however never shown


write-up Your prize Discard

By clicking “Post your Answer”, girlfriend agree come our regards to service, privacy policy and cookie plan


Not the price you're looking for? Browse various other questions tagged python sql database or ask your own question.


The Overflow Blog
Featured ~ above Meta
Linked
3974
Understanding slice notation
0
Is over there any means to accessibility SQL heat data by the obelisk name?
connected
1
perform indices have to be integers, no str
29
Python and JSON - TypeError perform indices must be integers not str
1
Why carry out I obtain this error: TypeError: tuple indices should be integers or slices, no str
0
Python nmap: TypeError: list indices should be integers or slices, not str
0
i had Error exemption has occurred: TypeError tuple indices need to be integers or slices, not str (Open CV)?
0
TypeError: heat indices must be integers, not str In python insert questions
1
"Tuple indices need to be integers or slices, not str" when loading Tensorflow Dataset
warm Network inquiries an ext hot inquiries

inquiry feed
subscribe to RSS
concern feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


default
ridge Overflow
assets
agency
ridge Exchange Network
site design / logo © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.9.28.40331


Stack Overflow works ideal with JavaScript enabled
*

your privacy

By clicking “Accept all cookies”, friend agree ridge Exchange deserve to store cookies on your maker and disclose information in accordance with our Cookie Policy.