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}
Related
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 trouble with my program in my raspberry pi. This is my source code
import MySQLdb
import csv
db=MySQLdb.connect(user='root',passwd='toor',
host='127.0.0.1',db='data')
cursor=db.cursor()
csv_data=csv.reader(file('datasensor.txt'))
for row in csv_data:
sql = "insert into `kelembapan` (`id`,`Tanggal`,`Tipe_sensor`,`Value`,`Ket`) values(%s,%s,%s,%s,%s);"
cursor.execute(sql,row)
db.commit()
cursor.close()
print "The Data has been inputted"
and this is for txt file
1, 2017-10-10, sensor1,40,Kurang lembap
2, 2017-10-10, sensor2,60,Lembap
That program can run in my ubuntu but not in my raspberry. when run in raspberry there is error
Traceback (most recent call last):
File "server.py", line 9, in <module>
cursor.execute(sql,row)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py",line 159, in execute
query = query% db.literal(args)
TypeError: not enough arguments for format strings
Thnks before :)
Here is the error:
gutschy#kiste:~/pizza/pizza_daten$ python datenimport3.py
Traceback (most recent call last):
File "datenimport3.py", line 20, in <module>
")
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 ''Adressliste_forum1_v4.csv' INTO TABLE pizzeria_table FIE' at line 1")
gutschy#kiste:~/pizza/pizza_daten$
Here the datenimport3.py
#!/usr/bin/python
#-*- coding: utf-8 -*-
import MySQLdb as mdb
con = mdb.connect('localhost', 'user', 'passw', 'pizzadb2');
with con:
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute(" 'Adressliste_forum1_v4.csv'\
INTO TABLE pizzeria_table \
FIELDS TERMINATED BY ',' \
ENCLOSED BY '\"' \
LINES TERMINATED BY '\\n' \
IGNORE 1 LINES \
(laden_name, vorwahl, telenr1, strasse, hausnr, \
ort, linkname1, linkname2, linkname3, forum_link, \
link2, link3, banner) \
")
Four month ago it works all fine, than I killed my debian 7 and now I'm on to bring it in the same way like before. I've add the last field "banner" new but I've done noting more.
You can sort through all that traceback chaff by looking for this:
...for the right syntax to use near ''Adressliste_forum1_v4.csv' INTO ...
On a 1064 error, MySQL presents, right after "use near", your statement starting with the place where its parse error was found.
Now, should not your statement start with
LOAD DATA INFILE 'Adressliste_forum1_v4.csv' INTO ...
?
I have a working python script which retrieves data from a sqlite3 db. I need to convert it to talk to a mysql db though, and here's the first line that is giving me grief:
PRODUCT_CODE_ID = "SELECT id FROM localsite_productcode WHERE localsite_productcode.product_code=?"
cur.execute(PRODUCT_CODE_ID,(product_code,))
When I try that with mysql I get the following error:
Traceback (most recent call last):
File "gen-csv.py", line 85, in <module>
cur.execute(PRODUCT_CODE_ID,(product_code,))
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
What am I doing wrong here?
I don't think the DB API for MySQL supports the ? as a place holder - just try %s instead.