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 execute a terminal command and send the output to a table in postgress Database:
res = subprocess.Popen('bgpq3 -4 AS-YAHOO-JP-2 -m 24 -l Google', shell=True,
universal_newlines=True,
stdout=subprocess.PIPE).communicate()[0]
for line in res.split('\n')[1:]:
po=line.split()
c.execute('CREATE TABLE IF NOT EXISTS "AS" ("prefix" text)')
c.execute('INSERT INTO AS ("prefix") VALUES (%s)',(po))
I am getting this error:
Traceback (most recent call last):
File "oefenen.py", line 30, in <module>
c.execute('INSERT INTO AS ("prefix") VALUES (%s)',(po))
TypeError: not all arguments converted during string formatting
can anyone help me with this?
c.execute('INSERT INTO AS ("prefix") VALUES (%s)'[po])
with this command the ouput in DB wil be in my table but i dont want ({},) in my table :
{ip,prefix-list,Google,permit,14.137.224.0/19}
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.