Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
''' dbapi2 tests '''
print 'psycopg2'
conn = psycopg2.connect("dbname=test user=postgres host=127.0.0.1")
cur = conn.cursor()
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, cur)
cur.execute("CREATE TEMPORARY TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def"))
cur.execute("SELECT * FROM test;")
cur.fetchone()
conn.commit()
cur.close()
conn.close()
print 'pg8000'
conn = pg8000.DBAPI.connect(host="localhost", user="test", password="test")
cursor = conn.cursor()
cursor.execute("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
cursor.execute(
"INSERT INTO book (title) VALUES (%s), (%s) RETURNING id, title",
("Ender's Game", "Speaker for the Dead"))
for row in cursor:
id, title = row
conn.commit()
cursor.execute("SELECT now()")
cursor.fetchone()
cursor.execute("SELECT now() - %s", (datetime.date(1980, 4, 27),))
cursor.fetchone()
pg8000.DBAPI.paramstyle = "numeric"
cursor.execute("SELECT array_prepend(:1, :2)", (500, [1, 2, 3, 4],))
cursor.fetchone()
cursor.close()
cfg_password = Config.get(configSection, "password")
cfg_database = Config.get(configSection, "database")
cfg_server = Config.get(configSection, "server")
if cfg_server == "" \
or cfg_database == "" \
or cfg_username == "" \
or cfg_password == "":
print "Could not load config file"
sys.exit(0)
if kwargs['verbosity'] <= DEBUG:
print "[debug] main args", args
print "[debug] main kwargs", kwargs
conn = DBAPI.connect(host=cfg_server, database=cfg_database,
user=cfg_username, password=cfg_password)
cursor = conn.cursor()
if kwargs['query_max_id']:
print get_max_id(conn,cursor)
return
loadSearchTerms(kwargs)
start_id = get_max_id(conn,cursor)
print start_id
if start_id is not None:
kwargs['start_id'] = int(start_id)
rl = GetRateLimiter()
rl.api._cache_timeout = 30
def connectDB(**db):
try:
conn = DBAPI.connect(**db)
cursor = conn.cursor()
except:
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
sys.exit("Database connection failed! ->%s" % (exceptionValue))
return cursor, conn
def connect(**db):
try:
# get a connection, if a connect cannot be made an exception will be raised here
conn = DBAPI.connect(**db)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
except:
# Get the most recent exception
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
# Exit the script and print an error telling what happened.
sys.exit("Database connection failed! -> %s" % (exceptionValue))
return cursor,conn
def connect():
logger.debug('Connecting to the database')
global conn
global cursor
try:
# Create a connection to the database
conn = DBAPI.connect(**db)
# Create a cursor that will be used to execute queries
cursor = conn.cursor()
except:
# Get the most recent exception
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
# Exit the script/thread and print an error telling what happened.
logger.error("Database connection failed! -> %s" % (exceptionValue))
sys.exit()
logger.debug('Connected to the database')
def connect():
logger.debug('Connecting to the database')
global conn
global cursor
try:
# Create a connection to the database
conn = DBAPI.connect(**db)
# Create a cursor that will be used to execute queries
cursor = conn.cursor()
except:
# Get the most recent exception
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
# Exit the script/thread and print an error telling what happened.
logger.error("Database connection failed! -> %s" % (exceptionValue))
sys.exit()
logger.debug('Connected to the database')
def __create_database(self):
""" Creates the database using template1 """
try:
try:
logging.info("Check if Database Exists")
connection = pg8000.DBAPI.connect(
user=self.USERNAME, password=self.PASSWORD, host=self.DB_HOST, database='template1') # Default DB
cursor = connection.cursor()
connection.autocommit = True
cursor.execute('''CREATE DATABASE network_monitor''')
connection.close()
logging.info('Created Database: ' + self.DATABASE)
except errors.ProgrammingError:
logging.warn('Database (' + self.DATABASE + ') Already Exists')
except pg8000.errors.InterfaceError:
logging.error("DB Connection Interface Error")
print('Please check the user settings')
import ConfigParser
import pg8000
from pg8000 import DBAPI
import re
from googlemaps import GoogleMaps
addressRegex = r"(\b(at|on)\s((\d+|\w{2,})\s){1,2}(st(reet)?|r[(oa)]d)(\sstation|\smarket)?[,.\s])"
#addressRegex =r"(\b(at|on)\s((\d+|\w{2,})\s){1,2}(st(reet)?|r[(oa)]d)(\sstation|\smarket)?)"
currRegex = addressRegex
gmaps = GoogleMaps("ABQIAAAAUGnYtZ9Py2CWqhKA2j8WNhSV67USoQ6pUbqiV9eqnAi_hHG1PhShAENkss9dydHdndy0C9ko99g-Pg")
#gmaps = GoogleMaps("AIzaSyCw6F9tfQG6R56y9tUUm4WaI7o-D3cn7HI")
conn = DBAPI.connect(host=cfg_server, database=cfg_database,user=cfg_username, password=cfg_password)
cursor = conn.cursor()
query = "select text from tweets where geolocation is null"
cursor.execute(query)
counter = 0
for row in cursor:
text = str(row[0])
regexMatch = re.search(currRegex, text, re.IGNORECASE)
if not regexMatch == None:
#print text[:140]
addr = regexMatch.group(0)[3:] #, text[:80]
lraddr = addr.lower()
if ("the street" in lraddr) or ("my street" in lraddr) or ("this street" in lraddr) or ("our street" in
lraddr) or ("a street" in lraddr) or ("high street" in lraddr) or ("upper st" in lraddr) :
continue;
def DateFromTicks(ticks):
return DBAPI.Date(*time.localtime(ticks)[:3])
DateFromTicks = staticmethod(DateFromTicks)
from pg8000 import DBAPI
connect = DBAPI.connect
Warning = DBAPI.Warning
Error = DBAPI.Error
InterfaceError = DBAPI.InterfaceError
InternalError = DBAPI.InternalError
DatabaseError = DBAPI.DatabaseError
DataError = DBAPI.DataError
OperationalError = DBAPI.OperationalError
IntegrityError = DBAPI.IntegrityError
ProgrammingError = DBAPI.ProgrammingError
NotSupportedError = DBAPI.NotSupportedError
Date = DBAPI.Date
Time = DBAPI.Time
Timestamp = DBAPI.Timestamp
DateFromTicks = DBAPI.DateFromTicks
TimeFromTicks = DBAPI.TimeFromTicks
TimestampFromTicks = DBAPI.TimestampFromTicks
Binary = DBAPI.Binary