pyodbc.OperationalError - Read from the server failed [duplicate] - python

This question already has an answer here:
Read from the server failed (20004) (SQLExecDirectW)Read from the server failed (20004) (SQLExecDirectW)
(1 answer)
Closed last month.
I am using mssql server in ubuntu, and tries to access using pyodbc
from sqlalchemy import create_engine
engine = create_engine('mssql+pyodbc:///?odbc_connects="Driver=;SERVER=;DATABASE=;UID=;PWD=;port=;TDS_Version=8.0")
using this statement, I got access to the database,
I tried to read the data from the table using sqlalchemy from flask
session.query(table1).filter(table1.col>4).all()
I got this error
sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08S01', '[08S01] [FreeTDS][SQL Server]Write to the server failed (20006) (SQLExecDirectW)')
The important note: this error happens randomly, which means, when executing this query, sometimes, I got the data and sometimes, I got this error
A few times, I got this error:
ERROR - sqlalchemy.pool.impl.QueuePool - Exception during reset or similar
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 502, in do_rollback
dbapi_connection.rollback()
pyodbc.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Write to the server failed (20006) (SQLEndTran)')
What is the possible reason for this error? Thanks.

Can you try below! and let me know if it works!!
from your statement - when you say sometimes it works and sometimes it dosen't - does the same query work again when you re-run to reset the conection again by running the create_engine again. if yes then please try setting up connection like below and try
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
connection_string = "DRIVER=;SERVER=;DATABASE=;UID=;PWD=;?autocommit=true"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = create_engine(connection_url)
I have added IMPORT URL from sqlalchemy, you can ignore it if you like (i prefer my connection this way)
now the main difference in my connection is , if you look at this line specifically
connection_string = "DRIVER=;SERVER=;DATABASE=;UID=;PWD=;?autocommit=true"
is using this in the end - autocommit=true - please give it a try.

Related

Connecting to Teradata using SQLAlchemy results in errors

I'm trying to connect to Teradata with SQLAlchemy and then save the results as pandas.DataFrame.
engine = create_engine(f"teradata://{UID}:{PWD}#host/?dsn={DSN}")
query = """SELECT TOP 10 * from db_5.mobile_sa"""
with engine.connect() as connect:
df = pd.read_sql(query,connect)
This results in an error:
sqlalchemy.exc.DatabaseError: (teradata.api.DatabaseError) (11470, '[HYC00] [Cloudera][ODBC] (11470) Transactions are not supported.') (Background on this error at: https://sqlalche.me/e/14/4xp6)
I've found that I should try to set autocommit to true, so I tried solutions from SQLAlchemy documentation and added isolation_level="AUTOCOMMIT" to create_engine() , but this time I got
TypeError: Invalid argument(s) 'isolation_level' sent to create_engine(), using configuration TeradataDialect/SingletonThreadPool/Engine. Please check that the keyword arguments are appropriate for this combination of components.
I also tried to add &autocommit=true to connection url, but that didn't seem to have an effect.
How do I go around it?

Invalid transaction error with SQLAclhemy, sqlalchemy-aurora-data-api

I have a Falsk app that is using sqlalchemy-aurora-data-api the package to connect to MySQL db from the AWS RDS.
In the app I am using raw queries eg. f'SELECT * FROM customers WHERE id={id}'
And to execute this query I use
conn.execute(customer_query).one()
and this is the connection string that I use to connect to the dB, not using sessions here
conn = create_engine(f'mysql+auroradataapi://:#/{db_name}',
connect_args=dict(aurora_cluster_arn=cluster_arn, secret_arn=secret_arn)).connect()
There is a random error that comes unexpected
Some error occurred in fetching data from customers table (botocore.errorfactory.BadRequestException) An error occurred (BadRequestException) when calling the RollbackTransaction operation: Invalid transaction ID
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I can't really figure out the solution to this problem.
How can I solve this problem?

sqlalchemy - pass through exact pyodbc string returning SQL server permission error?

The following code is working only using pyodbc -
conn_string = 'DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password
conn = pyodbc.connect(conn_string)
sql_query = 'SELECT * FROM table'
df = pd.read_sql(sql_query,conn)
However, when I try to use the pass through exact pyodbc string method of creating a sqlaclehmy engine, I get a permission error -
conn_string = 'DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password
params = urllib.parse.quote_plus(conn_string)
engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
engine.connect()
Error -
SAWarning: Could not fetch transaction isolation level, tried views: ('sys.dm_exec_sessions',
'sys.dm_pdw_nodes_exec_sessions'); final error was: ('42000', '[42000] [Microsoft]
[ODBC Driver 17 for SQL Server][SQL Server]User does not have permission to perform this action.
(6004) (SQLExecDirectW)')
(I tried with and without the port included)
I'm confused on where this permission error is coming from because its working when only using pyodbc. is there some parameter im missing here?
Solution - https://github.com/catherinedevlin/ipython-sql/issues/105
I know this is an older issue, but since I just resolved a similar
issue, I'd thought I would share. This is an issue with SQL DW, not
the SQL Alchemy engine that ipython-sql uses.
I did notice that configuring SQL Magic to use autocommit doesn't seem
to work with MSSQL. Go ahead and add that to your connection string:
%sql mssql+pyodbc://user:pwd#MSSQL_DSN?autocommit=true
You should be able to connect using an Admin account. For general
users:
"Could not fetch transaction isolation level" is a SQL error. This
means the user does not have permissions to see what the current
isolation is. For Azure SQL Data Warehouse, those permissions are in
this view:
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-pdw-exec-connections-transact-sql
Since VIEW SERVER STATE is not supported in SQL DW, you need to use
"GRANT VIEW DATABASE STATE TO user" instead:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/43a4f051-2a12-4a15-8362-3d7a67f5c88f/unable-to-give-permission-on-azure-sql-data-warehouse-catalog-views-and-dmvs?forum=AzureSQLDataWarehouse

I have a connexion problem to sql server using sqlalchemy wih python

I want to connect into a sql server database using sqlalchemy and python.
but when I run the code below I get the following error
OperationalError: (pyodbc.OperationalError) ('08001', '[08001]
[Microsoft][SQL Server Native Client 11.0]SQL Server Network
Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
(-1) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client
11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred
while establishing a connection to SQL Server. Server is not found or
not accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see SQL
Server Books Online. (-1)') (Background on this error at:
http://sqlalche.me/e/e3q8)
I think the problem has something to do with the driver, but I can't seem to understand it.
here is my code:
from sqlalchemy.sql import text
from sqlalchemy import create_engine
import secrets
engine = create_engine( 'mssql+pyodbc://servername/test_db? driver=SQL+Server+Native+Client+11.0')
conn = engine.connect()
s = text("SELECT * FROM user_tab ")
result = conn.execute(s).fetchall()
print(result)
Can you help me please!
You're connecting to a DB without a user, password, hostname or port.
When you look at the example in the documentation this is written:
# pymssql
engine = create_engine('mssql+pymssql://scott:tiger#hostname:port/dbname')
As you can see, this url contains way more information than you have in your url.
You just got an url with a name and a dbname. I don't know what the first name represents..
Most of the time the hostname will be localhost and the port 1434. But make sure where your mssqlserver is running and which user is allowed to make queries on the db!
Documentation: https://docs.sqlalchemy.org/en/13/core/engines.html

Need help connecting to SQL Server from Python Flask-Appbuilder

I'm new to Python + Flask + Flask Appbuilder but I am a professional Java developer. I've been working on a small app that I initially used SqlLite and now I want to move into SQL Server, which will be the production database.
I can't seem to get the connection right.
I have tried using a DSN but I get an error message indicating there is a mismatch between the driver and something else (Python?). The searches on this error seem to indicate the driver is 32 bit and Python is 64. Still I can't get that working so I thought I'd try to connect directly. I'd prefer not using a DSN anyway. I've searched the web and can't find an example that works for me.
I have imported pyodbc. This is the current way I'm trying to connect:
params = urllib.quote_plus("DRIVER={SQL Server};SERVER=devsql07:1433;DATABASE=DevOpsSnippets;UID=<user>;PWD=<password>")
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc:///?odbc_connect=%s" % params
This produces the following error message:
2016-02-17 07:11:38,115:ERROR:flask_appbuilder.security.sqla.manager:DB Creation and initialization failed: (pyodbc.Error) ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]Invalid connection. (14) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (ParseConnectParams()). (14)')
Can anyone help me get this connection correct?
I really appreciate any help.
if you are using pyodbc you should be able to connect this way
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=yourServer;DATABASE=yourDatabase;UID=;PWD=')
#putting to use
SQL = "select Field1, Field2 from someTable"
cursor = cnxn.cursor()
cursor.execute(SQL)
row = cursor.fetchall()
for r in row:
print r[0] #field1
print r[1] #field2
The port should be specified through a comma. Specify the connection string as
DRIVER={SQL Server};SERVER=devsql07,1433;DATABASE.....

Categories

Resources