Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:38:24+00:00 2026-05-11T06:38:24+00:00

I have some code which I’ve been using to query MySQL, and I’m hoping

  • 0

I have some code which I’ve been using to query MySQL, and I’m hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn’t work with SQLite:

cursor.execute(query)  rows = cursor.fetchall()  data = [] for row in rows   data.append(row['column_name']) 

This gives the following error:

 TypeError: tuple indices must be integers 

Whereas if I change the reference to use a column number, it works fine:

  data.append(row[1]) 

Can I execute the query in such a way that I can reference columns by their names?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:38:25+00:00Added an answer on May 11, 2026 at 6:38 am

    I’m not sure if this is the best approach, but here’s what I typically do to retrieve a record set using a DB-API 2 compliant module:

    cursor.execute('''SELECT foo, bar, baz, quux FROM table WHERE id = %s;''',                    (interesting_record_id,))  for foo, bar, baz, quux in cursor.fetchall():     frobnicate(foo + bar, baz * quux) 

    The query formatting method is one of the DB-API standards, but happens to be the preferred method for Psycopg2; other DB-API adapters might suggest a different convention which will be fine.

    Writing queries like this, where implicit tuple unpacking is used to work with the result set, has typically been more effective for me than trying to worry about matching Python variable names to SQL column names (which I usually only use to drop prefixes, and then only if I’m working with a subset of the column names such that the prefixes don’t help to clarify things anymore), and is much better than remembering numerical column IDs.

    This style also helps you avoid SELECT * FROM table..., which is just a maintenance disaster for anything but the simplest tables and queries.

    So, not exactly the answer you were asking for, but possibly enlightening nonetheless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Rex, first a few questions: Is this a WinForms/WPF app… May 11, 2026 at 11:53 am
  • added an answer In the spirit of DRY, I would say this is… May 11, 2026 at 11:52 am
  • added an answer Games.find( :all, :limit => 5, :include => [:games, :requests], :group… May 11, 2026 at 11:52 am

Related Questions

I have some code which I am making available via RMI. If my program
I have some code which I did not originally create that uses _beginthreadex and
I have some code which collects points (consed integers) from a loop which looks
I have some code which utilizes parameterized queries to prevent against injection, but I
I have some code which needs to ensure some data is in a mysql
I have some code which returns InnerXML for a XMLNode. The node can contain
I have some code which ignores a specific exception. try { foreach (FileInfo fi
I have have some code which adds new cells to a table and fills
I have some Ruby code which takes dates on the command line in the
I have some JS code which generates the following object, return { type: some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.