SQL Server configuration in Lumen - sql-server

I'm trying to set Lumen with SQL Server, but I can not ...
My .env file is:
DB_CONNECTION = sqlsrv
DB_HOST = MYSERVER
DB_DATABASE = MIDB
DB_USERNAME = MIUSER
DB_PASSWORD = MIPASSWORD
Lumen does not throw any errors, but is left wondering, do nothing.
we could explain the correct way to set up for another engine database than not MySQL.
Greetings, thank you very much.

Related

How to make SQL Server backup in Laravel?

I try to do backup to my SQL Server databases in App as its explained in Microsoft.
It's executing, I see the Dumpfile on disk as I have named it and it disappears after a few seconds. And no error appear. I do it by different ways all of its give the same result.
this is the Microsoft link: https://learn.microsoft.com/ar-sa/archive/blogs/brian_swan/backup-and-restore-a-database-with-the-sql-server-driver-for-php
I had run it by routes and controller, and by PHP code and JavaScript, all of them do the same .
This is the PHP code:
$backup_file = "d:\backup\TestDB_Backup.bak";
$sql = "BACKUP DATABASE Mydatabase TO DISK = '".$backup_file."'";
$connectionInfo = array( "Database"=>"Mydatabase","TrustServerCertificate"=>"True", "CharacterSet" => "UTF-8");
$conn=sqlsrv_connect( ".", $connectionInfo);
$stmt = sqlsrv_query($conn, $sql);
SQL Server Version 2017, Laravel V9

Connect python-polars to SQL server (no support currently)

How can I directly connect MS SQL Server to polars?
The documentation does not list any supported connections but recommends the use of pandas.
Update:
SQL Server Authentication works per answer, but Windows domain authentication is not working. see issue
Ahh, actually MsSQL is supported for loading directly into polars (via the underlying library that does the work, which is connectorx); the documentation is just slightly out of date - I'll take a look and refresh it accordingly.
Here you can connect to MS SQL Server with Polar (connectorx under the hood). Just use a connection string:
import polars as pl
# usually don't store sensitive info in plain text
username = 'my_username'
password = '1234'
server = 'SERVER1'
database = 'db1'
trusted_conn = 'no' # or yes
conn = f'mssql://{username}:{password}#{server}/{database}?driver=SQL+Server&trusted_connection={trusted_conn}'
query = "SELECT * FROM table1"
df = pl.read_sql(query, conn)

How to connect sqlalchemy to ms sql database

I have just installed MS SQL server on my system and I am having trouble using it from python. It works fine from the Management Studio, and I can see all my tables. I also have an MySQL server installed and it works fine from python using sqlalchemy. I would like now to use MS SQL as well. Here is what I tried:
from sqlalchemy import create_engine
import pyodbc
server = 'NEW-OFFICE\\NEWOFFICE'
database = 'testdb'
username = 'NEW-OFFICE\user'
password = 'password'
DRIVER = "ODBC Driver 17 for SQL Server"
SERVERNAME = "NEWOFFICE"
INSTANCENAME = "\MSSQLSERVER_ZVI"
First I tried:
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://{username}:{password}#{hostname}/{database}".format(
username=username,
password=password,
hostname=server,
database=database,
)
Second I tried:
engine = create_engine('mssql+pyodbc://' + server + '/' + database + '?trusted_connection=yes&driver={DRIVER}')
Third I tried:
engine = create_engine(
f"mssql+pyodbc://{username}:{password}#{SERVERNAME}{INSTANCENAME}/{database}?driver={DRIVER}", fast_executemany=True
)
Then the next code for all tries is:
db = SQLAlchemy(app)
db.app = app
db.init_app(app)
engine_container = db.get_engine(app)
engine_container.dispose() # to close db connection
db.engine.connect()
hallDB.query.filter_by(client_id = 1).first()
At the last line I get an exception:
(sqlite3.OperationalError) no such table: hall
Of course table "hall" exists and I can query it in the Management Studio.
I am stuck so any help will be mush appreciated.
An Update
The following code did work, but I want the sqlalchemy model approach, which I am not able to use, as listed above.
u = 'DRIVER=ODBC Driver 17 for SQL Server;SERVER=NEW-OFFICE\user;DATABASE=testdb;Trusted_Connection=yes;'
cursor = pyodbc.connect(u).cursor()
cursor.execute("SELECT * FROM hall;")
row = cursor.fetchone()
while row:
print(row[0])
row = cursor.fetchone()
Maybe this will show what needs to be done in order to connect using sqlalchemy.
An Update 2
The error related to sqlite3 was resolved after I saw the following warning
UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:
I realized that although I set SQLALCHEMY_DATABASE_URI, in the test code, I skipped the line app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
After fixing it, and using:
SQLALCHEMY_DATABASE_URI = r'mssql+pyodbc://NEW-OFFICE\user/testdb;driver=ODBC Driver 17 for SQL Server;Trusted_Connection=yes'
or
SQLALCHEMY_DATABASE_URI = r"mssql+pyodbc://NEW-OFFICE\user/testdb?driver=ODBC Driver 17 for SQL Server?trusted_connection=yes"
the error now is:
Message=(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)
And also a warning:
A Warning: No driver name specified; this is expected by PyODBC when using DSN-less connections
Finally after many frustrating hours I found the problem -> the connection staring requires # at the beginning. The proper one is:
mssql+pyodbc://#NEW-OFFICE\user/testdb?driver=ODBC Driver 17 for SQL Server

dynamic configuration strings in SQL server

I am new to SQL Server.
I am trying to connect to a remote SQL Server 2012 through my node js express application in VS 2012 with the following connection string.
var config = "Server={IPv4 add};Database=Dbname;Uid=domain\\username;Pwd=password";
I want to take in the values of username & password dynamically through a HTML form.
I store the entries in variables 'uid' & 'pwd' through app.post() of express and when I do this
var config = '"Server={IPv4 add};Database=Dbname;Uid=domain\\' + uid + ';Pwd=' + pwd+'"';
it throws an error in the console saying
Invalid connection String
and when I do this (removing " ")
'Server={IPv4 add};Database=Dbname;Uid=domain\\' + uid + ';Pwd=' + pwd;
The error is
Untrusted domain and cannot be used with windows authentication
I have been querying MySQL this way all the time. I tried this because I thought SQL Server might work the same way. Please tell me from where can I read about this & make it work.
Thank you
EDIT1 : I am using Windows authentication
I solved this. It was simply because of the asynchronous running, the config statement ran before the credentials were being entered. Silly mistake. Sorry for bothering you

How to Connect Airflow to oracle database

I am trying to create a connection to an oracle db instance (oracle:thin) using Airflow.
According to their documentation I entered my hostname followed by port number and SID:
Host: example.com:1524/sid
filled other fields as:
Conn Type: Oracle
Schema: username ( documentation says: use your username for schema )
Login: username
Password: * * *
After connection is setup, it gives the save error code for every query that I tried to execute (ORA-12514).
It seems like oracle doesn't let airflow to connect:
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Has someone experienced the same problem before. I mean connecting to a database shouldn't be a problem for a big platform like this. Or I am probably doing something wrong. Thanks
Version: Airflow v1.7.0, Oracle11g
EDIT:
I am using the same hostname which I use in Oracle SQLDeveloper client:
After digging into the source code, this is what finally how it worked for me:
Conn Type: Oracle
Host: example.com
schema: username
login: username
port: port number
extra: {"sid": "my sid", "dsn": "example.com"}
You have a problem in your connection settings, either your setting is not loading properly to the oracle hook or you are missing a python package that save/load your connection settings. You can test it by hard coding your credentials.
https://github.com/airbnb/airflow/blob/master/airflow/hooks/oracle_hook.py
conn = self.get_connection(self.oracle_conn_id)
dsn = conn.extra_dejson.get('dsn', None)
sid = conn.extra_dejson.get('sid', None)
service_name = conn.extra_dejson.get('service_name', None)
if dsn and sid and not service_name:
dsn = cx_Oracle.makedsn(dsn, conn.port, sid)
conn = cx_Oracle.connect(conn.login, conn.password, dsn=dsn)
elif dsn and service_name and not sid:
dsn = cx_Oracle.makedsn(dsn, conn.port, service_name=service_name)
conn = cx_Oracle.connect(conn.login, conn.password, dsn=dsn)
else:
conn = cx_Oracle.connect(conn.login, conn.password, conn.host)
for service name usage, if you leave (port, schema and extra) empty, you can put the full oracle connection descriptor under Host:
(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = mysidname)))
this worked for me in extra field
{ "dsn":"192.168.x.x" , "service_name":"some.service.name" }
I get from https://github.com/apache/airflow/blob/master/airflow/hooks/oracle_hook.py#L49
If anyone just does not see the connection in the Ad hoc query dropdown - you need to install the adapter: pip install cx_Oracle on the airflow server.

Resources