hi~ I want to access pgsql through psycopg2, but an error is reported.I use the same command and have no problem in the pgsql.
import psycopg2
connect = psycopg2.connect(database='xxx',
user='xxx',
password='xxx',
host='xxxx',
port=1521)
cursor=connect.cursor()
cursor.execute('SELECT version()')
rows = cursor.fetchall()
for row in rows:
print(row)
Traceback (most recent call last):
File "C:/Users/z30007746/PycharmProjects/untitled3/pt_test.py", line 14, in <module>
cursor.execute('SELECT version()')
psycopg2.errors.SyntaxError: syntax error at end of input
QUERY: DECLARE
BEGIN
CONTEXT: FUNCTION: plspl_anon_func
LINE 1 FROM "CREATE": BEGIN
I am trying to connect to oracle database and execute a query using cx_Oracle in python with following code
import cx_Oracle
mobileNumber='1234567890'
dbHost='some_value'
dbPortNumber='some_value'
dbUsername='some_value'
dbPasswd='some_value'
dsn_tns = cx_Oracle.makedsn(dbHost, dbPortNumber, service_name='ORCL')
conn = cx_Oracle.connect(user=dbUsername, password=dbPasswd, dsn=dsn_tns)
cur=conn.cursor()
query='insert into table_name values ((select max(ID)+1 from table_name),"sms","'+ mobileNumber + '")'
cur.execute(query)
res=cur.fetchall()
print (res)
But I am getting following exception when running this code
Traceback (most recent call last):
File "script.py", line 16, in <module>
result=cur.fetchall()
cx_Oracle.InterfaceError: not a query
When printing the value of variable query I am getting following
insert into table_name values ((select max(ID)+1 from table_name),"sms","1234567890")
I have a problem inserting data into my database table. I'm using Python and MariaDB. Connection to database is open and tested, I'm able to query the database, but I can't nail the insert syntax. I've found two ways, but neither work.
insert = (
"INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, ilosc stron)"
"VALUES (%s,%s,%s,%s,%s)"
)
dane = (ISBN, tytul, autor, rok_wydania, ilosc_stron)
cursor.execute(insert, dane)
or this way:
cursor.execute("INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, ilosc stron) VALUES (%s,%s,%s,%s,%s)", (ISBN, tytul, autor, rok_wydania, ilosc_stron))
When executing I get this error:
Traceback (most recent call last): File
"C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection_cext.py",
line 377, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'stron)VALUES
('12345678','wertvfdg','3','1243','213')' at line 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/jakub/PycharmProjects/biblioteka/sql_connector.py", line 57,
in
cursor.execute(insert, dane) File "C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor_cext.py",
line 264, in execute
raw_as_string=self._raw_as_string) File "C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection_cext.py",
line 380, in cmd_query
sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'stron)VALUES ('12345678','wertvfdg','3','1243','213')' at
line 1
If your column name has a space in it, then it needs special handling and you must escape it:
INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, `ilosc stron`) ...
This is why spaces in column names are annoying and should be avoided.
So I've got a .sql file that I want to build over to a .db file for sqllite3, but I'm getting a
sqlite3.OperationalError: near "ENGINE": syntax error
Any ideas why? Any help is appreciated.
import sqlite3
conn = sqlite3.connect('newdatabase.db')
f = open('olddatabase.sql','r')
sql = f.read()
conn.executescript(sql)
conn.close()
OUTPUT:
Traceback (most recent call last):
File "deleteme.py", line 6, in
conn.executescript(sql)
sqlite3.OperationalError: near "ENGINE": syntax error
(program exited with code: 1)
Divining based on ENGINE, you have a .sql file dumped by MySQL (CREATE TABLE ... ENGINE=InnoDB or similar), so it contains MySQL-specific extensions.
You'll have to either edit the .sql file to conform to SQLite's standards or figure out a way to convert it automatically to a more standard sequence of SQL instructions.
Here is some python code which is being executed against a HIVE database
pyodbc.autocommit = True
con = pyodbc.connect("DSN=MyCon", autocommit=True)
cursor = con.cursor()
cursor.execute("select name, surname from foo f inner join bar b on f.id = b.id")
Error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', "[HY000] [Hortonworks][HiveODBC] (35) Error from
Hive: error code: '0' error message: 'ExecuteStatement finished with operation
state: ERROR_STATE'. (35) (SQLExecDirectW)")
I solved it. when creating the ODBC connection use the user hdfs. I had read a tutorial and was using the user hue.
this caused the problem.