Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import pandas as pd
- import numpy as np
- def send_query(settings, query):
- '''
- \r[GeoPap]
- \rIt sends the query to the database
- '''
- if settings==None: raise ValueError("No settings provided")
- if query==None: raise ValueError("No query provided")
- # Delete invalid characters
- invalid_char="\n\t"
- for char in invalid_char:
- query=query.replace(char," ")
- # Repace 2 or more consecutive spaces with only one space
- query=" ".join(query.split())
- print("[send_query][MSG] Sending query:\n\t%s"%(query))
- with psycopg2.connect(**settings) as conn:
- with conn.cursor() as cur:
- try:
- cur.execute(query)
- data=cur.fetchall()
- answer=pd.DataFrame(data)
- print("[send_query][MSG] Got an answer!")
- except Exception as error:
- print("[send_query][ERR] %s"%(error))
- answer=""
- return answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement