0

I currently have a python script that can connect to a mySQL db and execute queries. I wish to modify it so that I can connect to a different SQL db to run a separate query. I am having trouble doing this, running osx 10.11. This is my first question and I'm a newbie programmer so please take it easy on me...

Here is the program i used to for mySQL

sf_username = "user"
sf_password = "pass"
sf_api_token = "token"

sf_update = beatbox.PythonClient()
 password = str("%s%s" % (sf_password, sf_api_token))
sf_update.login(sf_username, password)

t = Terminal()

hub = [stuff]

def FINAL_RUN():
    cnx = alternate_modify(hub)
    cur = cnx.cursor()
    queryinput = """
    SQL QUERY I WANT
    """
    cur.execute(queryinput)
    rez = cur.fetchone()
    while rez is not None:
        write_to_sf(rez)
        rez = cur.fetchone()

FINAL_RUN()
Malakaman
  • 63
  • 1
  • 1
  • 4
  • It will be much easier to you (as long as you're _newbee_ as you said) not to deal with some generic case, but to ask more specific question. As long as, for example, handling MsSQL/PostgreSQL/Oracle may differ lot in a short while after to connect to it – agg3l Oct 16 '16 at 00:32
  • You could always check the database you're storing to and execute different SQL queries depending on the database. Or, you could use an ORM like SQLAlchemy. – Sean Francis N. Ballais Oct 16 '16 at 11:13

2 Answers2

0

You can use a Python library called SQLAlchemy. It abstracts away the "low-level" operations you would do with a database (e.g. specifying queries manually).

A tutorial for using SQLAlchemy can be found here.

Sean Francis N. Ballais
  • 2,338
  • 2
  • 24
  • 42
0

I was able to get connected using SQL Alchemy--thank you. If anyone else tries, I think you'll need a ODBC driver per this page:

http://docs.sqlalchemy.org/en/latest/dialects/mssql.html

Alternatively, pymssql is a nice tool. If you run into trouble installing like I did, there is a neat workaround here:

mac - pip install pymssql error

Thanks again

Community
  • 1
  • 1
Malakaman
  • 63
  • 1
  • 1
  • 4