I designed a python script that does something and then writes to a Azure SQL server. Originally, I wrote the script on a windows machine and everything works great. Now I want to deploy the script on raspberry pis in the field. Where i'm having the hiccup is trying to get the pi to write to the SQL server. I am using FreeTDS as my driver for pyodbc, and my .ini files and the config file are as show below:
odbcinst.ini
Description = FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
fileusage=1
dontdlclose=1
usagecount=1
odbc.ini
Driver = FreeTDS
Description = Azure cloud SQL server
Trace = no
ServerName = azure
database = main
TDS_Version = 7.0
freetds.conf
[azure]
host = ************.database.windows.net
port = 1433
tds version = 7.0
instance = MSSQLSERVER
python method
def writeData():
server = '***************.database.windows.net'
database = 'main'
username = '****'
password = '************'
driver = '{FreeTDS}'
with pyodbc.connect(
'DRIVER=' + driver + ';SERVER=' + server + ';PORT=1433;DATABASE=' + database + ';UID=' + username + ';PWD=' + password) as conn:
with conn.cursor() as cursor:
cursor.execute(
"UPDATE dbo.server_ips SET ip_address = ?, port = ?, DateTime= ? WHERE server = 1;", (external_ip, port, datetime.now()))
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
I'm not sure what I'm doing wrong, and I have tried searching for answers but have come up empty so far.
Turns out that writing to an azure sql server is not supported on ARM yet. I'm sure it will be soon considering windows 11 works on ARM processors.
Related
I am trying to connect to Azure SQL server (authentication via active directory password) on a Linux VM using Python and sqlalchemy:
The error is:
sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('HYT00', '[HYT00] [unixODBC][Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)'
)
(Background on this error at: https://sqlalche.me/e/14/e3q8)
ODBC driver installed on VM:
[ODBC Driver 18 for SQL Server]
Description=Microsoft ODBC Driver 18 for SQL Server
Driver=/opt/microsoft/msodbcql18/lib64/libmsodbcsql-18.1.so.2.1
UsageCount=1
Connection string used:
driver= "ODBC Driver 18 for SQL Server", endpoint="####-####.database.windows.net", dbname="sql#####", user="xxxxx#xxx.com", password="abc123"
connection_string = "DRIVER={};SERVER={},port=1433;DATABASE={};UID={};PWD={};Authentication=ActiveDirectoryPassword;".format(driver, endpoint, dbname, user, password)
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = sqlalchemy.create_engine(connection_url, fast_executemany=True, pool_size=100) conn = engine.connect()
Note:
SQL Server has only active directory password authentication
I have tried the endpoint with IP as well, no luck
I created linux virtual machine and sql database with Active directory authentication only. in Azure portal.
I installed ODBC Driver 17 for SQL Server in the virtual machine, and I tried to connect Azure sql database with active directory authentication using below code
import sqlalchemy as sa
username = "server"
password = "password"
host = "dbservere.database.windows.net"
database = "db"
authentication = "ActiveDirectoryPassword"
conn_string = sa.engine.url.URL.create(
"mssql+pyodbc",
username=username,
password=password,
host=host,
port=1433,
database=database,
query={"driver": "ODBC Driver 17 for SQL Server","authentication": authentication},
)
engine = sa.create_engine(conn_string, pool_timeout=30)
connection = engine.connect()
I got the same error.
Image for reference:
I uninstall the ODBC Driver 17 for SQL Server in the virtual machine, and install ODBC Driver 13 for SQL Server in the virtual machine again I tried with the above changing odbc driver version as below
import sqlalchemy as sa
username = "username"
password = "password"
host = "server.database.windows.net"
database = "db"
authentication = "ActiveDirectoryPassword"
conn_string = sa.engine.url.URL.create(
"mssql+pyodbc",
username=username,
password=password,
host=host,
port=1433,
database=database,
query={"driver": "ODBC Driver 13 for SQL Server","authentication": authentication},
)
engine = sa.create_engine(conn_string, pool_timeout=30)
connection = engine.connect()
it connected successfully to the Azure sql database.
Image for reference:
It worked for me kindly check from your side.
I have a SQL Server running on docker. I'm trying to connect to it from a web app written in python3 with pymssql.
It's working until I run the same app inside a docker container.
I get this error:
20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (nameofmydb.database.windows.net:1433)\n'.
Code:
pymssql.connect(server='mydb.database.windows.net', port='1433', database='mydb', user='user#server', password='pwd')
I tried with --net=host but it doesn't work either.
Can someone help me, please?
EDIT: So i finally succeeded after around 2 days , it was because of the installation of odbc driver in the container i was doing it in a wrong way.
Also i was forced to use pyodbc yes.
Install the odbc drivers + python the way azure tell you to in the docs and use pyodbc and not pymssql (which works outside of docker).
You can reference this blog: Adaptive server connection failed (DB-Lib error message 20002, severity 9).
Try to use pyodbc instead of pymssql. I tried and it works well.
import pyodbc
server = 'XXX.database.windows.net'
database = 'dbname'
username = 'username'
password = 'psd'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
The Azure document Quickstart: Use Python to query an Azure SQL database also provides the example:
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
Hope this helps.
So i did all of these .
But now i have this :
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
i used the odbcinst.ini that is in connection strngs in the azure pannel which correspond to :
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount=1
I changed driver and setup to suit the docker container.
The pyodbc code is :
pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};Server=tcp:mydb.database.windows.net,1433;Database=mydb;Uid=myuser#mydb;Pwd=mypwd;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'))
Also i repeat it but it looks that everything works well outside the docker container . i have absolutely no clue what is going on ...
I was facing the problem, seems to me that it was connection string which was causing the issue.
import pyodbc
server = 'xxxx.database.windows.net'
database = 'xxxx'
driver= '{ODBC Driver 17 for SQL Server}'
username='xxxxx#xxx.com'
password = 'xxxxxx'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password+';Authentication=ActiveDirectoryPassword') as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT top 4 * FROM [dbo].[TableName]")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
I am having terrible time connecting to SQL Server using pyodbc from the linux machine (Ubuntu 16.04).
conn = pyodbc.connect(r'DRIVER={FreeTDS};PORT=**; SERVER=**; DATABASE=**;UID=AA;PWD=hfghj;')
curr = conn.cursor()
curr.fast_executemany = True
query = "INSERT INTO dbo.STG_CONTACTABILITY_SCORE VALUES (?" + ",?"*21 + ")"
sql_data = list(map(tuple, i.values))
curr.executemany(query, sql_data)
I am getting following error:
('HY004', '[HY004] [FreeTDS][SQL Server]Invalid data type (0)
(SQLBindParameter)')
The same insert query works from my windows laptop when I change the connection string to:
conn = pyodbc.connect(r'DRIVER={SQL Server};PORT=**; SERVER=**; DATABASE=**; UID=AA; PWD=hfghj;')
If I change the Driver from FreeTDS to SQL SERVER on my Linux machine
I get the following error:
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open
lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
I searched for answers but there isn't much out there that could solve mine problem.
What is the driver to connect to SQL SERVER from Linux machine?
What is the driver to connect to SQL SERVER from Linux machine?
If you install Microsoft's ODBC driver as described in
Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS
then you can use DRIVER=ODBC Driver 17 for SQL Server or DRIVER=ODBC Driver 13 for SQL Server depending on which version you choose.
I am trying to connect to SQL Server running on Windows from a Redhat machine. I can login to the server through the "SQL Server" account but not from a "Windows Authentication" account. I tried all sorts of tricks - but none worked:
# isql FBI_MS CORP\\Kapilv kapil
# isql FBI_MS 192.168.14.158\\CORP\\Kapilv kapil
# isql FBI_MS 'CORP\Kapilv' kapil
I also googled and came across: https://askubuntu.com/questions/167491/connecting-ms-sql-using-freetds-and-unixodbc-isql-no-default-driver-specified
I have unixODBC drivers but do not have freetds. I should still be able to connect without freetds. Or is it impossible to connect with just unixODBC drivers?
-- I installed freetds as suggested by Benny Hill and followed the configurations - I could still not get it to work.
odbc.ini:
[FBI_MSW]
Description = FBI MS SQL Server database using freetds
Driver = freetds
Database = FBI
ServerName = 192.168.14.158
TDS_Version = 8.0
odbcinst.ini (RedHat installation of freetds does not come with a Set Up file
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/local/freetds/lib/libtdsodbc.so
Setup =
TraceFile = /tmp/freetds.log
FileUsage = 1
UsageCount = 1
Now, when I try to connect - I get the following error messages:
[root#localhost sqlncli-11.0.1790.0]# isql -v FBI_MSW CORP\\Kapilv kapil
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect
I cannot even connect to SQL Server account through freetds (which i could without freetds) - what can i say about Windows Authentication account?
What is the error message you got?
It is better to be sure, if you can reach the MS SQL server, so try: telnet 192.168.14.158 1433 (or the port it is running on)
You will need to install FreeTDS.
Look at this question and the answer I submitted there to see configuration examples.
I have a Perl script on a Linux (Ubuntu 8.10) machine and I need to write data to a SQL Server Database. I've been trying to use the DBD::ODBC module but I can't get it to connect. Where can I get a free/open source driver to use to use for the ODBC connection or is there another way to do this from Perl on Linux?
I connect to SQL Server 2005 with the stack of unixODBC, freeTDS (this is the driver) and DBD::ODBC.
After you install these components, edit /etc/unixODBC/odbc.ini to read like this:
[DNS]
Description = my database
Driver = /usr/lib/libtdsodbc.so #path to freeTDS driver
Server = ServerName
Database = DatabaseName
Port = 1433 #sql server default port
TDS_Version = 9.0 #9.0 is sql server 2005
try domain login = yes
try server login = yes
nt domain = DOMAIN
If all goes well, you should be able to connect with:
$dbh = DBI->connect('dbi:ODBC:DNS', "userName", "passWord");
Good luck!
Use the DBD::Sybase module, at one point Sybase and MS SQL Server shared a common codebase.
You may also want to investigate the open source FreeTDS libraries. See the FreeTDS FAQ Question "Which Perl library should I use".