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.
Related
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
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.
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.
I am using python 2.7 for a specific job. I am connecting to MSSQL Server (2008) using FreeTDS. I can make some simple select queries but when I try to run a parametrised query I got an error:
('HY004', '[HY004] [FreeTDS][SQL Server]Invalid data type (0) (SQLBindParameter)')
Here is my query:
query = u"UPDATE table SET column1=? WHERE column2=?"
cursor.execute(query,[param1, param2])
However the same code on live works fine.
I have skimmed so many thread in various forums but they all seem misleading and I am really confused.
What is my actual problem and what do you suggest?
Edit: I've added query.
I know this is a super old thread, but I came across this same problem and the solution for me was to type cast the variables. For instance:
query = u"UPDATE table SET column1=? WHERE column2=?"
cursor.execute(query,[str(param1), str(param2)])
In this case it doesn't really matter what type the parameters are as it will be converted to a string.
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.