本文整理匯總了Python中psycopg2.extensions.cursor方法的典型用法代碼示例。如果您正苦於以下問題:Python extensions.cursor方法的具體用法?Python extensions.cursor怎麽用?Python extensions.cursor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.cursor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: as_string
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def as_string(self, context):
# is it a connection or cursor?
if isinstance(context, ext.connection):
conn = context
elif isinstance(context, ext.cursor):
conn = context.connection
else:
raise TypeError("context must be a connection or a cursor")
a = ext.adapt(self._wrapped)
if hasattr(a, 'prepare'):
a.prepare(conn)
rv = a.getquoted()
if sys.version_info[0] >= 3 and isinstance(rv, bytes):
rv = rv.decode(ext.encodings[conn.encoding])
return rv
示例2: test_subclass_rollback
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_subclass_rollback(self):
rollbacks = []
class MyConn(ext.connection):
def rollback(self):
rollbacks.append(None)
super(MyConn, self).rollback()
try:
with self.connect(connection_factory=MyConn) as conn:
curs = conn.cursor()
curs.execute("insert into test_with values (11)")
1/0
except ZeroDivisionError:
pass
else:
self.assert_("exception not raised")
self.assertEqual(conn.status, ext.STATUS_READY)
self.assert_(rollbacks)
curs = conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [])
示例3: test_with_error
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_with_error(self):
try:
with self.conn as conn:
with conn.cursor() as curs:
curs.execute("insert into test_with values (5)")
1/0
except ZeroDivisionError:
pass
self.assertEqual(self.conn.status, ext.STATUS_READY)
self.assert_(not self.conn.closed)
self.assert_(curs.closed)
curs = self.conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [])
示例4: _pipeDbCursorToTsvFile
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def _pipeDbCursorToTsvFile(self, dbCursor, tsvFile, include_columns=True):
"""
Pipe any arbitrary DB cursor to a TSV file.
"""
# Extract DB columns.
columns = dbCursor.description
numColumns = len(columns)
if include_columns:
# Write TSV header.
for i in range(numColumns - 1):
# 0th index is column name.
tsvFile.write("%s\t" % columns[i][0])
tsvFile.write("%s\n" % columns[numColumns - 1][0])
# By default, cursor iterates through both header and data rows.
self._numRows = 0
row = dbCursor.fetchone()
while row is not None:
for i in range(numColumns - 1):
tsvFile.write("%s\t" % row[i])
tsvFile.write("%s\n" % row[numColumns - 1])
row = dbCursor.fetchone()
self._numRows += 1
示例5: _processPatientEpisodeDbCursor
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def _processPatientEpisodeDbCursor(self):
"""
Convert self.patientEpisodeInput from DB cursor to TSV file.
"""
# Instantiate patientEpisodeTempFile.
patientEpisodeTempFile = open(self._patientEpisodeTempFileName, "w")
self._pipeDbCursorToTsvFile(self.patientEpisodeInput, patientEpisodeTempFile)
patientEpisodeTempFile.close()
self.patientsProcessed = True
if LocalEnv.DATABASE_CONNECTOR_NAME == 'psycopg2':
return self.patientEpisodeInput.rowcount
elif LocalEnv.DATABASE_CONNECTOR_NAME == 'sqlite3':
# In sqlite3, rowcount is somehow "always" -1; See for details:
# https://docs.python.org/3.0/library/sqlite3.html#sqlite3.Cursor.rowcount
return self._numRows #self.patientEpisodeInput.rowcount
示例6: _pipeDbCursorToTsvFile
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def _pipeDbCursorToTsvFile(self, dbCursor, tsvFile):
"""
Pipe any arbitrary DB cursor to a TSV file.
"""
# Extract DB columns.
columns = dbCursor.description
numColumns = len(columns)
# Write TSV header.
for i in range(numColumns - 1):
# 0th index is column name.
tsvFile.write("%s\t" % columns[i][0])
tsvFile.write("%s\n" % columns[numColumns - 1][0])
# By default, cursor iterates through both header and data rows.
self._numRows = 0
row = dbCursor.fetchone()
while row is not None:
for i in range(numColumns - 1):
tsvFile.write("%s\t" % row[i])
tsvFile.write("%s\n" % row[numColumns - 1])
row = dbCursor.fetchone()
self._numRows += 1
示例7: setPatientEpisodeInput
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def setPatientEpisodeInput(self, patientEpisodeInput, \
patientIdColumn = "patient_id", timestampColumn = "item_date"):
"""
Define the input patient episode list for the feature matrix.
patientEpisodeInput: TSV file descriptor or DB cursor.
patientIdColumn: Name of TSV column or DB column.
timestampColumn: Name of TSV column or DB column.
"""
# Verify patientEpisodeInput is TSV file or DB cursor.
if not isinstance(patientEpisodeInput, cursor) and \
not isinstance(patientEpisodeInput, file):
raise TypeError("patientEpisodeInput must be DB cursor or TSV file.")
self.patientEpisodeInput = patientEpisodeInput
self.patientEpisodeIdColumn = patientIdColumn
self.patientEpisodeTimeColumn = timestampColumn
示例8: _solve_conn_curs
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def _solve_conn_curs(conn_or_curs):
"""Return the connection and a DBAPI cursor from a connection or cursor."""
if conn_or_curs is None:
raise psycopg2.ProgrammingError("no connection or cursor provided")
if hasattr(conn_or_curs, 'execute'):
conn = conn_or_curs.connection
curs = conn.cursor(cursor_factory=_cursor)
else:
conn = conn_or_curs
curs = conn.cursor(cursor_factory=_cursor)
return conn, curs
示例9: cursor
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def cursor(self, *args: Any, **kwargs: Any) -> TimeTrackingCursor:
kwargs.setdefault('cursor_factory', TimeTrackingCursor)
return connection.cursor(self, *args, **kwargs)
示例10: setUp
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def setUp(self):
ConnectingTestCase.setUp(self)
curs = self.conn.cursor()
try:
curs.execute("delete from test_with")
self.conn.commit()
except psycopg2.ProgrammingError:
# assume table doesn't exist
self.conn.rollback()
curs.execute("create table test_with (id integer primary key)")
self.conn.commit()
示例11: test_with_ok
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_with_ok(self):
with self.conn as conn:
self.assert_(self.conn is conn)
self.assertEqual(conn.status, ext.STATUS_READY)
curs = conn.cursor()
curs.execute("insert into test_with values (1)")
self.assertEqual(conn.status, ext.STATUS_BEGIN)
self.assertEqual(self.conn.status, ext.STATUS_READY)
self.assert_(not self.conn.closed)
curs = self.conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [(1,)])
示例12: test_with_connect_idiom
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_with_connect_idiom(self):
with self.connect() as conn:
self.assertEqual(conn.status, ext.STATUS_READY)
curs = conn.cursor()
curs.execute("insert into test_with values (2)")
self.assertEqual(conn.status, ext.STATUS_BEGIN)
self.assertEqual(self.conn.status, ext.STATUS_READY)
self.assert_(not self.conn.closed)
curs = self.conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [(2,)])
示例13: test_with_error_db
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_with_error_db(self):
def f():
with self.conn as conn:
curs = conn.cursor()
curs.execute("insert into test_with values ('a')")
self.assertRaises(psycopg2.DataError, f)
self.assertEqual(self.conn.status, ext.STATUS_READY)
self.assert_(not self.conn.closed)
curs = self.conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [])
示例14: test_with_error_python
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_with_error_python(self):
def f():
with self.conn as conn:
curs = conn.cursor()
curs.execute("insert into test_with values (3)")
1/0
self.assertRaises(ZeroDivisionError, f)
self.assertEqual(self.conn.status, ext.STATUS_READY)
self.assert_(not self.conn.closed)
curs = self.conn.cursor()
curs.execute("select * from test_with")
self.assertEqual(curs.fetchall(), [])
示例15: test_subclass
# 需要導入模塊: from psycopg2 import extensions [as 別名]
# 或者: from psycopg2.extensions import cursor [as 別名]
def test_subclass(self):
closes = []
class MyCurs(ext.cursor):
def close(self):
closes.append(None)
super(MyCurs, self).close()
with self.conn.cursor(cursor_factory=MyCurs) as curs:
self.assert_(isinstance(curs, MyCurs))
self.assert_(curs.closed)
self.assert_(closes)