Ubuntu 22.04, Qt 6.2.0:
QSqlDatabase _db;
bool OdbcSql::connectToDB(QUrl host, QString database, QString username, QString password)
{
_db.setDatabaseName(QStringLiteral("Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1; Server=%1;Database=%2").arg(host.url()).arg(database));
_db.setUserName(username);
_db.setPassword(password);
_db.setConnectOptions("SQL_ATTR_CONNECTION_TIMEOUT=5;SQL_ATTR_LOGIN_TIMEOUT=5");
bool ret = _db.open();
if (!ret) _db.lastError();
return ret;
}
QSqlError("", "QODBC: Unable to connect", "[Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000086:SSL routines::certificate verify failed:EE certificate key too weak] [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection")
The server is not under my control.
Because it's an internal connection between two machines inside a LAN, can I disable the SSL requirement for the ODBC connection in order to fix this issue?
Is there any other way to fix it?
Related
Need help using a trusted connection to connect to SQL Server using SQLALCHEMY
So far I have this code to establish the connection
import sqlalchemy as sa
engine = sa.create_engine("mssql+pyodbc://REPORTING/REPORTING_System?driver={SQL Server}?TrustedConnection=yes")
view = f"SELECT Date From Table"
df = pd.read_sql_query(view, engine)
However I got this error message - DBAPIError: (pyodbc.Error) ('01S00', '[01S00] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/dbapi)
I tried to change the driver to {ODBC Driver 17 for SQL Server}
It did not help.
I'm trying to connect to a SQL server database using pyodbc in Python 3. But I get an error when I'm trying to establish the connection.
I do something like this:
import pyodbc
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
And I get this:
OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol][error:140B40C7:SSL routines:SSL_do_handshake:peer did not return a certificate] (-1) (SQLDriverConnect)')
Does anybody know how to solve this? The database is not my own, so I hope there is a solution that doesn't require changing any settings there.
I'm running Ubuntu within the Windows Subsystem for Linux.
There is a breaking change in ODBC Driver 18 for SQL Server
Similar to the HTTP to HTTPS default changes made in web browsers a few years back (and the security reasons for them), we are changing the default value of the Encrypt connection option from no to yes/mandatory.
ODBC Driver 18.0 for SQL Server Released
So this
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;')
is the same as
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=yes')
If you don't want an encrypted connection you must opt out:
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no')
We also changed the behavior of TrustServerCertificate to not be tied to the Encrypt setting
So if your server is using a self-signed certificate, you also must opt out of certificate validation. so
conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=192.168.2.250;Database=DB;UID=username;PWD=password;Encrypt=no;TrustServerCertificate=yes')
I ended up taking my script out of WSL. Running the same command (with David's additions or ODBC Driver 17 for SQL Server instead of 18) under Windows works without issues in my case.
I am trying to connect my qt application to ms sql database. I dont know server machine's IP addres and PORT number. I allready have Data Source Name in System DSN. Here is the sample of my code :
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER={serverName};DATABASE=dbname;UID=username;PWD=password;WSID=.;Trusted_connection=yes");
bool ok = db.open();
if(ok == true){
qDebug()<<"connected";
}
else{
qDebug()<<"not connected";
qDebug()<<db.lastError().text();
}
And i get this error :
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user
'xxx'. QODBC3: Unable to connect.
Any help will be appreciated.
A few things here.
First, you either use windows authentication, or you use SQL authentication. Not both.
If your connection string specifies Trusted_connection=yes, or integrated security=SSPI, then you're using windows authentication.
If your connection string specifies uid=someuser; pwd=somepassword then you're using SQL auth.
Pick one.
Now, if you're using an ODBC DSN, then the DSN already specifies the driver and server, so you don't need that in your application connection string. Instead, you just tell the application which DSN to use.
Let's say you created your DSN and called it "MySqlServer". Then your code will look like this:
db.setDatabaseName("MySqlServer");
Now, if the DSN is set up to use windows authentication, that's all you need to do. On the other hand, if it's set up to use SQL authentication, then you can take the credentials already specified in the DSN, or set a username and password in your code:
db.setUserName("myUserName");
db.setPassword("myPassword");
I am studying Core Java Volume 2 Advanced Features. I have to connect to any database to execute the source code in Database Programming chapter. I cannot share the whole source code probably because of copyright issues. But basically I am trying to connect to SQL Server with a simple TestDb java program and the connection properties such as DriverName, DB Url , Username and Password are located in a db.properties file as follows:
#jdbc.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433 ;databaseName=COREJAVA;integratedSecurity=true"
jdbc.username="sa"
jdbc.password="123456"
The code for connection is present in the getConnection() function as follows:
public static Connection getConnection() throws SQLException, IOException
{
Properties props = new Properties();
try (InputStream in = Files.newInputStream(Paths.get("database.properties")))
{
props.load(in);
}
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null) System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password);
}
I fixed the error of TCP first thanks to this stackpost
JDBC connection failed, error: TCP/IP connection to host failed
But now when I run the program using the following command from cmd:
java -cp .;sqljdbc4.jar test.TestDB
I get the following stacktrace:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '"sa"'. ClientConnectionId:2986bb7f-4f57-4932-b4c1-614ab3684ee0
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at test.TestDB.getConnection(TestDB.java:68)
at test.TestDB.runTest(TestDB.java:35)
at test.TestDB.main(TestDB.java:19)
I have referred to Java connection to a SQL Server Database: Login failed for user 'sa'
I have IPALL port set to 1433, TCP/IP enabled in Server Configuration Manager and I am able to login normally using SQL server management studio with the 'sa' account. I am using the latest sqljdbc4.jar from Microsoft's website.
#GordThompson thank you! I removed the double quotes from both username and password making
jdbc.username=sa
jdbc.password=123456
And it is connecting to the database now!
Anyone know a good reason why bcp cannot connect to a sql server hosted by AWS while SSMS can?
I have double checked the server and user account details and they both match.
I'm using the generic command to import a csv file:
bcp DB_Name.dbo.Table in "somefile_file.csv" -c -S xxx.rds.amazonaws.com -U username -P xxx -b 1000
The error is:
SQLState = 08001, NativeError = 53
Error = [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
Error = [Microsoft][ODBC Driver 13 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.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired
Is bcp using a different port maybe?
The first error shows that bcp uses the Named Pipes protocol to connect to the server. Apparently, this protocol is inaccessible for network connections.
On the client workstation where bcp is run, check SQL Server Configuration Manager and ensure that, in the client configuration list TCP/IP is enabled and prioritised. Strangely enough, bcp doesn't have a command line switch for protocol selection, so this is the only way to manage it I can think of.
P.S. Another option is to specify the server address in the form ip_address,port_number, but I don't think you will find this option particularly attractive.