I can I update multiple columns for a particular row with psycopg2? I have tried this but I get the error I show bellow:
cursor = postgres_conn.cursor()
sql_update = "UPDATE table_name SET a=%s, b=%s, c=%s WHERE url=%s"
cursor.execute(sql_update,(1,2,3,url))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
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.
Querying COSMOS by Hive with this python script:
import pyhs2
with pyhs2.connect(host='cosmos.lab.fiware.org',
port=10000,
authMechanism="PLAIN",
user='glaquidara',
password='*****',
database='default') as conn:
with conn.cursor() as cur:
cur.execute("select * from glaquidara_mazzi")
print cur.getSchema()
for i in cur.fetch():
print i
generates the following error:
Traceback (most recent call last):
File "libra.py", line 10, in
cur.execute("select * from glaquidara_mazzi")
File "/Library/Python/2.7/site-packages/pyhs2/cursor.py", line 64, in execute
raise Pyhs2Exception(res.status.errorCode, res.status.errorMessage)
pyhs2.error.Pyhs2Exception: 'Error while compiling statement: FAILED: RuntimeException org.apache.hadoop.ipc.RemoteException:
org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot
create directory
/tmp/hive-root/hive_2015-10-07_13-32-30_348_3807878262746902303-3.
Name node is in safe mode.\nResources are low on NN. Safe mode must be
turned off manually.\n\tat
org.apache.hadoop.hdfs.server.namenode.FSNamesystem.mkdirsInternal(FSNamesystem.java:2140)\n\tat
org.apache.hadoop.hdfs.server.namenode.FSNamesystem.mkdirs(FSNamesystem.java:2114)\n\tat
org.apache.hadoop.hdfs.server.namenode.NameNode.mkdirs(NameNode.java:892)\n\tat
sun.reflect.GeneratedMethodAccessor586.invoke(Unknown Source)\n\tat
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)\n\tat
java.lang.reflect.Method.invoke(Method.java:597)\n\tat
org.apache.hadoop.ipc.RPC$Server.call(RPC.java:557)\n\tat
org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1439)\n\tat
org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1435)\n\tat
java.security.AccessController.doPrivileged(Native Method)\n\tat
javax.security.auth.Subject.doAs(Subject.java:396)\n\tat
org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1278)\n\tat
org.apache.hadoop.ipc.Server$Handler.run(Server.java:1433)\n'
What is the problem with this?
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.