Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.cnxn = pyodbc.connect(self.connection_string)
self.cursor = self.cnxn.cursor()
# As of libmyodbc5w 5.3 SQLGetTypeInfo returns absurdly small sizes
# leading to slow writes. Override them:
self.cnxn.maxwrite = 1024 * 1024 * 1024
# My MySQL configuration (and I think the default) sends *everything*
# in UTF-8. The pyodbc default is to send Unicode as UTF-16 and to
# decode WCHAR via UTF-16. Change them both to UTF-8.
self.cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
self.cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
self.cnxn.setencoding(encoding='utf-8')
for i in range(3):
try:
self.cursor.execute("drop table t%d" % i)
self.cnxn.commit()
except:
pass
for i in range(3):
try:
self.cursor.execute("drop procedure proc%d" % i)
self.cnxn.commit()
except:
pass
def test_empty_string_encoding(self):
self.cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='shift_jis')
value = ""
self.cursor.execute("create table t1(s varchar(20))")
self.cursor.execute("insert into t1 values(?)", value)
v = self.cursor.execute("select * from t1").fetchone()[0]
self.assertEqual(v, value)
async def test_getTypeInfo_empty(conn, table):
cur = await conn.cursor()
await cur.getTypeInfo(pyodbc.SQL_CHAR)
resp = await cur.fetchall()
expected = [('char', 1, 255, "'", "'", 'length', 1, 0, 3, None, 0, 0,
'char', None, None, 1, 0, None, None)]
type_info = [tuple(r) for r in resp]
assert type_info == expected
def test_raw_decoding(self):
# Read something that is valid ANSI and make sure it comes through.
# The database is actually going to send us UTF-8 so don't use extended
# characters.
#
# REVIEW: Is there a good way to write UTF-8 into the database and read
# it out?
self.cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='raw')
self._test_strtype('varchar', self.SMALL_STRING)
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:
print("\nODBC Query Error Message:\n")
print(e)
def connect(self, *args, **kwargs):
connection = super(VirtuosoDialect, self).connect(*args, **kwargs)
if util.py2k:
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)
return connection
row_to_table_info = lambda row: row[0]
else:
row_to_table_info = lambda row: TableInfo(row[0].lower(), row[1])
import pyodbc as Database
SQL_AUTOFIELD = -777555
class DatabaseIntrospection(BaseDatabaseIntrospection):
# Map type codes to Django Field types.
data_types_reverse = {
SQL_AUTOFIELD: 'IntegerField',
Database.SQL_BIGINT: 'BigIntegerField',
Database.SQL_BINARY: 'BinaryField',
Database.SQL_BIT: 'NullBooleanField',
Database.SQL_CHAR: 'CharField',
Database.SQL_DECIMAL: 'DecimalField',
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',
from django.db.backends import BaseDatabaseIntrospection
import pyodbc as Database
SQL_AUTOFIELD = -777555
class DatabaseIntrospection(BaseDatabaseIntrospection):
# Map type codes to Django Field types.
data_types_reverse = {
SQL_AUTOFIELD: 'AutoField',
Database.SQL_BIGINT: 'IntegerField',
#Database.SQL_BINARY: ,
Database.SQL_BIT: 'BooleanField',
Database.SQL_CHAR: 'CharField',
Database.SQL_DECIMAL: 'DecimalField',
Database.SQL_DOUBLE: 'FloatField',
Database.SQL_FLOAT: 'FloatField',
Database.SQL_GUID: 'TextField',
Database.SQL_INTEGER: 'IntegerField',
#Database.SQL_LONGVARBINARY: ,
#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: ,
Database.SQL_VARCHAR: 'TextField',
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)
def connect(self, *args, **kwargs):
connection = super(VirtuosoDialect, self).connect(*args, **kwargs)
if util.py2k:
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)
return connection