SQL compilation error-- Snowsql validation using python - python

I am following this tutorial :https://docs.snowflake.com/en/sql-reference/functions/validate.html
to try and 'return errors by query ID and saves the results to a table for future reference'
however for a seamless transfer I don't want to be putting the job id always as it would require me to go to snowflake console- go to history- get the jobid -copy and paste it to python code.
Instead I wanted to go with just the tablename which is a variable and 'last_query_id()' and give me the list errors. Is there any way i can achieve this?
import snowflake.connector
tableName='F58155'
ctx = snowflake.connector.connect(
user='*',
password='*',
account='*')
cs = ctx.cursor()
ctx.cursor().execute("USE DATABASE STORE_PROFILE_LANDING")
ctx.cursor().execute("USE SCHEMA PUBLIC")
try:
ctx.cursor().execute("PUT file:///temp/data/{tableName}/* #%
{tableName}".format(tableName=tableName))
except Exception:
pass
ctx.cursor().execute("truncate table {tableName}".format(tableName=tableName))
ctx.cursor().execute("COPY INTO {tableName} ON_ERROR = 'CONTINUE' ".format(tableName=tableName,
FIELD_OPTIONALLY_ENCLOSED_BY = '""', sometimes=',', ERROR_ON_COLUMN_COUNT_MISMATCH = 'TRUE'))
I have tried the below validate function....it is giving me error on this line
the error is "SQL compilation error:
syntax error line 1 at position 74 unexpected 'tableName'.
syntax error line 1 at position 83 unexpected '}'."
ctx.cursor().execute("create or replace table save_copy_errors as select * from
table(validate({tableName},'select last_query_id()'))");
ctx.close()

The line
ctx.cursor().execute("create or replace table save_copy_errors as select * from
table(validate({tableName},'select last_query_id()'))");
should be replaced with these two
job_id = ctx.cursor().execute("select last_query_id()").fetchone()[0]
ctx.cursor().execute(f"create or replace table save_copy_errors as select * from
table(validate({tableName},job_id=>'{job_id}'))");

Related

Error in extracting DDL for functions in DB2

I used the below statement to extract the ddl for particular function using python in DB2.
Function name = 'DEPTEMPLOYEES'
DDL = "select text from syscat.routines where routineschema = {}
and routinename = {} and routinetype = 'F'".format(user_schema,objs.upper())
cursor.execute(DDL)
But when I tried to execute this statement am getting an error.
ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI
Driver][DB2/NT64] SQL0206N "DEPTEMPLOYEES" is not valid in the
context where it is used. SQLSTATE=42703\r SQLCODE=-206
Can someone please help me to solve this error
Have you tried something like this, adding single quotes around the strings?
DDL = "select text from syscat.routines where routineschema = '{}'
and routinename = '{}' and routinetype = 'F'".format(user_schema,objs.upper())
cursor.execute(DDL)
Based on the error it seems that your parameters was printed and the quotes were missing. This turned the parameter into a keyword, hence the error message.

Errors adding a new column to a table in MySQL with Python

I'm new to MySQL and database in general, however I'm having some issue when I try to add a new column of integer inside my table. To add a new column I'm doing so:
import mysql.connector
mydb = mysql.connector.connect(
# host, user, password and database
)
mycursor = mydb.cursor(buffered = True)
# some stuff to get the variable domain
mycursor.execute('ALTER TABLE domainsMoreUsed ADD {} INTEGER(10)'.format(domain)) # domain is a string
but i get this error:
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
mysql.connector.errors.ProgrammingError: 1064 (42000): You
have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right
syntax to use near 'in INTEGER(10)' at line 1
I get the same error above also trying:
mycursor.execute('ALTER TABLE domainsMoreUsed ADD %s INTEGER(10)' % domain)
Instead when I use:
mycursor.execute('ALTER TABLE domainsMoreUsed ADD %s INTEGER(10)', (domain))
i get:
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
I read some post of other users about the same error, but I couldn't find what I need. I'm pretty sure about the SQL syntax being correct.
I'm using MySQL 8.0 with Python 3.8.3 on Windows 10.
Thank you in advance for your help.
What is the string domain set to? The error message syntax to use near 'in INTEGER(10)' at line 1, implies "in", which is a reserved word. If you want to use that for a table or column name, you need to add backticks: " ` " (left of '1' on the top row of your keyboard) around them.
Change your queries like this:
mycursor.execute('ALTER TABLE domainsMoreUsed ADD `{}` INTEGER(10)'.format(domain))
mycursor.execute('ALTER TABLE domainsMoreUsed ADD `%s` INTEGER(10)', (domain))

Python execute sql file error pyodbc 'HY090', '[HY090] [Microsoft][ODBC Driver Manager] Invalid string or buffer length (0) (SQLExecDirectW)'

import sql_connect
def main():
# check if db exists on target, if not create
qry_create_db = "if not exists(select * from sys.databases where name = '{}') create database {};".format('mydb','mydb')
with sql_connect.conn:
cur1 = sql_connect.cursor.execute(qry_create_db)
cur1.commit()
main()
def creation_table(filename):
# Open and read the file as a single buffer
fd = open(filename, 'r')
sqlFile = fd.read()
fd.close()
# all SQL commands (split on ';')
sqlCommands = sqlFile.split(';')
# Execute every command from the input file
for command in sqlCommands:
with sql_connect.conn:
cur2 = sql_connect.cursor.execute(command)
cur2.commit()
creation_table('Mypath\\schema\\TABLES\\TOSHBA.sql')
creation_table('Mypath\\schema\\TABLES\\TALM_TYPE.sql')
This my python code that creates my database and my tables. The problem is when I execute my function
"creation_table('Mypath\\schema\\TABLES\\TALM_TYPE.sql')"
I receive this error :
pyodbc.Error: ('HY090', '[HY090] [Microsoft][ODBC Driver Manager] Invalid string or buffer length (0) (SQLExecDirectW)')
This my first sql file script TOSHBA that I execute first with my function creation_table and I have no error with it :
USE mydb;
DROP TABLE IF EXISTS TOSHBA;
CREATE TABLE TOSHBA
(
TOSHBA_WORK_ID INT NOT NULL IDENTITY,
WORK_NAME NVARCHAR(200) NOT NULL,
CONSTRAINT PK_TOSHBA PRIMARY KEY (TOSHBA_WORK_ID)
);
And here the second sql script file with which I have the error:
USE mydb;
DROP TABLE IF EXISTS TALM_TYPE;
CREATE TABLE TALM_TYPE
(
TALM_ID INT NOT NULL IDENTITY,
TOSHBA_id INT NOT NULL,
TALM_NAME NVARCHAR(20) NOT NULL,
CONSTRAINT PK_TALM PRIMARY KEY (TALM_ID),
CONSTRAINT FKÖ¹_TOSHBA_id FOREIGN KEY (TOSHBA_id) REFERENCES TOSHBA (TOSHBA_WORK_ID)
);
Please help me to understand the error and to find the solution.
Python version : 3.7.3
Pyodbc version : 4.0.27
It appears removing the last ';' from the sql file solves this issue.
I'm not 100% on this but when I had this same issue and came here, I tried removing the last ';' from the sql script and it worked. So it seems that the .split() on the last ';' causes a blank line to be run through as a statement through the loop, whether there is another line there or not. As a blank line is not a valid sql statement.
In theory to work around this issue pragmatically you could either count the number of ; and only run the split that many times, or remove the last ; from the script before loading the statements. But both of these would be overload when you can just remove the last ';' in your sql file.

MySQL/Python Syntax Error

I keep getting a error with a SQL query that is written in python.
Here is the code in question:
else:
else_query = "SELECT count(*) FROM PARKING_SPOTS WHERE OCCUPANCY = %s"
cursor.execute(else_query, (occupancy,)
" AND WHERE LOCATION = %s", (location,))
Here's the error message:
File "exp1", line 116
" AND WHERE LOCATION = %s", (location,))
^
SyntaxError: invalid syntax
Can anyone spot the error ? I've changed things around several times, including containing part of the SQL query in a variable, yet I receive the same error.
your query is incorrect because you can't have 2 WHERE clauses
you can only pass one querystring
so make that:
else_query = """SELECT count(*) FROM PARKING_SPOTS WHERE OCCUPANCY = %s
AND LOCATION = %s
"""
cursor.execute(else_query, (occupancy, location))
parameters for the query need to be passed as a tuple

Returning a database with locations

I think I have the right idea to solve this function, but I'm not sure why I get this error when I test it. Can anyone please help me fix this?
Error: conn = sqlite3.connect(db)
sqlite3.OperationalError: unable to open database file
Desired Output:
>>> get_locations(db, 'ANTA01H3F')
[('ANTA01H3F', 'LEC01', 'AA112'), ('ANTA01H3F', 'LEC01', 'SY110'), ('ANTA01H3F', 'LEC02', 'AC223')]
def get_locations(db, course):
'''Return the course, section and locations of the exam for the given course.'''
return run_query('''SELECT Courses.Course, Courses.Sections, Room.Locations
FROM Courses JOIN Locations ON Courses.ID = Locations.ID WHERE Course = ?''', [course])
This is too much abstract. ;)
See run_query() from where it is getting the value of db (sqlite database file name) to run queries. It is not getting correct file name that you are expecting.
You are calling the function wrong, it accepts db and sql statement string:
return run_query(db, "SELECT Courses.Course, Courses.Sections, Locations.Room " \
" FROM Courses JOIN Locations ON Courses.ID = Locations.ID WHERE Course = '{}'".format(course))

Categories

Resources