psycopg2 programming error while creating table - psycopg2

In a Python2.7 script, the following gave me an error, I can't figure out why:
import psycopg2
conn = psycopg2.connect("dbname=mydb user=username password=password")
curs = conn.cursor()
curs.execute("CREATE TABLE newtable;")
The error looks like:
Traceback (most recent call last):
File "<ipython-input-17-f4ba0186c40c>", line 1, in <module>
curs.execute("CREATE TABLE newtable;")
ProgrammingError: syntax error at or near ";"
LINE 1: CREATE TABLE newtable;
Any SELECT statement works perfectly well on the other hand. For example:
curs.execute("SELECT * FROM table1 LIMIT 0;")
works like a charm.

CREATE TABLE newtable; is not the correct syntax to create a new table. You need to define some columns.
CREATE TABLE newtable (
foo INTEGER,
bar TEXT
);
See the CREATE TABLE docs for more info.

Related

Incorrect number of bindings supplied. The current statement uses 11, and there are 10 supplied

I´m Learning Coding and the Error Message from the Debugging confuse me a bit,
when i try to create a SQLlite Database with 11 statements, i have two different Errors.
here my Code:
import sqlite3
connection = sqlite3.connect("wtime1.db")
cursor = connection.cursor()
date = ("31.10.2022")
p_unit = ("MMO")
equip = ("D014-K-00001")
report_prio = ("1")
order_prio = ("2")
job = ("Oel wechsel")
w_time = ("120")
fte = ("2")
reason = ("Kein Oel vorhanden")
cause = ("MAR 2")
solved = ("Erledigt")
mar2_list = ([date, p_unit, equip, report_prio, order_prio, job, w_time, fte, reason, cause, solved])
cursor.execute("create table if not exists mar21 (DATE integer, P_UNIT text, EQUIP text, REPORT_PRIO text, ORDER_PRIO text, JOB text, W_TIME integer, FTE integer, REASON text, CAUSE text, SOLVED text)")
cursor.executemany("insert into mar21 values (?,?,?,?,?,?,?,?,?,?,?)", mar2_list)
the first Error:
Incorrect number of bindings supplied. The current statement uses 11, and there are 10 supplied.
File "C:\Users\andre\PycharmProjects\Wartezeiten\db_test.py", line 26, in <module>
cursor.executemany("insert into mar21 values (?,?,?,?,?,?,?,?,?,?,?)", mar2_list)
Okay i found in my search i need one more comma at the end of the Values
when i set the comma like this
cursor.executemany("insert into mar21 values (?,?,?,?,?,?,?,?,?,?,?,)", mar2_list)
i have a syntax error
near ")": syntax error
File "C:\Users\andre\PycharmProjects\Wartezeiten\db_test.py", line 26, in <module>
cursor.executemany("insert into mar21 values (?,?,?,?,?,?,?,?,?,?,?,)", mar2_list)
i think i´m running a wrong way with the "insert values" line!
since you have one record use execute instead of executemany
cursor.execute("insert into mar21 values (?,?,?,?,?,?,?,?,?,?,?)", mar2_list)

Python3 Sqllite Normal query works but not while passing it a tuple

I have a Db from where I have to display all the columns which match the substring of the column given by user.
The following code works:
c.execute("select *from Transactions where Description like '%sh%' ")
conn.commit()
print(c.fetchall())
conn.close()
But when I try to run this code it returns me an empty list:
def search(col,val):
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("Select *from Transactions where ? Like ? ",(col,'%'+val+'%'))
print(c.fetchall())
search('description',"sh")
Also the result will always be a blank list even if the col name is wrong. as opposed the usual error which says column not found.
Please Help

How to execute sql Script with multiple statements from Airflow OracleOperator

i am trying to call a sql file with multiple statements separated by ; through the OracleOperator in airflow , but its giving below error with multiple statements
E.g File Containing
CALL DROP_OBJECTS('TABLE_XYZ');
CREATE TABLE TABLE_XYZ AS SELECT 1 Dummy from DUAL;
[2019-06-18 18:19:12,582] {init.py:1580} ERROR - ORA-00933: SQL command not properly ended
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/airflow/models/init.py", line 1441, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/lib/python3.6/site-packages/airflow/operators/oracle_operator.py", line 63, in execute
parameters=self.parameters)
File "/usr/local/lib/python3.6/site-packages/airflow/hooks/dbapi_hook.py", line 172, in run
cur.execute(s)
cx_Oracle.DatabaseError: ORA-00933: SQL command not properly ended
Even with single statement ending with ; giving below error :
e.g file
CREATE TABLE TABLE_XYZ AS SELECT 1 Dummy from DUAL;
[2019-06-18 17:47:53,137] {init.py:1580} ERROR - ORA-00922: missing or invalid option
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/airflow/models/init.py", line 1441, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/lib/python3.6/site-packages/airflow/operators/oracle_operator.py", line 63, in execute
parameters=self.parameters)
File "/usr/local/lib/python3.6/site-packages/airflow/hooks/dbapi_hook.py", line 172, in run
cur.execute(s)
with DAG('my_simple_dag',
default_args=default_args,
template_searchpath=['/root/rahul/'],
schedule_interval='*/10 * * * *',
) as dag:
opr_oracle = OracleOperator(task_id='oracleTest',oracle_conn_id='STG',
sql='test.sql')
do i need to pass any additional parameter to make the dbhook understand that the file need to be split in separate statement ?
as per the documentation it expects
param sql: the sql code to be executed. Can receive a str representing a sql statement,
a list of str (sql statements), or reference to a template file.
Template reference are recognized by str ending in '.sql'
(templated)
but the .sql template is not working with multiple statement. any help will be greatly appreciated . Thanks !!
The Oracle Operator will take a list of SQL Strings that are templated.
What I have done is read the SQL file in as a text file and then split it on the ';' to create a list of strings.
with open('/home/airflow/airflow/dags/sql/test_multi.sql') as sql_file:
sql_list = list(filter(None, sql_file.read().split(';')))
t_run_sql = OracleOperator(task_id='run_sql',
sql=sql_list,
oracle_conn_id='user_id',
autocommit=True,
depends_on_past=True,
dag=dag)
I tested this with templating (and yes this will fail in Oracle without creating the table first):
drop table test_multi;
create table test_multi as
select
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=1),'%Y%m%d') }} as day1,
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=2),'%Y%m%d') }} as day2,
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=3),'%Y%m%d') }} as day3
from dual;
insert into test_multi
select
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=4),'%Y%m%d') }} as day1,
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=5),'%Y%m%d') }} as day2,
{{ macros.datetime.strftime(execution_date.in_tz('Australia/Sydney') + macros.timedelta(days=6),'%Y%m%d') }} as day3
from dual;
This solution has an issue with the need to ensure that your SQL doesn't contain a semi-colon anywhere else. I also think splitting on ';/n' might be better, but it requires that the user always starts a new-line after the ';', so still not ideal.
I also found that I needed to deal with the last semi-colon with the filter(None,...) or the operator would submit an empty command to the database and then error.
Instead of sql file, you can assign string format sql statement"
Below is original API doc for Oracle operation in airflow. the sql could be str or list of str. If you prefer use file template, you need rendering the file template with parameter.
Note: Airflow use jinjia2 as template rending.
oracle_operator API
sql (str or list[str]) – the sql code to be executed. Can receive a str representing a sql statement, a list of str (sql statements), or
reference to a template file. Template reference are recognized by str
ending in ‘.sql’ (templated)
oracle_conn_id (str) – reference to a specific Oracle database
parameters (mapping or iterable) – (optional) the parameters to render
the SQL query with.
autocommit (bool) – if True, each command is automatically committed.
(default value: False)

Python sqlite3 and string formatting - Error

This is my code:
title = "Importunate Widow"
conn = sqlite3.connect('parable.sqlite')
c = conn.cursor()
sqlite3.enable_callback_tracebacks(True)
c.executescript("""
CREATE TABLE IF NOT EXISTS _index(
title text NOT NULL,
context text NOT NULL
);
CREATE TABLE IF NOT EXISTS {}(
value int NOT NULL,
number int NOT NULL,
question text NOT NULL,
choice1 text,
choice2 text,
choice3 text,
choice4 text,
answer text NOT NULL,
explanation text,
see text
)""".format(title))
c.executemany('INSERT INTO _index(title,context) VALUES(?,?)', index)
c.executemany('INSERT INTO {}(value,number,question,choice1,choice2,choice3,choice4,answer,explanation,see) VALUES(?,?,?,?,?,?,?,?,?,?)'.format(title), quiz)
conn.commit()
conn.close()
This gives me:
OperationalError Traceback (most recent call last)
/home/user/Hobby/Quiz/quiz_sqlite.py in <module>()
sqlite3.enable_callback_tracebacks(True)
87
---> 88 c.executescript("""CREATE TABLE IF NOT EXISTS _index(title text NOT NULL,context text NOT NULL);CREATE TABLE IF NOT EXISTS {}(value int NOT NULL,number int NOT NULL,question text NOT NULL,choice1 text,choice2 text,choice3 text,choice4 text,answer text NOT NULL,explanation text,check text )""".format(title))
89
90 c.executemany('INSERT INTO _index(title,context) VALUES(?,?)', index)
OperationalError: near "Widow": syntax error
I am basically iterating over multiple files and inserting data into tables named after the files. I have searched the internet and have turned up with nothing.
What I tried:
Tried adding ; after the ) before """
Tried adding , after see text
Tried changing the name of the column see to refer
None of this helped!
What is the syntax error in the code?
You're trying to use Importunate Widow as a table name. You're not allowed to use spaces in a table or field name.

strange Oracle error: "invalid format text"

I'm trying to fetch some data from a column whose DATA_TYPE=NUMBER(1,0) with this piece of code:
import cx_Oracle
conn = cx_Oracle.connect(usr, pwd, url)
cursor = conn.cursor()
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
print(cursor.fetchone()[0])
which complains thus:
Traceback (most recent call last):
File "main.py", line 247, in <module>
check = completed()
File "main.py", line 57, in completed
deleted = cursor.fetchone()[0]
cx_Oracle.DatabaseError: OCI-22061: invalid format text [T
Replacing 'DELETED' column with one whose DATA_TYPE=VARCHAR2 does not throw such a complaint.
I am running in to this problem now using cx_Oracle 5.0.4 with Unicode support. The above accepted solution did not work for me. The DELETED column in the question is a Numeric column, which is what causes this bug.
According to the mailing list ( http://comments.gmane.org/gmane.comp.python.db.cx-oracle/2390 ) it may be a bug in Oracle that shows only in cx_Oracle with Unicode support.
from the link:
"When I build cx_Oracle without Unicode support, it all works as expected.
When I build cx_Oracle with Unicode support, attempting to use a query
that returns a numeric value (such as):
con = Connection( ... )
cur = con.cursor()
cur.execute( 'SELECT 1 FROM DUAL' )
rows = cur.fetchall()
results in this exception:
cx_Oracle.DatabaseError: OCI-22061: invalid format text [T
"
What I did to work around it, is on the select statement, do:
cur.execute( 'SELECT to_char(1) FROM DUAL' )
rows = cur.fetchall()
for row in rows:
val = int(row[0])
It's pretty ugly, but it works.
These types of errors went away when I upgraded to cx_Oracle 5.1. If the RPM doesn't install (like it happened for me on Red Hat 5.5) then you can usually rpm2cpio the file, take the cx_Oracle.so and put it into your python site-packages directory.
A work-around is putting time.sleep(1) before cursor.fetchone():
...
cursor.execute("SELECT DELETED FROM SERVICEORDER WHERE ORDERID='TEST'")
time.sleep(1)
print(cursor.fetchone()[0])
I had the same error.
Commit helped me:
conn = cx_Oracle.connect(...)
...
cursor.execute()
conn.commit()

Resources