Sqlalchemy issue with Azure SQL Database - python

I'm trying to connect to an Azure SQL Server via SQLAchemy (1.2.5), but I'm getting the following error regardless of the driver that I use:
Traceback (most recent call last):
File "C:\Users\user1\Desktop\test.py", line 27, in <module>
res = engine.connect().execute(q)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2102, in connect
return self._connection_cls(self, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2188, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2162, in _wrap_pool_connect
e, dialect, self)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception_noconnection
exc_info
File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 2158, in _wrap_pool_connect
return fn()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 345, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 784, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 532, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1189, in _do_get
self._dec_overflow()
File "C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 1186, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 350, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 477, in __init__
self.__connect(first_connect_check=True)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 677, in __connect
exec_once(self.connection, self)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 274, in exec_once
self(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 284, in __call__
fn(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\util\langhelpers.py", line 1334, in go
return once_fn(*arg, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 183, in first_connect
dialect.initialize(c)
File "C:\Python27\lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 1931, in initialize
super(MSDialect, self).initialize(connection)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 283, in initialize
self.do_rollback(connection.connection)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 457, in do_rollback
dbapi_connection.rollback()
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]111214;An attempt to complete a transaction has failed. No corresponding transaction found. (111214) (SQLEndTran)') (Background on this error at: http://sqlalche.me/e/f405)
This is the script which I use to try and read a table which generates the error:
from sqlalchemy import *
from datetime import datetime
import urllib, sqlalchemy
from urllib import quote_plus as urlquote
q = """
SELECT count(*) FROM [Products]
"""
params = urllib.quote_plus("Driver={ODBC Driver 13 for SQL Server};Server=mydb.database.windows.net,1433;Database=mydb;Uid=myuser;Pwd=mypwd;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;")
engine = sqlalchemy.engine.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
#engine = create_engine("mssql+pyodbc://myuser:mypwd#mydb.database.windows.net/mydb?charset=utf8&driver=ODBC+Driver+13+for+SQL+Server")
print engine
res = engine.connect().execute(q)
If I run the same query directly via pyodbc, everythin works fine:
import pyodbc
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};Server=mydb.database.windows.net,1433;Database=mydb;Uid=myuser;Pwd=mypwd;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;")
cursor = cnxn.cursor()
cursor.execute(q)
row = cursor.fetchone()
if row:
print row
The output, in this case, is this (the table is empty):
(0, )
Can anyone help me out on this?

Please try use Driver={SQL Server} instead of Driver={ODBC Driver 13 for SQL Server}. It works fine at my side with python 2.7.
import urllib
import pyodbc
from sqlalchemy import *
q = """
SELECT count(*) FROM test
"""
params = urllib.quote_plus("Driver={SQL Server};Server=tcp:your_server_name.database.windows.net,1433;Database=your_db;Uid=xxxx;Pwd=xxxx;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
print engine
res = engine.connect().execute(q)
print(res.fetchone())
Test result(I can fetch the result from azure sql):

Related

Writing to SQL Server from Python

This is the smallest example I can give that is a MRE
I am attempting to do the following:
Using pyodbc, read from a SQL Server instance (complete)
Then, print that data to verify it (complete)
Then, take that data, and insert it into a new table (or overwrite the table if it exists) <- FAILURE
The code is below:
import pyodbc
import pandas as pd
import sqlalchemy as sa
sqlConn = pyodbc.connect(
"DRIVER={SQL Server};"
"SERVER=servername;"
"DATABASE=dbname;"
"Trusted_Connection=yes;"
)
sql = """
SELECT TOP (1000) [PART]
,[STEP]
,[COMPLETIONTIME]
FROM [dbname].[dbo].[STEPS]
"""
engine = sa.create_engine('mssql+pyodbc://servername/dbname')
df = pd.read_sql(sql, sqlConn)
df.to_sql(name = 'Test', con = engine, if_exists = 'replace', index = False)
sqlConn.commit()
sqlConn.close()
I get the following for an error:
File "WiP.py", line 26, in <module>
df.to_sql(name = 'Test', con = engine, if_exists = 'replace', index = False)
File "C:\Python367-64\lib\site-packages\pandas\core\generic.py", line 2712, in to_sql
method=method,
File "C:\Python367-64\lib\site-packages\pandas\io\sql.py", line 518, in to_sql
method=method,
File "C:\Python367-64\lib\site-packages\pandas\io\sql.py", line 1319, in to_sql
table.create()
File "C:\Python367-64\lib\site-packages\pandas\io\sql.py", line 641, in create
if self.exists():
File "C:\Python367-64\lib\site-packages\pandas\io\sql.py", line 628, in exists
return self.pd_sql.has_table(self.name, self.schema)
File "C:\Python367-64\lib\site-packages\pandas\io\sql.py", line 1344, in has_table
self.connectable.dialect.has_table, name, schema or self.meta.schema
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2162, in run_callable
with self._contextual_connect() as conn:
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2242, in _contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2280, in _wrap_pool_connect
e, dialect, self
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 1547, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2276, in _wrap_pool_connect
return fn()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
return _ConnectionFairy._checkout(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\default.py", line 482, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/rvf5)
I looked that up on this site, and got the following:
Exception raised for errors that are related to the database’s
operation and not necessarily under the control of the programmer,
e.g. an unexpected disconnect occurs, the data source name is not
found, a transaction could not be processed, a memory allocation error
occurred during processing, etc.
I have also consulted:
Connecting to Microsoft SQL server using Python
List sql tables in pandas.read_sql
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html
https://github.com/mkleehammer/pyodbc/issues/300
Connecting to SQL Server 2012 using sqlalchemy and pyodbc
I also tried switching df.to_sql(name = 'Test', con = engine, if_exists = 'replace', index = False) to df.to_sql(name = 'Test', con = sqlConn, if_exists = 'replace', index = False) but got the exact same error.
What am I doing incorrectly? How can I wrote to a new table (or overwrite an existing one) from a Pandas dataframe?
UPDATE
It appears the connection is failing. The following:
import pyodbc
import pandas as pd
import sqlalchemy as sa
from sqlalchemy import create_engine
sqlConn = pyodbc.connect(
"DRIVER={SQL Server};"
"SERVER=servername;"
"DATABASE=dbname;"
"Trusted_Connection=yes;"
)
sql = """
SELECT TOP (1000) [ORDR_PART_NO]
,[OROP_ID]
,[COMPLETIONTIME]
FROM [dbname].[dbo].[OpsLookup]
"""
engine = sa.create_engine('mssql+pyodbc://servername/dbname')
cnxn = engine.connect()
result = cnxn.execute("SELECT TOP (1000) * FROM [dbname].[dbo].[STEPS]")
for row in result:
print(row)
cnxn.close()
Yields:
C:\Python367-64\lib\site-packages\sqlalchemy\connectors\pyodbc.py:79: SAWarning: No driver name specified; this is expected by PyODBC when using DSN-less connections
"No driver name specified; "
Traceback (most recent call last):
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2276, in _wrap_pool_connect
return fn()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\default.py", line 482, in connect
return self.dbapi.connect(*cargs, **cparams)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "WiP.py", line 21, in <module>
cnxn = engine.connect()
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2209, in connect
return self._connection_cls(self, **kwargs)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 103, in __init__
else engine.raw_connection()
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2307, in raw_connection
self.pool.unique_connection, _connection
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2280, in _wrap_pool_connect
e, dialect, self
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 1547, in _handle_dbapi_exception_noconnection
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\base.py", line 2276, in _wrap_pool_connect
return fn()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
self._dec_overflow()
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python367-64\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
return self._create_connection()
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "C:\Python367-64\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
connection = pool._invoke_creator(self)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Python367-64\lib\site-packages\sqlalchemy\engine\default.py", line 482, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/rvf5)
engine = sa.create_engine('mssql+pyodbc://servername/dbname')
needed to be changed to
engine = sa.create_engine('mssql+pyodbc://servername/dbname?trusted_connection=yes&driver=ODBC Driver 13 for SQL Server')
Per: https://docs.sqlalchemy.org/en/13/dialects/mssql.html#hostname-connections

connect teradata DB using sqlalchemy

I am trying to connect to Teradata using SqlAlchemy.
Below is the code:
from sqlalchemy import create_engine
username = 'userName'
password = 'pass#word'
host = 'hostname'
query = 'sel count(*) from databaseName.tableName;'
link = 'teradata://'+ username +':'+ password +'#'+host+'/'+'?driver=/teradata/client/15.10/odbc_64/lib/tdata.so'
td_engine = create_engine(link)
result = td_engine.execute(query)
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2074, in execute
connection = self.contextual_connect(close_with_result=True)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2123, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2162, in _wrap_pool_connect
e, dialect, self)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception_noconnection
exc_info
File "/ftp/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2158, in _wrap_pool_connect
return fn()
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 413, in connect
return _ConnectionFairy._checkout(self, self._threadconns)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 788, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 532, in checkout
rec = pool._do_get()
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1096, in _do_get
c = self._create_connection()
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 350, in _create_connection
return _ConnectionRecord(self)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 477, in __init__
self.__connect(first_connect_check=True)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/pool.py", line 671, in __connect
connection = pool._invoke_creator(self)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 106, in connect
return dialect.connect(*cargs, **cparams)
File "/ftp/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 410, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/ftp/lib/python2.7/site-packages/teradata/tdodbc.py", line 427, in __init__
connectParams["DRIVER"] = determineDriver(dbType, driver)
File "/ftp/lib/python2.7/site-packages/teradata/tdodbc.py", line 381, in determineDriver
" Available drivers: {}".format(driver, ",".join(drivers)))
sqlalchemy.exc.InterfaceError: (teradata.api.InterfaceError) ('DRIVER_NOT_FOUND', "No driver found with name '/teradata/client/15.10/odbc_64/lib/tdata.so'. Available drivers: composite70 ,composite70_2x ,composite70_x64 ,") (Background on this error at: http://sqlalche.me/e/rvf5)
Please suggest what could be the issue. I gave actual path of Teradata driver, i even tried changing driver to /teradata/client/15.10/odbc_64/lib and Teradata but still error is same, it is not able to find the Driver.

TypeError when SQLAlchemy engine.connect tries to compare MS SQL Server version

I'm on a Windows OS and Python 3.6.4 and I'm trying to connect to an InterSystem ODBC35 DSN Datasource using SQLAlchemy 1.2.5.
Using pyodbc to connect to the DSN Datasource works great but when using the SQLAlchemy create_engine method:
from sqlalchemy import create_engine
import pyodbc
engine = create_engine("mssql+pyodbc://user:pass#mydsn", echo=True)
cnxn = engine.connect()
rows = cnxn.execute("SELECT name FROM sys.tables").fetchall()
print(rows)
I get the following error:
2018-03-29 11:33:44,631 INFO sqlalchemy.engine.base.Engine SELECT CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR)
2018-03-29 11:33:44,631 INFO sqlalchemy.engine.base.Engine ()
Traceback (most recent call last):
File "mentrix.py", line 28, in <module>
cnxn = engine.connect()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\base.py", line 2102, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\base.py", line 90, in __init__
if connection is not None else engine.raw_connection()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\base.py", line 2188, in raw_connection
self.pool.unique_connection, _connection)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\base.py", line 2158, in _wrap_pool_connect
return fn()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 345, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 784, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 532, in checkout
rec = pool._do_get()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 1189, in _do_get
self._dec_overflow()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 1186, in _do_get
return self._create_connection()
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 350, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 477, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\pool.py", line 677, in __connect
exec_once(self.connection, self)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\event\attr.py", line 274, in exec_once
self(*args, **kw)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\event\attr.py", line 284, in __call__
fn(*args, **kw)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\util\langhelpers.py", line 1334, in go
return once_fn(*arg, **kw)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\strategies.py", line 183, in first_connect
dialect.initialize(c)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 1931, in initialize
super(MSDialect, self).initialize(connection)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\engine\default.py", line 267, in initialize
self._get_default_schema_name(connection)
File "C:\Users\m.m\Envs\mentrix\lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 1958, in _get_default_schema_name
if self.server_version_info < MS_2005_VERSION:
TypeError: '<' not supported between instances of 'str' and 'int'

Issue around connecting to a MySQL database table

I have some code which tries to connect to a mySQL database and retrieve a query.
The connection code is in a module called connection and looks like:
#!/usr/bin/env python3
import pandas as pd
import pymysql
from sqlalchemy import create_engine
COMPANY_SERVER = create_engine(
'mysql+pymysql://view_user:connectiondetails')
def read_sql(query):
return pd.read_sql_query(query, COMPANY_SERVER)
I have omitted the connection details for security. But this connection has worked in the past.
I am trying to connect and query a database table (which is pulled in with variable name table) and trying returning a DESCRIBE of the table in the following way:
#!/usr/bin/env python3
import datetime as dt
import numpy as np
import pandas as pd
from copy import copy
import pymysql
from sql.connection import read_sql
class DBSnapData(object):
def __init__(self,
start_dt,
end_dt,
start_point,
end_point,
variables,
table,
stock_id
):
self.start_dt = start_dt
self.end_dt = end_dt
self.start_point = start_point
self.end_point = end_point
self.variables = variables
self.table = table
self.stock_id = stock_id
self.schema = self.load_schema()
def load_schema(self):
return run_schema_sql_query(self.table)
def run_schema_sql_query(table):
return read_sql('DESCRIBE %s' % table)
I am getting the following error output:
2018-01-17 22:34:02,717 (pymysql.err.OperationalError) (1142, "SELECT command denied to user 'view_user'#'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'") [SQL: 'DESCRIBE TBL_MKTDATA_SNAPS_AUS']
Traceback (most recent call last):
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1142, "SELECT command denied to user 'view_user'#'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/scoleman/bin/export_mkt_data", line 405, in main
extract_by_id(sc_id, bbg_ticker, bb_id, lot_size_changes)
File "/home/scoleman/bin/export_mkt_data", line 346, in extract_by_id
stock_id)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 100, in __init__
self.schema = self.load_schema()
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 106, in load_schema
return run_schema_sql_query(self.table)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 19, in run_schema_sql_query
return read_sql('DESCRIBE %s' % table)
File "/home/scoleman/git/sql_data/sql/connection.py", line 17, in read_sql
return pd.read_sql_query(query, COMPANY_SERVER)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 331, in read_sql_query
parse_dates=parse_dates, chunksize=chunksize)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 1084, in read_query
result = self.execute(*args)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 975, in execute
return self.connectable.execute(*args, **kwargs)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 2064, in execute
return connection.execute(statement, *multiparams, **params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 939, in execute
return self._execute_text(object, multiparams, params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
statement, parameters
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
exc_info
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/sit
If anyone could give me a pointer as to a solution it would be much appreciated. I think the problem might be with pymysql in the connection but really am unsure.
Thanks
Your error says view_user doesn't have permission to query data on 192.168.9.132 this server.
You will have to do
GRANT SELECT ON *.* TO 'view_user'#'host_or_wildcard' IDENTIFIED BY 'password'; to allow your user to query on specified server.
Or
GRANT ALL PRIVILEGES ON *.* TO 'view_user'#'192.168.9.132'; to grant all priviliges.
P.S. To do above you will have to be root user. Use mysql -u root -p command to connect to mysql server.

sqlalchemy fails to connect to ms sql server

Trying to connect to SQL server using SQLAlchemy with pyodbc(freeTDS) as the driver; The connections succeeds if I use pyodbc directly:
>>> import pyodbc
>>> conn = pyodbc.connect('DSN=serverdsn;UID=user;PWD=password')
>>> crsr = conn.cursor()
>>> rows = crsr.execute("select ##VERSION").fetchall()
>>> print(rows)
[('Microsoft Azure SQL Data Warehouse - 10.0.9248.28 Sep 12 2017 01:08:55 Copyright (c) Microsoft Corporation', )]
>>> crsr.close()
>>> conn.close()
But when I use SQLAlchemy, it fails with a mysterious error:
>>> from sqlalchemy import create_engine
>>> e = create_engine("mssql+pyodbc://user:password#serverdsn")
>>> with e.connect() as con:
... rs = con.execute('select * from users')
... for row in rs:
... print(row)
...
Here is the full stack trace:
Traceback (most recent call last): File
"/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 1122, in _do_get
return self._pool.get(wait, self._timeout) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/queue.py",
line 145, in get
raise Empty sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 1, in
File
"/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py",
line 2091, in connect
return self._connection_cls(self, **kwargs) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py",
line 90, in init
if connection is not None else engine.raw_connection() File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py",
line 2177, in raw_connection
self.pool.unique_connection, _connection) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/base.py",
line 2147, in _wrap_pool_connect
return fn() File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 328, in unique_connection
return _ConnectionFairy._checkout(self) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 516, in checkout
rec = pool._do_get() File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 1138, in _do_get
self._dec_overflow() File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py",
line 66, in exit
compat.reraise(exc_type, exc_value, exc_tb) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/compat.py",
line 187, in reraise
raise value File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 1135, in _do_get
return self._create_connection() File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 333, in _create_connection
return _ConnectionRecord(self) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 461, in init
self.connect(first_connect_check=True) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/pool.py",
line 661, in __connect
exec_once(self.connection, self) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py",
line 246, in exec_once
self(*args, **kw) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/event/attr.py",
line 256, in __call
fn(*args, **kw) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py",
line 1331, in go
return once_fn(*arg, **kw) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py",
line 181, in first_connect
dialect.initialize(c) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/connectors/pyodbc.py",
line 165, in initialize
super(PyODBCConnector, self).initialize(connection) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py",
line 1742, in initialize
super(MSDialect, self).initialize(connection) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/engine/default.py",
line 256, in initialize
self.get_isolation_level(connection.connection) File "/Users/purecarscomputer/anaconda/envs/tensorflow/lib/python3.5/site-packages/sqlalchemy/dialects/mssql/base.py",
line 1735, in get_isolation_level
"tried views: %s; final error was: %s" % (views, err)) UnboundLocalError: local variable 'err' referenced before assignment
I've tried install and uninstall sqlalchemy and searched around on google, but not find a solution. Does anyone have similar problems and have a clue about what is happening?
OS information:
ProductName: Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G29
Here's the connection string I used to solve a connection problem with similar symptoms:
import urllib
from sqlalchemy import create_engine
# utilize existing odbc connection to create engine
params = urllib.quote_plus("DRIVER={}; SERVER=server; Database=database; UID=user; PWD=pw")
engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
ref http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#pass-through-exact-pyodbc-string

Categories