PYODBC query not running but works fine in MSSMS - python

I am trying to run a large query set up at work, maybe a stored procedure.?. I am unfamiliar with large queries but i think that is the issue. I can use pyodbc to connect and run a simple "SELECT * FROM db;" and it works fine, and if i run the full query in MSSMS and it works fine, but not when I copy the large query into the query variable
I have found a few articles that say to add "SET NOCOUNT NO;" I tried that and it didn't work either.
the python error is:
"No results. Previous SQL is not a query"
Once again the query works fine in MSSMS so any guidance would be appreciated.
thanks

Related

Use multi=True error when calling stored procedures from flask

Hello I get the following error on certain stored procedures. "Use multi=True when executing multiple statements" We're using python 3.7 , flask and mysql-connector. I encountered this problem on a stored procedure previously and was able to get rid of it by changing a select statement like the following select column1, column2 from table to a select *. I've also run into this same error with another stored procedure that is using a cross join but have been unable to solve it.
Now these stored procedures run fine from workbench but they have issues when calling the api which uses mysql-connector callproc method. They were also working fine in the API back when i wrote the API code. This problem seems to have appeared recently. Below is a generic code snippet.
cursor = cnxn.cursor();
cursor.callproc('storedprocedure', args) #error line
I've tried setting multi=True for the cursor but that doesn't do anything. Was wondering if anybody had any ideas on what might cause this issue.

Difference between spark.sql() and cursor.execute in pyspark?

what is difference in working of both. as i am getting result in executing below command.
spark.sql("select * from MetadataTable").show()
but when i am trying to run cursor.execute("select * from MetadataTable"), it is showing me error
"metadatatable" does not exist
what should I do access table "metadatatable" by cursor.execute?
At a glance, spark.sql is the spark way to use SQL to work with your data.
Cursor.execute does not appear to be spark code.
Perhaps it is python code for interaction with a database.
You could share the documentation if this seems wrong, but probably reviewing the documentation should explain what it is.

Is there a way to run a saved SQL query from Python (using cx_Oracle)?

I have been using cx_Oracle to perform SQL queries on an Oracle database in Python. So far I have been pasting these queries into strings and then running them using the cursor.execute() function that comes with cx_Oracle:
#simple example
query = """SELECT *
FROM my_table"""
cursor.execute(query)
However, my select queries have gotten quite complex, and the code is starting to look a bit messy. I was wondering if there were any way to simply save the SQL code into a .sql file and for Python or cx_Oracle to call that file? I thought something like that might be simple to find using Google, but my searches are oddly coming up dry.
Well, you can certainly save SQL code to a file and load it:
query = open('foo.sql', 'r').read()
cursor.execute(query)
I can't find any reference to saved queries in cx_Oracle, so that may be your best bet.

Mysql join query works in Mysql Workbench, but hangs at PyDev

I have been trying to run this query in PyDev environment for a while, but it just hangs there.
The tables involved are large (tweets around 700,000 and user about 400,000). I have all the indexes sorted out. I tried to run the query at Workbench and it works perfectly within seconds, but just hangs while executed in Eclipse...
cursor.execute("SELECT id, user, hashtags FROM users JOIN tweets ON users.user_id = tweets.user")
It seems that I don't understand the buffering associated with executing at Workbench vs any other environment...!
Any thoughts or ideas?

What is the process for using MySQL from Python in Windows?

I come from a PHP background where MySQL easily works from PHP, and don't the process for getting MySQL to work from Python. From all the research i did and reading of similar yet non-exact questions, it seems to me that there are different ways to achieve this, which makes it even harder for me to wrap my head around. So far I have MySQL-python-1.2.3 installed for python 2.7.1 in Windows XP 32Bit. Can anyone give me an overview of what is necessary to get MySQL working from Python in Windows, or even what is next after my steps all the way to fetching a table row? Thanks in advance.
UPDATE:
#Mahmoud, using your suggestion i have triggered the following:
If you just want to use the DBAPI, then here's a simple snippet explaining how to issue a SELECT query.
import MySQLdb
db = MySQLdb.connect(host="host", user="username", passwd="your-pass", db="the-db-name")
To perform a query, you first need a cursor, and then you can execute queries on it:
cursor = db.cursor()
max_age = 42
cursor.execute("""SELECT name FROM employees WHERE age < %s""", (max_age,))
print cursor.fetchone()
However, you most likely want to use an ORM, I recommend SQLAlchemy. It essentially trivializes database interaction by providing a super-powerful abstraction layer.

Categories

Resources