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

  • Home
  • SEARCH
  • 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 6679245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:20:14+00:00 2026-05-26T04:20:14+00:00

I am new to python, mongodb and sql. I am working in Eclipse 3.7.1

  • 0

I am new to python, mongodb and sql. I am working in Eclipse 3.7.1 on a Mac 10.7. I am using a pyodbc driver (and freeTDS) to connect to an mssql database. I am scripting in python 2.7. I want to query the mssql database and write it out to a mongo database.

Where I stumble is that the query output is in a python list of tuples without field names, and I am looking for a way to convert this list of tuples into a form that mongodb will import.

Current script:

    ############
    # Query mssql
    import pyodbc
    import json
    url = 'DSN=myServer;UID=myUserName;PWD=myPassword;PORT=1433;DATABASE=mydb'
    pyodbccon = pyodbc.connect(url)
    cursor = pyodbccon.cursor()

    numusersQ = "SELECT COUNT(users.userid) FROM users"; 
    cursor.execute(numusersQ); numusers = cursor.fetchall()
    nummembsQ = "SELECT COUNT(memberships.membernumber) FROM memberships"; 
    cursor.execute(nummembsQ); nummembs = cursor.fetchall()
    userclientQ = "SELECT users.userid, users.client, users.industry FROM users"
    cursor.execute(userclientQ); userclient = cursor.fetchall()

    #format key value tuples
    output = []
    for row in userclient:
        tuplenew = {'userid': row[0], 'client': row[1], 'industry': row[2], 'numusers': numusers, 'nummembs': nummembs}
        output = [output, tuplenew]


    #output to mongo
    from pymongo.connection import Connection ;
    conmongo = Connection('localhost') 
    db = conmongo.mypymongodb

    for key, value in output():
        temp = [key,value]
        mongooutput.append(temp)

    db.pymongocollection.save(mongooutput)
    cursor = db.pymongocollection.find() 

############

OUTPUT looks like:

[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[], {'numusers': [(103068, )], 'industry': 'npwild', 'client': 'cmh', 'userid': 1, 'nummembs': [(10519, )]}], {'numusers': [(103068, )], 'industry': 'npwild', 'client': 'cmh', 'userid': 2, 'nummembs': [(10519, )]}], {'numusers': [(103068, )], 'industry': 'npwild', 'client': 'cmh', 'userid': 3, 'nummembs': [(10519, )]}], {'numusers': [(103068, )], 'industry': 'npwild', 'client': 'cmh', 'userid': 5, 'nummembs': [(10519, )]}]

ERROR MESSAGE:

Traceback (most recent call last):
  File "/Users/eclipse/workspace/pymongo/pymongopkg.py", line 34, in <module>
    for key, value in output():
TypeError: 'list' object is not callable

If anyone can just suggest a function or direct me to a solution I’d be ever so greatful.

  • 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-05-26T04:20:14+00:00Added an answer on May 26, 2026 at 4:20 am

    this is how i got it to work:

    1. pyodbc to query mssql database
    2. python dict and zip to convert list of tuples into key-value dictionary pairs
    3. pymongo to save it as collection

      #===============================================================================
      # 1. MSSQL QUERY WITH PYODBC
      #===============================================================================
      import pyodbc
      url = 'DSN=myserver;UID=myusername;PWD=mypassword;PORT=1433;DATABASE=mydatabase;'
      pyodbccon = pyodbc.connect(url); cursor = pyodbccon.cursor()
      
      userdataQ = "SELECT users.userid, users.client, users.industry FROM users"
      cursor.execute(userdataQ); userdata = cursor.fetchall()
      
      ##===============================================================================
      ## 2. convert tuple list to key-value dictionary
      ## 3. export to mongodb
      ##===============================================================================        
      from pymongo import Connection; conmongo = Connection('localhost') 
      db = conmongo.mypymongodb #mypymongodb = dbname
      headers = ['userid','client','industry'] 
      
      for tup in userdata:
          nextdoc = dict(zip(headers, tup))
          db.usercollection.save(nextdoc)
      print "usercollection in mypymongodb updated with " + str(db.usercollection.count()) + " docs"
      

    output:

    > db.usercollection.find()
    { "_id" : ObjectId("4ef000000"), "industry" : "npwild", "client" : "cmh", "userid" : 1 }
    { "_id" : ObjectId("4ef000001"), "industry" : "npwild", "client" : "cmh", "userid" : 2 }
    { "_id" : ObjectId("4ef000002"), "industry" : "npwild", "client" : "cmh", "userid" : 3 }
    etc.
    

    thank you for your help!
    -d

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

Sidebar

Related Questions

I am new to Python, and I'm working on writing some database code using
I'm a new Python programmer who is having a little trouble using 'self' in
New to python and trying to learn the ropes of file i/o. Working with
I'm trying to create an environment using virtualenv. virtualenv test New python executable in
I'm using Eclipse on Windows, with the PyDev plugin for Python development. When I
New to python and django. Using the forms module and going through the errors
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1.
Im new two python and am trying to grow a dictionary of dictionaries. I
I am new to python and struggling to find how to control the amount
I'm relatively new to Python and am having problems programming with Scapy, the Python

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.