Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_setdecoding(self):
# Force the result to be a string instead of unicode object. I'm not
# sure how to change the encoding for a single column. (Though I'm
# glad you can't - the communications encoding should not depend on
# per-column encoding like MySQL uses.)
self.cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf8', to=str)
self.cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf8', to=str)
self._test_strtype('varchar', 'test', self.SMALL_READ)
def pyodbc_connect_and_query(odbc_connect_string: str, query: str) -> pyodbc.Cursor:
try:
conn = pyodbc.connect(odbc_connect_string)
conn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
conn.setencoding(str, encoding='utf-8')
conn.setencoding(str, encoding='utf-8', ctype=pyodbc.SQL_CHAR)
# https://github.com/mkleehammer/pyodbc/issues/194 for this encoding fix
conn.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le')
except pyodbc.Error as e:
print("ODBC Connection Error Message:\n")
print(e)
print("ODBC error, exiting...\n")
sys.exit()
cursor = conn.cursor()
try:
cursor.execute(query)
except pyodbc.ProgrammingError as e:
def basicQuery(idNumber):
destination = "filemaker"
user = login(destination)[0]
cred = login(destination)[1]
# OPEN CONNECTION TO FILEMAKER DATABASE WITH DESCRIPTIVE METADATA
c = pyodbc.connect("DRIVER={FileMaker ODBC};DSN=pfacollection;SERVER=bampfa-pfm13.ist.1918.berkeley.edu;UID="+user+";PWD="+cred)
c.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
c.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
c.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-8')
c.setencoding(encoding='utf-8')
cursor= c.cursor()
# SQL TO GET REQUIRED METADATA VALUES FROM FM
cursor.execute("""SELECT m_245a_CompleteTitle, AlternativeTitle,
AccessionNumberPrefix, AccessionNumberDepositorNumber,
AccessionNumberItemNumber, ProjectGroupTitle,
m_257a_Country, m_260c_ReleaseYear,
ct_DirectorsNames, Credits,
GeneralNotes, m_945z_GeneralConditionNotes
FROM CollectionItem WHERE AccessionNumberItemNumber = ?""",idNumber)
rows = cursor.fetchall()
resultData = {}
resultList = [x for y in rows for x in y]
def initialize_connection(self):
connection = self._connection
if sys.version_info[0] < 3:
connection.setdecoding(pyodbc.SQL_CHAR, 'utf-8', pyodbc.SQL_CHAR)
connection.setdecoding(pyodbc.SQL_WCHAR, 'utf-32LE', pyodbc.SQL_WCHAR, unicode)
connection.setdecoding(pyodbc.SQL_WMETADATA, 'utf-32LE', pyodbc.SQL_WCHAR, unicode)
connection.setencoding(unicode, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding(str, 'utf-8', pyodbc.SQL_CHAR)
else:
connection.setdecoding(pyodbc.SQL_CHAR, 'utf-8', pyodbc.SQL_CHAR)
connection.setdecoding(pyodbc.SQL_WCHAR, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setdecoding(pyodbc.SQL_WMETADATA, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding('utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding('utf-8', pyodbc.SQL_CHAR)
self.__init_ns_decls__()
def query(idNumber,basename):
# OPEN CONNECTION TO FILEMAKER DATABASE WITH DESCRIPTIVE METADATA
c = pyodbc.connect("DRIVER={FileMaker ODBC};DATABASE=PFA_Collection;SERVER=bampfa-pfm13.ist.1918.berkeley.edu;UID=resourcespace;PWD=mediaarchive2017")
c.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
c.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
c.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-8')
c.setencoding(encoding='utf-8')
cursor= c.cursor()
# SQL TO GET REQUIRED METADATA VALUES FROM FM
# SQL TO GET REQUIRED METADATA VALUES FROM FM
cursor.execute("SELECT m_245a_CompleteTitle FROM CollectionItem WHERE AccessionNumberItemNumber = ?",idNumber)
# for row in cursor.tables():
# print(row.table_name)
rows = cursor.fetchall()
resultData = {}
resultList = [x for y in rows for x in y]
# print(resultList)
# A NULL RESULT WILL YIELD AN EMPTY LIST, SO CHECK THAT SOMETHING WAS FOUND, OR SKIP THE FILE.
------
Implement pooled connections.
http://initd.org/psycopg/docs/pool.html
"""
if isinstance(connection_params, str):
connection = backend.connect(connection_params)
elif isinstance(connection_params, dict):
connection = backend.connect(**connection_params)
else:
raise NotImplementedError
if backend.__name__ == "pyodbc":
connection.autocommit = False
connection.setencoding(encoding)
connection.setdecoding(pyodbc.SQL_CHAR, encoding=short_decoding)
connection.setdecoding(pyodbc.SQL_WCHAR, encoding=wide_decoding)
cursor = connection.cursor()
def rollback(crsr):
try:
try:
crsr.execute("ROLLBACK;")
except SQLOperationalError:
print("No transaction to rollback.")
except PyODBCProgrammingError as err:
print("ROLLBACK not supported.")
try:
yield connection, cursor
except PyODBCDatabaseError as err:
error, = err.args
Database.SQL_DOUBLE: 'FloatField',
Database.SQL_FLOAT: 'FloatField',
Database.SQL_GUID: 'TextField',
Database.SQL_INTEGER: 'IntegerField',
Database.SQL_LONGVARBINARY: 'BinaryField',
#Database.SQL_LONGVARCHAR: ,
Database.SQL_NUMERIC: 'DecimalField',
Database.SQL_REAL: 'FloatField',
Database.SQL_SMALLINT: 'SmallIntegerField',
Database.SQL_TINYINT: 'SmallIntegerField',
Database.SQL_TYPE_DATE: 'DateField',
Database.SQL_TYPE_TIME: 'TimeField',
Database.SQL_TYPE_TIMESTAMP: 'DateTimeField',
Database.SQL_VARBINARY: 'BinaryField',
Database.SQL_VARCHAR: 'TextField',
Database.SQL_WCHAR: 'CharField',
Database.SQL_WLONGVARCHAR: 'TextField',
Database.SQL_WVARCHAR: 'TextField',
}
def get_table_list(self, cursor):
"""
Returns a list of table names in the current database.
"""
# TABLES: http://msdn2.microsoft.com/en-us/library/ms186224.aspx
# TODO: Believe the below queries should actually select `TABLE_NAME, TABLE_TYPE`
if cursor.db.limit_table_list:
cursor.execute("SELECT TABLE_NAME, 't' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo'")
else:
cursor.execute("SELECT TABLE_NAME, 't' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'")
return [row_to_table_info(row) for row in cursor.fetchall()]
def initialize_connection(self):
connection = self._connection
if sys.version_info[0] < 3:
connection.setdecoding(pyodbc.SQL_CHAR, 'utf-8', pyodbc.SQL_CHAR)
connection.setdecoding(pyodbc.SQL_WCHAR, 'utf-32LE', pyodbc.SQL_WCHAR, unicode)
connection.setdecoding(pyodbc.SQL_WMETADATA, 'utf-32LE', pyodbc.SQL_WCHAR, unicode)
connection.setencoding(unicode, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding(str, 'utf-8', pyodbc.SQL_CHAR)
else:
connection.setdecoding(pyodbc.SQL_CHAR, 'utf-8', pyodbc.SQL_CHAR)
connection.setdecoding(pyodbc.SQL_WCHAR, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setdecoding(pyodbc.SQL_WMETADATA, 'utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding('utf-32LE', pyodbc.SQL_WCHAR)
connection.setencoding('utf-8', pyodbc.SQL_CHAR)
self.__init_ns_decls__()