本文整理汇总了Python中pg8000.connect方法的典型用法代码示例。如果您正苦于以下问题:Python pg8000.connect方法的具体用法?Python pg8000.connect怎么用?Python pg8000.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pg8000
的用法示例。
在下文中一共展示了pg8000.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_copy_data_from
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def test_copy_data_from(self):
try:
tmpdir = tempfile.mkdtemp()
# create new database
with testing.postgresql.Postgresql(base_dir=tmpdir) as pgsql:
conn = pg8000.connect(**pgsql.dsn())
with closing(conn.cursor()) as cursor:
cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
conn.commit()
conn.close()
# create another database from first one
data_dir = os.path.join(tmpdir, 'data')
with testing.postgresql.Postgresql(copy_data_from=data_dir) as pgsql:
conn = pg8000.connect(**pgsql.dsn())
with closing(conn.cursor()) as cursor:
cursor.execute('SELECT * FROM hello ORDER BY id')
self.assertEqual(cursor.fetchall(), ([1, 'hello'], [2, 'ciao']))
conn.close()
finally:
rmtree(tmpdir)
示例2: assert_inserted
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def assert_inserted(test_case, host, port):
"""
Verify some data has been inserted into the database.
:param TestCase test_case: A test.
:param host: Host to connect to.
:param port: Port to connect to.
:return: ``Deferred`` that fires when we verify data has been inserted.
"""
d = get_postgres_connection(host, port, u"flockertest")
def assert_data(connection):
cursor = connection.cursor()
cursor.execute("SELECT * FROM testtable;")
fetched_data = cursor.fetchone()[0]
test_case.assertEqual(fetched_data, 123)
d.addCallback(assert_data)
return d
示例3: test_execute_dsn_kwargs
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def test_execute_dsn_kwargs():
q = 'SELECT 1'
with testing.postgresql.Postgresql() as postgresql:
dsn = postgresql.dsn()
conn = pg8000.connect(database=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = conn.cursor()
cur.execute(q)
subsegment = xray_recorder.current_segment().subsegments[-1]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['database_version']
示例4: test_execute_bad_query
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def test_execute_bad_query():
q = 'SELECT blarg'
with testing.postgresql.Postgresql() as postgresql:
dsn = postgresql.dsn()
conn = pg8000.connect(database=dsn['database'],
user=dsn['user'],
password='',
host=dsn['host'],
port=dsn['port'])
cur = conn.cursor()
try:
cur.execute(q)
except Exception:
pass
subsegment = xray_recorder.current_segment().subsegments[-1]
assert subsegment.name == 'execute'
sql = subsegment.sql
assert sql['database_type'] == 'PostgreSQL'
assert sql['user'] == dsn['user']
assert sql['database_version']
exception = subsegment.cause['exceptions'][0]
assert exception.type == 'ProgrammingError'
示例5: connect_db
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def connect_db(self):
''' Connect to PostgreSQL. '''
connect_args = {
'host': self._host,
'user': self._user,
'password': self._password,
'database': self._database,
}
if self._port is not None:
connect_args['port'] = self._port
db = pg8000.connect(**connect_args)
db.autocommit = True
if self._schema is not None:
cursor = db.cursor()
cursor.execute("SET SCHEMA '{}'".format(self._schema))
return db
示例6: connect_db
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def connect_db(self, user, password, database):
''' Return a connection to the specified database. '''
connect_args = {
'host': os.getenv('POSTGRES_HOST', 'localhost'),
'user': user,
'password': password,
'database': database,
'timeout': 1,
}
try:
connect_args['port'] = os.environ['POSTGRES_PORT']
except KeyError:
pass
db = pg8000.connect(**connect_args)
db.autocommit = True
return db
示例7: poststart
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def poststart(self):
with closing(pg8000.connect(**self.dsn(database='postgres'))) as conn:
conn.autocommit = True
with closing(conn.cursor()) as cursor:
cursor.execute("SELECT COUNT(*) FROM pg_database WHERE datname='test'")
if cursor.fetchone()[0] <= 0:
cursor.execute('CREATE DATABASE test')
示例8: is_server_available
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def is_server_available(self):
try:
with closing(pg8000.connect(**self.dsn(database='template1'))):
pass
except pg8000.Error:
return False
else:
return True
示例9: test_basic
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def test_basic(self):
try:
# start postgresql server
pgsql = testing.postgresql.Postgresql()
self.assertIsNotNone(pgsql)
params = pgsql.dsn()
self.assertEqual('test', params['database'])
self.assertEqual('127.0.0.1', params['host'])
self.assertEqual(pgsql.settings['port'], params['port'])
self.assertEqual('postgres', params['user'])
# connect to postgresql (w/ psycopg2)
conn = psycopg2.connect(**pgsql.dsn())
self.assertIsNotNone(conn)
self.assertRegexpMatches(pgsql.read_bootlog(), 'is ready to accept connections')
conn.close()
# connect to postgresql (w/ sqlalchemy)
engine = sqlalchemy.create_engine(pgsql.url())
self.assertIsNotNone(engine)
# connect to postgresql (w/ pg8000)
conn = pg8000.connect(**pgsql.dsn())
self.assertIsNotNone(conn)
self.assertRegexpMatches(pgsql.read_bootlog(), 'is ready to accept connections')
conn.close()
finally:
# shutting down
pid = pgsql.server_pid
self.assertTrue(pgsql.is_alive())
pgsql.stop()
sleep(1)
self.assertFalse(pgsql.is_alive())
with self.assertRaises(OSError):
os.kill(pid, 0) # process is down
示例10: test_with_statement
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def test_with_statement(self):
with testing.postgresql.Postgresql() as pgsql:
self.assertIsNotNone(pgsql)
# connect to postgresql
conn = pg8000.connect(**pgsql.dsn())
self.assertIsNotNone(conn)
conn.close()
self.assertTrue(pgsql.is_alive())
self.assertFalse(pgsql.is_alive())
示例11: get_postgres_connection
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def get_postgres_connection(host, port, database=None):
"""
Returns a ``Deferred`` which fires with a pg8000 connection when one
has been created.
See http://pythonhosted.org//pg8000/dbapi.html#pg8000.connect for
parameter information.
:param host: Host to connect to.
:param port: Port to connect to:
:param database: Database to connect to.
:return: ``Deferred`` that fires with a pg8000 connection.
"""
def connect_to_postgres():
try:
return connect(host=host, user=u"postgres", port=port,
database=database)
except (InterfaceError, ProgrammingError) as e:
Message.new(
message_type=u"acceptance:integration:postgres_connect",
exception=unicode(e.__class__), reason=unicode(e)).write()
return False
d = loop_until(reactor, connect_to_postgres)
return d
示例12: insert_data
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def insert_data(test_case, host, port):
"""
Insert some data into the database.
:param TestCase test_case: A test.
:param host: Host to connect to.
:param port: Port to connect to.
:return: ``Deferred`` that fires when data has been inserted.
"""
d = get_postgres_connection(host, port)
def create_database(connection):
connection.autocommit = True
cursor = connection.cursor()
cursor.execute("CREATE DATABASE flockertest;")
cursor.close()
connection.close()
d.addCallback(create_database)
d.addCallback(
lambda _: get_postgres_connection(host, port, u"flockertest"))
def add_data(connection):
cursor = connection.cursor()
cursor.execute(
"CREATE TABLE testtable (testcolumn int);")
cursor.execute(
"INSERT INTO testtable (testcolumn) VALUES (123);")
connection.commit()
connection.close()
d.addCallback(add_data)
return d
示例13: setup
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def setup(self):
pg8000.paramstyle = "qmark"
self.conn = pg8000.connect(host=self.host, port=self.port, ssl_context=self.ssl,
database=self.db_name,
user=self.db_user, password=self.db_pass)
self.conn.autocommit = True
self.cursor = self.conn.cursor()
示例14: with_pg8000
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def with_pg8000():
pg8000.paramstyle = "qmark"
t = PostgresConnectionData()
t.read_env()
pg_conn = pg8000.connect(host=t.host, port=t.port,
database=t.db_name,
user=t.db_user, password=t.db_pass)
pg_conn.autocommit = True
pg_cursor = pg_conn.cursor()
yield (pg_conn, pg_cursor)
pg_cursor.close()
pg_conn.close()
示例15: __enter__
# 需要导入模块: import pg8000 [as 别名]
# 或者: from pg8000 import connect [as 别名]
def __enter__(self):
self.conn = pg8000.connect(database='dnxfirewall', user=USER, password=PASSWORD, host='127.0.0.1', port=5432)
self.c = self.conn.cursor()
return self