I'm trying to create procedure on Teradata using turbodbc.
There is my sample code:
from turbodbc import connect
con = connect(dsn="Teradata")
cur = con.cursor()
cur.execute("""
create procedure dev.test_procedure (
) sql security invoker
begin
delete dev.test_table;
end;
""")
And got this error:
DatabaseError: ODBC error
state: 42000
native error code: -3706
message: [Teradata][ODBC Teradata Driver][Teradata Database] Syntax error:
Invalid SQL Statement.
But the same code works with no errors in Teradata SQL Assistant. What's wrong?
Possible solution is use teradatasql. This sample works fine:
import teradatasql
with teradatasql.connect(
'{"host":"my_host","user":"my_user","password":"my_password"}'
) as con:
with con.cursor() as cur:
cur.execute("""
create procedure dev.test_procedure (
) sql security invoker
begin
delete dev.test_table ;
end ;
""")
But, it will be good if someone knows solution with turbodbc.
Related
I'm using 32-bit Python along with 32-bit Microsoft Access.
I connect to the database and create a cursor.
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=P:\path\database.accdb;')
c = conn.cursor()
I then read in a table for future use:
c.execute('select * from tbl')
tbl = c.fetchall()
But when I try to make a copy of the table using:
query = 'CREATE TABLE tbl2 LIKE tbl'
c.execute(query)
I get a programming error:
ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement. (-3551) (SQLExecDirectW)')
I'm new to PYODBC but this is a pretty basic SQL line. Any tips on what might be wrong?
Due to a lack of documentation, the best way I found to figure this out is to use Microsoft Access to create a template for the query...example below worked.
query = 'SELECT tbl.* INTO tbl2 FROM tbl'
c.execute(query)
I want to write a program in Python to control MySQL database, but the following error occurs
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; chec
k the manual that corresponds to your MySQL server version for the right syntax
to use near 'Code,type, number,Whether )VALUES ('cl8k-k520-fg45-d4sa-sd9k','n' at line 1")
Codeļ¼
import pymysql
db = pymysql.connect("localhost", "root", "root", "test")
cursor = db.cursor()
sql = "INSERT INTO BOOKS(Activation Code, \
type, number,Whether ) \
VALUES (%s,%s,%s,%s)"
data = [("cl8k-k520-fg45-d4sa-sd9k",'n','10','n'),
("jhg6-58jh-gh8o-8uik-7jk8",'c','20','n'),
("x5hg-gf5h-4hj5-g4h5-fh5t",'y','999','n'),
("fg8j-h584-fd4g-fg4d-fgg4",'n','1','y'),
("4jk4-5kl4-4klk-4lji-4ghj",'e','6','n'),
]
cursor.executemany(sql,data)
db.close()
This is my first time to ask questions on this platform. I hope to get answers
enclose Activation Code as 'Activation Code'
I'm trying to set up a small database for a simple card game using mysql.connector 8.0 in jupyter notebook, yet I can't execute any queries, it says that the syntax is wrong even though it's all good.
That's the code:
'''
import random
import collections
import mysql.connector
conn = mysql.connector.connect(
host='localhost',
user='root',
passwd='*****',
auth_plugin='mysql_native_password',
database='CardGame'
)
cur = conn.cursor()
query = "CREATE TABLE cards (id int PRIMARY KEY AUTO_INCREMENT,rank VARCHAR(1),suit VARCHAR(10))"
cur.execute(query)
conn.commit()
'''
That's the error I keep getting:
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 'rank VARCHAR(1), suit VARCHAR(10))' at line 1
I don't have an access to a MySQL database at the moment but your create table has RANK as a column name which is also a keyword. Can you change that to something else and try it out?
I'm using petl package to build an ETL pipeline from Python 2.7 to MySQL5.6
My db connector is MySQLdb (mysql-python).
The following code fails to execute:
import MySQLdb as mdb
import petl as etl
con = mdb.connect(host = '127.0.0.1', user = '<someuser>', passwd = '<somepass>')
cur = con.cursor() # get the cursor
cur.execute('DROP SCHEMA IF EXISTS petltest')
cur.execute('CREATE SCHEMA petltest')
cur.execute('USE petltest')
dat = [{'id':1,'name':'One'},
{'id':2,'name':'Two'},
{'id':3,'name':'Three'}]
table = etl.fromdicts(dat) # petl object
etl.todb(table,cur,'table',schema='petltest',create=True)
The error code is:
ProgrammingError: (1064, '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 \'"table" (\n\tid INTEGER NOT NULL, \n\tname
VARCHAR(5) NOT NULL\n)\' at line 1')
This error occurs also when trying to create table separately or running petl.appenddb
How can I fix it / overcome the issue?
Thanks
Apparently the problem was the quotes style that PETL use.
If you run:
cur.execute('SET SQL_MODE=ANSI_QUOTES')
before petl sql (petl.todb()) statements it executes well.
I am using pyodbc to connect to a database and extract certain data from it.
Here is my code:
con = pyodbc.connect("driver={SQL Server};server= MyServer;database= MyDatabase;trusted_connection=true")
cursor = con.cursor()
SQL_command = """
SELECT RowID = ISNULL
(
(
SELECT TOP 1 RowID
FROM [MyDatabase].[admin].[MyTable]
WHERE [queue] = ? and processed IS NULL
)
,-1
)
"""
cursor.execute(SQL_command, queueNumber)
cursor.commit()
con.commit()
result_set = cursor.fetchall()
And I got following error after I run above code:
pyodbc.Error: ('HY010', '[HY010] [Microsoft][ODBC SQL Server
Driver]Function sequence error (0) (SQLFetch)')
May I know what caused such problem, and how can I fix it?
Thanks.
I believe your problem is the strange commit statements. You only need to commit when inserting or updating records not selecting.
cursor.execute(SQL_command, queueNumber)
result_set = cursor.fetchall()
Also, in the future when using commit, both cursor.commit and con.commit do the same thing, you only need one.
Finally, I'd get used to calling execute with the second arguement as a tuple:
cursor.execute(SQL_command, (queueNumber,))
The way you have it works for pyodbc but is not DB API standard.
I was recieving the two following errors more or less interchangeably:
pyodbc.Error: ('HY010', '[HY010] [Microsoft][ODBC Driver 17 for SQL Server]Function sequence error (0) (SQLGetData)')
pyodbc.Error: ('HY007', '[HY007] [Microsoft][ODBC Driver 17 for SQL Server]Associated statement is not prepared (0) (SQLNumResultCols)')
My problem was, that two threads were using the same connection, which led to weird states of the prepared statements. I fixed it by creating a new connection for each thread.