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 8876817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:18:21+00:00 2026-06-14T19:18:21+00:00

I have this code which retrieves data from a mysql table. I am using

  • 0

I have this code which retrieves data from a mysql table. I am using Python’s MySQLdb module. I want EACH column’s data based on the SELECT WHERE condition to be retrieved under an array. For instance, in the code below, I want all the data where location field is ‘NY, US’ to be retrieved under different arrays – with each array representing different columns values.

import numpy
import MySQLdb

db = MySQLdb.connect("localhost", "root", "", "test")
cursor = db.cursor()

sql = "SELECT * FROM usa_new WHERE location = 'NY, US'"
try:
   cursor.execute(sql)
   results = cursor.fetchall()
   discresults = {}
   for row in results:

      id = row[0]
      location = row[1]
      temp_f = row[2]
      pressure_mb = row[3]
      wind_dir = row[4]
      wind_mph = row[5]
      relative_humidity = row[6]
      timestamp = row[7]

except:
   print "Error: unable to fecth data"

db.close()

Is there something going wrong?

  • 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. Editorial Team
    Editorial Team
    2026-06-14T19:18:27+00:00Added an answer on June 14, 2026 at 7:18 pm

    There is a data structure called ‘list’ in python which you can use as array.
    If your question’s semantic what I understood is
    “Get the result in arrays categorized by columns, to be stored in local lists”, so here is simple implementation you can do:
    remember I have fetched rows one by one matching the given criteria; as its a good practice;

    import MySQLdb
    
    db = MySQLdb.connect("localhost", "root", "", "test")
    cursor = db.cursor()
    id, location, temp_fm, pressure_mb, .. = [],[],[],[],...
    //for the number of lists you want to create, just add their names and a empty list
    sql = "SELECT * FROM usa_new WHERE location = 'NY, US'"
    
    try:
       cursor.execute(sql)
    
       rcount = int(cursor.rowcount)
    
       for r in rcount:
          row = cursor.fetchone()
    
          id.append(row[0])
          location.append(row[1])
          temp_f.append(row[2])
          pressure_mb.append(row[3])
          wind_dir.append(row[4])
          wind_mph.append(row[5])
          relative_humidity.append(row[6])
          timestamp.append(row[7])
    
    except:
       print "Error: unable to fecth data"
    
    db.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script which retrieves data from MySQL Database. this is the
I have this code which retrieves some data from a sql query and place
I have this simple while loop which retrieves data from a mysql query and
I have this code which will include template.php file from inside each of these
I have a query which retrieves the data from Oracle DB. The table which
i have this code, which helps me to retrieve all table fields and associate
I have this code (which is way simplified from the real code): public interface
I have the following code, which retrieves the page slugs from the database which
I have this code which executes a search on about 30-40 columns using a
I have an ajax code which passes data to php and then retrieves data

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.