I'm new to Python. I'm executing this very basic query:
connection = psycopg2.connect("dbname='test' host='localhost' user='admin' password='pass' port='9100'")
cur = connection.cursor()
cur.execute("""SELECT id FROM pages WEHERE uri = %(uri)s""", {'uri': uri})
row = cur.fetchall()
and keep getting this error:
<class 'psycopg2.ProgrammingError'>
('syntax error at or near "uri"\nLINE 1: SELECT id FROM pages WEHERE uri = \'http://example.com/index.php...\n ^\n',)
uri is a string and has the value http://example.com/index.php
Could you please help me?? This is making me crazy
It should be:
cur.execute("""SELECT id FROM pages WHERE uri = %(uri)s""", {'uri': uri})
That is, it should be where instead of wehere. Since there is no function like wehere in SQL, the syntax error is thrown.
The error itself is self-explanatory. Next time, read the error message closely.
Related
I am running into a weird error trying to append dataframe to MySQL table using pandas to_sql function. I have not been able to find answer to this anywhere. Here is a test example:
test_df = pd.DataFrame(['a','b','c','d'], columns = ['char'])
with engine.begin() as connection:
test_df.to_sql(name='test', con=connection, if_exists='append')
The above runs successfuly, I can see the table being created in my database.
new_df = pd.DataFrame(['e','f'], columns = ['char'])
with engine.begin() as connection:
new_df.to_sql(name='test', con=connection, if_exists='append')
However, when I try to append more data, I get following error:
OperationalError: (MySQLdb._exceptions.OperationalError) (1050, "Table 'test' already exists")
[SQL:
CREATE TABLE test (
`index` BIGINT,
`char` TEXT
)
]
(Background on this error at: https://sqlalche.me/e/14/e3q8)
This is very confusing. Since I did not see anyone encounter this error, could it be a bad installation of my packages. Any help will be greatly appreciated.
Thanks to the comment by Rouhollah. I made the "append' to work by replacing
engine = create_engine(f"mysql://{user}:{password}#{host}:{port}")
with
engine = create_engine(f"mysql://{user}:{password}#{host}:{port}/{database}")
previously, I was accessing database using engine.execute(f"USE {database}") which seems to break the append function.
Hello and thank you for taking the time to read this. For days I'm figuring out why I get this error when I try to load Account data towards an mssql database. The connection is fine.
But I keep on getting these errors:
(pyodbc.ProgrammingError) ('Invalid parameter type. param-index=17 param-type=collections.OrderedDict', 'HY105')
Exception: (102, b"Incorrect syntax near 'Id'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
Exception: One or more values in the dataframe have more characters than possible in the database table. The maximum number of characters in each column are:
How can I circumvent these errors and load the data without errors:
I use this for instance:
engine = sal.create_engine('mssql+pyodbc:///?odbc_connect={}'.format(params))
conn = engine.connect()
for entity in ['Account']:
df = get_salesforce_data(sf=sf, sf_object=entity, method=method)
df.to_sql(entity, con = engine, if_exists ='append', index = False, chunksize = 1000)
There 94 columns in this Account table?
Thank you for thinking with me
I want to save an API response, on some table of my database, I'm using Postgres along with psycopg2.
This is my code:
import json
import requests
import psycopg2
def my_func():
response = requests.get("https://path/to/api/")
data = response.json()
while data['next'] is not None:
response = requests.get(data['next'])
data = response.json()
for item in data['results']:
try:
connection = psycopg2.connect(user="user",
password="user",
host="127.0.0.1",
port="5432",
database="mydb")
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO table_items (NAME VALUES (%s)"""
record_to_insert = print(item['name'])
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
print (count, "success")
except (Exception, psycopg2.Error) as error :
if(connection):
print("error", error)
finally:
if(connection):
cursor.close()
connection.close()
my_func()
I mean, I just wanted to sort of "print" all the resulting data from my request into the db, is there a way to accomplish this?
I'm a bit confused as You can see, I mean, what could be some "print" equivalent to achieve this?
I mean, I just want to save from the API response, the name field, into the database table. Or actually INSERT that, I guess psycopg2 has some sort of function for this circumstance?
Any example You could provide?
EDIT
Sorry, I forgot, if I run this code it will throw this:
PostgreSQL connection is closed
A particular name
Failed to insert record into table_items table syntax error at or near "VALUES"
LINE 1: INSERT INTO table_items (NAME VALUES (%s)
There are a few issues here. I'm not sure what the API is or what it is returning, but I will make some assumptions and suggestions based on those.
There is a syntax error in your query, it is missing a ) it should be:
postgres_insert_query = 'INSERT INTO table_items (NAME) VALUES (%s)'
(I'm also assuming thatNAME` is a real column in your database).
Even with this correction, you will have a problem since:
record_to_insert = print(item['name']) will set record_to_insert to None. The return value of the print function is always None. The line should instead be:
record_to_insert = item['name']
(assuming the key name in the dict item is actually the field you're looking for)
I believe calls to execute must pass replacements as a tuple so the line: cursor.execute(postgres_insert_query, record_to_insert) should be:
cursor.execute(postgres_insert_query, (record_to_insert,))
I'm trying to search for user_name in mongoDB and if it's found it will print the user_id from the db. And if is not found I want to print "Id not found" but there's a problem: If the user_no_aront is not found then it doesn't print anything. Not even a "null" message. So any way to make the code send a "null"/"None" message if the data I'm asking for is not found?
connection = pymongo.MongoClient("mongodb://xxx:xxx#xxxx.mlab.com:xxx/xxx")
ddb = connection['aurora']
coll = db['users']
user = ''.join(args)
user_no_aront = re.sub('[^A-Za-z0-9]+', '', user)
curs = coll.find({"user_name":user_no_aront}, {"user_id":True, "_id":False})
for item in curs:
get_user = item.get("user_id", None)
print("get_user")
if user_no_aront doesn't exist in coll, the for-loop won't get execute at all. that's the reason why you won't print any thing, even None.
I'm updating the output of Google reverse geocoding (which is in JSON format),
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=mydb;UID=test;PWD=abc#123;autocommit=True')
cursor = cnxn.cursor()
wp = urllib.request.urlopen("http://maps.googleapis.com/maps/api/geocode/json?latlng=18.5504,73.9412&sensor=false")
pw = wp.read()
#print(pw)
cursor.execute("UPDATE GEOCODE_tbl SET JSON_str = ? WHERE GEOCODE_ID = ?", pw,749904)
print('Done')
cnxn.commit()
But it gives error
('22018', '[22018] [Microsoft][ODBC SQL Server Driver][SQL Server]Operand type clash: image is incompatible with nvarchar(max) (206) (SQLExecDirectW)')
What kind of error is that?
The JSON_str column has such JSON output, so I'm executing the task for those column whose JSON_str column is NULL.
Does anyone have any idea about it?
The value pw is not of type str. Try converting your query to this:
cursor.execute("UPDATE GEOCODE_tbl SET JSON_str = ? WHERE GEOCODE_ID = ?", (str(pw), 749904))
Good luck!