I am trying to execute SQL query from a file in Python 2.7.13 and getting the following error while displaying the resultset.
The SQL statements in the file are simple like count(*) from table but if this logic works I need to replace it with complex queries.
Error
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 19, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
Python Script:
import pymssql
conn = pymssql.connect(
host=r'name',
user=r'user',
password='credential',
database='Test')
c = conn.cursor()
fd = open('ZooDatabase.sql', 'r') # Open and read the file as a single buffer
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';') # all SQL commands (split on ';')
for command in sqlCommands: # Execute every command from the input file
c.execute(command)
fields = c.fetchall()
for row in fields:
print "Info : %s " % str(row)
c.close()
conn.close()
Error Message
**SQL File - ZooDatabase.sql**
select count(*) from emp2;
select count(*) from emp1;
**Error Log with SQL print statement output:**
C:\Python27\pycode>python SQLserver_loop.py
SELECT count(*) FROM emp2
Info : (7,)
SELECT count(*) FROM emp1
Info : (7,)
Traceback (most recent call last):
File "SQLserver_loop.py", line 20, in <module>
fields = c.fetchall()
File "pymssql.pyx", line 542, in pymssql.Cursor.fetchall (pymssql.c:9352)
pymssql.OperationalError: Statement not executed or executed statement has no re
sultset
fields = c.fetchall() was causing the error I commented it and works fine now.
for command in sqlCommands:
#print command
c.execute(command)
#fields = c.fetchall()
for row in c:
print (row)
Related
I always used psycopg2 to connect my Postgres database to my script, but, this is the first time that a get this error.
import psycopg2
import pandas as pd
conn = psycopg2.connect(
host='dabname',
database='db',
user='myuser',
password='mypassword')
cursor = conn.cursor()
cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
print(cursor.fetchall())
and this returns:
[('tb_orcamento_mes',), ('tb_notificacao',), ('tb_grupo_premissa',), ('tb_premissa',), ('tb_etapas',), ('tb_orcamento_anual',)]
But, when I try to fetch all records from 'tb_premissa', I get an error:
cursor = conn.cursor()
cursor.execute("SELECT * FROM tb_premissa")
quantitativos_realizado = cursor.fetchall()
results in this error:
UndefinedTable: ERRO: relação "tb_premissa" LINE 1: SELECT *
FROM tb_premissa
Does anyone have an idea?
I'm trying to query multiple sql tables and store them as pandas dataframe.
cur = conn.cursor()
cur.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
tables_df = cur.fetchall()
##table_name_list = tables_df.table_name
select_template = ' SELECT * FROM {table_name}'
frames_dict = {}
for tname in tables_df :
query = select_template.format(table_name = tname)
frames_dict [tname] = pd.read_sql ( query , conn)
But I'm getting error like :
DatabaseError: Execution failed on sql ' SELECT * FROM ('customer',)': syntax
error at or near "'yesbank'"
`enter code here`LINE 1: SELECT * FROM ('customer',)
Customer is name of table in my databse that i get from line
tables_df = cur.fetchall()
Per your error, looks like you have a typo in the word format:
AttributeError: 'str' object has no attribute 'formate'
Try
query = select_template.format(table_name = tname)
I want to remove the quotes from each end of string for SQL insert into Postgres.
At the moment my code looks like this:
label='ave_number'
sql_cmd=u"""UPDATE rpt.so_form2_test_noquotes SET %s=%s WHERE channel_id=%s and report_id=%s and load_date=current_date;"""
sql_params=(label, e[u"label"],channel_id,report_id,)
calldb(conn, sql_cmd, sql_params)
def calldb( db, sql_cmd, sql_params): # invoke backend function that is a INSERT/UPDATE statement
try:
cur = db.cursor() # use standard cursor as return is likely void
print 'Executing sql cmd "{0}"'.format(cur.mogrify(sql_cmd, sql_params))
cur.execute(sql_cmd, sql_params)
return # execute returns None expected
except Exception as e:
print u'Error ', e
raise
My Postgres column name is ave_number with no quotes. When the %s get substitued with the string it gives me an error saying there is no column called 'ave_number'.
Thanks in advance
I think you need to use AsIs from psycopg2.extensions
from psycopg2.extensions import AsIs
import psycopg2
def calldb( db, sql_cmd, sql_params): # invoke backend function that is a INSERT/UPDATE statement
try:
cur = db.cursor() # use standard cursor as return is likely void
print 'Executing sql cmd "{0}"'.format(cur.mogrify(sql_cmd, sql_params))
cur.execute(sql_cmd, sql_params)
return # execute returns None expected
except Exception as e:
print u'Error ', e
raise
conn = psycopg2.connect(host='localhost', port=5432, user='xxxxxxx', password='xxxxxx')
label='ave_number'
e={u'label':'test_label'}
channel_id=1
report_id=1
sql_cmd=u"""UPDATE rpt.so_form2_test_noquotes SET %s=%s WHERE channel_id=%s and report_id=%s and load_date=current_date;"""
sql_params=(AsIs(label), e[u"label"],channel_id,report_id,)
calldb(conn, sql_cmd, sql_params)
which outputs this
Executing sql cmd "UPDATE rpt.so_form2_test_noquotes SET ave_number='test_label' WHERE channel_id=1 and report_id=1 and load_date=current_date;"
I am connecting to Teradata with the following code which I execute on the python console:
conn = pyodbc.connect('DRIVER={Teradata}; DBCNAME=TdwB; UID=####;PWD=###;')
query = file(full_path).read()
opportunities = pd.read_sql(query, conn)
conn.close()
In query I read a very simple sql query from a file and everything works fine.
Then, I try running a much more complex query, expected to return about 350000 rows (0.2 GB). I am sure the query works because it has been executed perfectly on the SQL Assistant, Teradata query tool.
The script fails with DatabaseError: Execution failed on sql: SELECT after something like 5 minutes (I expect the query to run for about 10-20 minutes).
I am not sure how to tackle this because the error message is rather cryptic.
Is it a timeout?
Data formatting issue?
Anonymized query
Originally over 300 lines but it's just a select. Here are the main operations on data:
SELECT
TRUNC (CAST (a.created_at AS DATE ), 'M') AS first_day_month
,d.country_name AS country
,d.contract_id AS contract_id
,MAX (TRIM(CAST(REGEXP_REPLACE(contracts.BillingStreet, '\||\n|\r|\t', '_',1,0,'m') AS CHARACTER(2048))) || ', ' || TRIM(contracts.BillingPostalCode) || ', ' || TRIM(contracts.BillingCity)) AS FullAdress
,MIN (CAST (bills.created_at AS DATE )) AS first_transaction
,SUM (gross_revenue )
FROM db_1.billings AS bills
LEFT JOIN db_2.contracts AS contracts ON bills.deal_id = contracts.deal_id
WHERE bills.economic_area = 'NA'
AND CAST (bills.created_at AS DATE ) >= TRUNC (ADD_MONTHS (CURRENT_DATE -1, - 26) , 'MM')
AND bills.country_id = 110
GROUP BY 1,2,3
INTERESTINGLY:
conn = pyodbc.connect('DRIVER={Teradata}; DBCNAME=####; UID=####;PWD=####;', timeout=0)
cursor = conn.cursor()
query = file(full_path).read()
cursor.execute(query)
row = cursor.fetchone()
if row:
print row
cursor.close()
conn.close()
results in
Traceback (most recent call last):
File "C:\Users\mferrini\AppData\Local\Continuum\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-19-8f156c658077>", line 6, in <module>
cursor.execute(query)
Error: ('HY000', '[HY000] [Teradata][ODBC Teradata Driver][Teradata Database] Wrong Format (-9134) (SQLExecDirectW)')
Following this SO question, I'm trying to "truncate" all tables related to a certain django application using the following raw sql commands in python:
cursor.execute("set foreign_key_checks = 0")
cursor.execute("select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'")
for sql in [sql[0] for sql in cursor.fetchall()]:
cursor.execute(sql)
cursor.execute("set foreign_key_checks = 1")
Alas I receive the following error:
C:\dev\my_project>my_script.py
Traceback (most recent call last):
File "C:\dev\my_project\my_script.py", line 295, in <module>
cursor.execute(r"select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'")
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 18, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 216, in last_executed_query
return smart_unicode(sql) % u_params
TypeError: not enough arguments for format string
Is the % in the LIKE making trouble? How can I workaround it?
Have you tried %%? That quotes a % in Python string-formatting.