I have DataGrip program, I tried to connect with it to azure sql database and it connect successfully.
I try to connect to azure sql database using sqlalchemy but it fail, here is the code:
from sqlalchemy import create_engine
try:
connection = create_engine("mysql+pymysql://<username>:<password>#<dbserver>.database.windows.net:1433/<dbname>").connect()
with connection:
with connection.begin():
print("OK")
except Exception as err:
print(err)
then the result was:
(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
(Background on this error at: https://sqlalche.me/e/14/e3q8)
I tried to use pyodbc instead of pymysql but the result was:
(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: https://sqlalche.me/e/14/rvf5)
the final goal is deploy my fastapi project into azure as webapp then use azure sql.
how can I make this working ?
Related
I have a Python program which is trying to connect to a MySQL 8.31 database. This is the code I am using.
connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;DATABASE=%s;UID=root;PWD=password" % db_name
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = create_engine(connection_url)
pd.read_sql('select * from apps_list', engine)
I get this exception
sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001',
'[08001] [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes
Provider: Could not open a connection to SQL Server 1. (2)
(SQLDriverConnect); [08001] [Microsoft][ODBC Driver 17 for SQL
Server]Login timeout expired (0); [08001] [Microsoft][ODBC Driver 17
for SQL Server]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. (2)') (Background on this error at:
https://sqlalche.me/e/14/e3q8)
I have researched this exception and most people recommend using SQL Server Configuration manager which I do not seem to have on my system.
Not in Computer Management
Not in C:\Windows\SysWOW64
Others have said to ensure Named Pipes are allowed which they are
And finally I've ensured Ports 3306 and 1433 are open
Yet I still get this exception.
For the record I am able to connect using this code
mydb = pymysql.connect(
host="localhost",
database=db_name,
user="root",
password="****",
autocommit=True
)
mycursor = mydb.cursor()
engine = create_engine('mysql+pymysql://****#localhost/%s' % db_name )
However this gives me a warning and tells me to use SQLAlchemy:
userwarning: pandas only support SQLAlchemy
connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2
connectionother DBAPI2 objects are not tested, please consider using
SQLAlchemy warnings.warn(
I am slightly confused at this because I am using SQLAlchemy to create the connection on this line
engine = create_engine('mysql+pymysql://****#localhost/%s' % db_name )
Is it fine the way I am currently connecting? Please let me know how I should connect.
I'm trying to write to MSSQL using SQL Alchemy but can't get the connection to work. I've got
engine = create_engine('mssql+pyodbc://<Username>#localhost:5432/<DBName>?driver=SQL+Server+Native+Client+11.0')
df.to_sql('<TableName>', con=engine, if_exists='append')
This gives:
OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (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. (10061)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)
I've looked at the error documentation and tried several different things with the connection string but nothing seems to work. Reading from the same database using pyodbc works fine:
con = pyodbc.connect('Driver={SQL Server};'
'Server=<Server>;'
'Database=<DBName>;'
'Trusted_Connection=yes;')
df = pd.read_sql_query('select * from <DBName>.dbo.<TableName>', con)
I am looking for a solution for inserting CSV file to SQL Server and for inserting data to SQL Server I faced with below error.
I read lot about this issue and did several changes but nothing happened and still I can't import csv to relative table. I can't understand why this error also happened?
Microsoft SQL has been installed on my local computer:
conn = pyodbc.connect('Driver=SQL Server; Server=ServerName\MSSQLSERVER; Database=DBName;Trusted_Connection=yes ; UID=Administrator')
Error:
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBMSLPCN]Invalid connection. (14) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server
Driver][DBMSLPCN]ConnectionOpen (ParseConnectParams()). (14)')
For anyone who faced with above problem,If you want to connect to sql server with SQL Authentication, the format will be changed as below:
pyodbc.connect('Driver={SQL Server}; Server=ServerName;
Database=DBName; UID=UserName; PWD= {Password};')
I am having problem querying to a MariaDB database that is hosted on a Virtual Machine. I am using the dataset Python module.
This is my code:
import dataset
db = dataset.connect("mysql+pymysql://....")
Q = "select * from mytable Limit 1"
print(db.query(Q))
However I would get this error message:
(pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server
during query ([WinError 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond)') (Background on this error at: http://sqlalche.me/e/e3q8)
I have tried to configure the timeout variables such as; connect_timeout, wait_timeout etc within the my.cnf file and sending a very simple query, but the issue still exist.
Any solutions or suggestions would be highly appreciated. Thank you
So I am trying to write a data frame to Microsoft SQL Server using the pandas to_sql function.
I have created an engine using
engine = sqlalchemy.create_engine(
'mssql:///Server/Database?driver=SQL Server Native Client 11.0'
)
con = engine.connect()
switchers.to_sql('check',engine)
The error I am getting is as follows :
OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. (2) (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. (2)') (Background on this error at: http://sqlalche.me/e/e3q8)
Any idea what I should be looking for?
Your connection string should be:
engine =
sqlalchemy.create_engine('mssql+pyodbc://Server/Database?driver=SQL+Server+Native+Client+11.0')