This is probably a simple question:
How do I connect to Microsoft SQL Server 2008 R2 from Matlab?
How do I read a table into a matrix given some SQL query?
Update
I'd prefer a method that doesn't require use of manual setup using ODBC.
I present below a review of the different approaches for accessing databases in MATLAB. Here is a list of Stack Overflow questions where some of them were discussed:
How can I access a postgresql database from matlab with without matlabs database toolbox?
connection of MATLAB 7.0 and MYSQL
communicate MATLAB SQL Server
Getting names of Access database tables with Matlab
Invoking ADO.NET from MATLAB
Java
MATLAB have an embedded Java JVM, allowing you to directly call the JDBC drivers from MATLAB. You first need to make them available on the Java classpth in MATLAB:
javaclasspath('sqljdbc4.jar');
%# load driver and create connection
driver = com.microsoft.sqlserver.jdbc.SQLServerDriver;
conn = driver.connect('jdbc:sqlserver://<HOST>:<PORT>;databaseName=<DB>');
%# query database
q = conn.prepareStatement('select * from <TABLE>');
rs = q.executeQuery();
while rs.next()
char(rs.getString(0))
end
rs.close();
conn.close();
Database Toolbox
If you have access to the Database Toolbox, it can simplify the above as it acts as a wrapper around JDBC/ODBC stuff:
conn = database('<DB>', '<USER>','<PASS>', ...
'com.microsoft.sqlserver.jdbc.SQLServerDriver', ...
'jdbc:sqlserver://<HOST>:<PORT>;database=<DB>');
curs = exec(conn, 'select * from <TABLE>');
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
You can also access the database through ODBC. First create a DSN to MSSQL Server (Control Panel > ODBC Data Sources), then use it from the Database Toolbox:
conn = database('myDB', '', ''); %# User/System DSN
%...
close(conn)
COM
You can directly use the ADO OLEDB component from MATLAB. One way is to specify a connection string (DNS-less):
conn = actxserver('ADODB.Connection');
conn.Open('Provider=sqloledb;Data Source=<HOST>;Initial Catalog=<DB>;User Id=<USER>;Password=<PASS>;');
conn.Execute('select * from <TABLE>').GetRows
conn.Close()
.NET
Finally, recent versions of MATLAB added the ability to call .NET from MATLAB. So you can use the ADO.NET data providers:
import System.Data.SqlClient.*
NET.addAssembly('System.Data');
conn = SqlConnection('Data Source=<HOST>;Initial Catalog=<DB>');
conn.Open();
q = SqlCommand('select * from <TABLE>', conn);
r = q.ExecuteReader();
while r.Read()
char(r.GetString(0))
end
r.Close()
conn.Close()
Related
I have been able to read/write from Databricks into SQL Server table using JDBC driver. However this time I have to execute a command before I write to a SQL Server.
I need to execute this command on SQL server: SET IDENTITY_INSERT <sqlserver_table_name> ON
How to do this from Databricks ? Any help/pointers are appreciated. Thanks.
You can't do this with the JDBC Spark Connector (or the SQL Server Spark Connector), but it's trivial when using JDBC directly in Scala or Java. When using JDBC directly you have explicit control of the session, and you can issue multiple batches in the same session, or multiple statements in the same batch. EG
%scala
import java.util.Properties
import java.sql.DriverManager
val jdbcUsername = dbutils.secrets.get(scope = "kv", key = "sqluser")
val jdbcPassword = dbutils.secrets.get(scope = "kv", key = "sqlpassword")
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://xxxxxx.database.windows.net:1433; . . ."
val connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
val stmt = connection.createStatement()
val sql = """
SET IDENTITY_INSERT <sqlserver_table_name> ON
"""
stmt.execute(sql)
//run additional batches here with IDENTITY_INSERT ON
connection.close()
And you can always use the Spark Connector to load a staging table, then use JDBC to run a stored procedure or ad-hoc SQL batch to load the staging data into the target table.
I want to create a connection to a SQL Server Database using a python script that you will see below. In fact, I managed to connect to a single database but I want to make a loop on a database list so that every time I point to a database I can connect directly.
My question is how can I make the name of the database as a variable. Could you help me please?! Thank you in advance
from os import listdir
from os.path import isfile, join
import pyodbc
import pandas as pd
mypath = 'C:\\Users\\DataBaseList'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for i in range (len(onlyfiles)) :
print (onlyfiles[i])
connection = pyodbc.connect("Driver={ODBC Driver 11 for SQL Server};"
"Server=FR0010APP31;"
"Database= Base;"
"uid=*****;pwd=*****")
cursor = connection.cursor()
sql='Select * from Activity'
df= pd.read_sql(sql, connection)
df.to_csv('DatabaseFile')
I don't have much experience with SQL Server, I use it currently to run some simple queries, and I link to SQL tables from Access where I have all my heavy queries. My goal is to run all of my queries in Access daily and then at the end write the finished tables up to SQL where my Access front end will read them (versus keeping them in my Access backend).
I've tried messing around with the code below to try and figure out how to do this, but I'm stuck at the driver and I can't find any references on how to do this with just a single table. Let's call it "PO_STATUS_TBL"
Public Sub ADOtest()
Dim ADOConn As New ADODB.Connection
Dim ADOCom As New ADODB.Command
On Error Resume Next
ADOConn.ConnectionString = "Driver =(SQL Server);DRIVER=SQL Server;SERVER=BUSINESS_BWP;Trusted_Connection=Yes"
ADOConn.Open
Debug.Print ADOConn.State
Dim db As Database
Set db = CurrentDb
'db.Execute "INSERT INTO [ODBC;DRIVER=SQL Server;ENCSQL28\BUSINESS_BWP;DATABASE=CurrentDb].SFTransfersDB ( ID, TO ) SELECT ID,TO FROM SFTransfersDB"
End Sub
I went through this process lately.
For migration you can use this tool by Microsoft. With this tool you can either migrate a table or a query to MS-SQL - or even both. Even with relations.
Simple export with this wizard and add your one table through ODBC Connector in Access. Important is to have the correct ODBC Driver.
If you are interested I resolved this issue with Parfait's advise. By using a simple INSERT statement.
INSERT INTO Dbo_PO_STATUS_ALL_TBL Select * FROM PO_STATUS_ALL_TBL
I am trying to connect to local MS SQL Express Edition. I am using canopy for Python editing.
Code:
import pymssql
conn = pymssql.connect(server='******\SQLEXPRESS',user = 'MEA\*****',password='*****',database='BSEG')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Table')
print(cursor.fetchone())
conn.close()
Error::
pymssql.pyx in pymssql.connect (pymssql.c:10734)()
_mssql.pyx in _mssql.connect (_mssql.c:21821)()
_mssql.pyx in _mssql.MSSQLConnection.init (_mssql.c:5917)()
ValueError: too many values to unpack (expected 2)
user = 'MEA\*****',password='*****'
MEA\***** seems to be Windows login, in this case you shouldn't pass in any password, your user name is enough, but you also should use Integrated security or Trusted parameter in your connection string
It should be smth like this:
server='******\SQLEXPRESS',Trusted_Connection=yes,database='BSEG'
I am using a VB6 application. Which was using the MSAccess database. Now i am changing the database access to sql server. to getting/updating the data from MSAccess the application was using the DAO object. So now i am also trying to connect SQL Server with DAO method. Now i am able to connect the database and also able to get the data. But when i am trying to Edit record it is giving the error "Runtime error '3027' cannot update. Database or object is read only". i am showing my code here:
Public LSWs As Workspace, LSDb As Database
Dim lsConnString As String
Dim l0 As Recordset, SQL0 As String
Dim lehReturn As Integer, retrycount As Integer
lsConnString = "ODBC;DRIVER=SQL Server;SERVER=SERVERName;DATABASE=" & DBname & ";APP=Visual Basic;UID=UID;PWD=PWD"
Set LSWs = DBEngine.Workspaces(0)
Set LSDb = LSWs.OpenDatabase(DBname, dbDriverNoPrompt, True, lsConnString)
retrycount = 0
SQL0 = "select * from schedule_hdr where status = '" & Trim(PCName) & "'"
Set l0 = LSDb.OpenRecordset(SQL0, dbOpenDynaset, dbSeeChanges, adLockPessimistic)
Do While Not (l0.EOF)
LSWs.BeginTrans
l0.Edit
l0!status = "R"
l0.Update
LSWs.CommitTrans
l0.MoveNext
Loop
l0.Close
but it is giving the error at l0.Edit this line code
giving the error
"Runtime error '3027' cannot update. Database or object is read only
Any Help! or suggesion please reply
Thanks in Advance!
Initially I'm thinking that the username and password you are using for the SQL server may possibly have read-only rights.
If this is not the case and you want to stick with DAO then I suggest using the built-in ODBC link feature of Access as this helps you connect successfully to your SQL server.
However, my advice has to be abandoning DAO and going with ADO, mainly because DAO restricts your data processing to client side which is slow, and the whole point of having a dedicated server is to run as much as you can server side to dramatically improve performance.