How to open a SQL Server .mdf file with Python (pandas)










1















I'm trying to open a mdf sql database file that I have saved to my desktop. How do you open it as a pandas dataframe? so far all I have is the following:



conn=pyodbc.connect(driver='SQL Server', dsn=filepath)


Its giving me the error message



OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')



I found another question that was similar but it was also unanswered. I have also been unable to find a good tutorial to start using sql databases with python I'm very new to the topic. Let me know if there is any extra information I can give. Thanks in advance.










share|improve this question
























  • check this rebasedata.com/python-read-mdf

    – Abdulrahman Bres
    Nov 14 '18 at 20:15











  • I cant install poster on python 3 is there another option?

    – Matthew
    Nov 14 '18 at 21:03











  • Would this work? stackoverflow.com/questions/51734803/…

    – Evan
    Nov 14 '18 at 21:44











  • I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

    – Matthew
    Nov 15 '18 at 13:12















1















I'm trying to open a mdf sql database file that I have saved to my desktop. How do you open it as a pandas dataframe? so far all I have is the following:



conn=pyodbc.connect(driver='SQL Server', dsn=filepath)


Its giving me the error message



OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')



I found another question that was similar but it was also unanswered. I have also been unable to find a good tutorial to start using sql databases with python I'm very new to the topic. Let me know if there is any extra information I can give. Thanks in advance.










share|improve this question
























  • check this rebasedata.com/python-read-mdf

    – Abdulrahman Bres
    Nov 14 '18 at 20:15











  • I cant install poster on python 3 is there another option?

    – Matthew
    Nov 14 '18 at 21:03











  • Would this work? stackoverflow.com/questions/51734803/…

    – Evan
    Nov 14 '18 at 21:44











  • I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

    – Matthew
    Nov 15 '18 at 13:12













1












1








1








I'm trying to open a mdf sql database file that I have saved to my desktop. How do you open it as a pandas dataframe? so far all I have is the following:



conn=pyodbc.connect(driver='SQL Server', dsn=filepath)


Its giving me the error message



OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')



I found another question that was similar but it was also unanswered. I have also been unable to find a good tutorial to start using sql databases with python I'm very new to the topic. Let me know if there is any extra information I can give. Thanks in advance.










share|improve this question
















I'm trying to open a mdf sql database file that I have saved to my desktop. How do you open it as a pandas dataframe? so far all I have is the following:



conn=pyodbc.connect(driver='SQL Server', dsn=filepath)


Its giving me the error message



OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect)')



I found another question that was similar but it was also unanswered. I have also been unable to find a good tutorial to start using sql databases with python I'm very new to the topic. Let me know if there is any extra information I can give. Thanks in advance.







python sql pandas pyodbc mdf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:57









Gord Thompson

77.6k1493218




77.6k1493218










asked Nov 14 '18 at 19:32









MatthewMatthew

394




394












  • check this rebasedata.com/python-read-mdf

    – Abdulrahman Bres
    Nov 14 '18 at 20:15











  • I cant install poster on python 3 is there another option?

    – Matthew
    Nov 14 '18 at 21:03











  • Would this work? stackoverflow.com/questions/51734803/…

    – Evan
    Nov 14 '18 at 21:44











  • I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

    – Matthew
    Nov 15 '18 at 13:12

















  • check this rebasedata.com/python-read-mdf

    – Abdulrahman Bres
    Nov 14 '18 at 20:15











  • I cant install poster on python 3 is there another option?

    – Matthew
    Nov 14 '18 at 21:03











  • Would this work? stackoverflow.com/questions/51734803/…

    – Evan
    Nov 14 '18 at 21:44











  • I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

    – Matthew
    Nov 15 '18 at 13:12
















check this rebasedata.com/python-read-mdf

– Abdulrahman Bres
Nov 14 '18 at 20:15





check this rebasedata.com/python-read-mdf

– Abdulrahman Bres
Nov 14 '18 at 20:15













I cant install poster on python 3 is there another option?

– Matthew
Nov 14 '18 at 21:03





I cant install poster on python 3 is there another option?

– Matthew
Nov 14 '18 at 21:03













Would this work? stackoverflow.com/questions/51734803/…

– Evan
Nov 14 '18 at 21:44





Would this work? stackoverflow.com/questions/51734803/…

– Evan
Nov 14 '18 at 21:44













I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

– Matthew
Nov 15 '18 at 13:12





I found that one earlier as well but I don't get the server part. I have a mdf file on my desktop is there no way to just open that file in python. Do I have to create a local server somehow to read the mdf file?

– Matthew
Nov 15 '18 at 13:12












1 Answer
1






active

oldest

votes


















0















I have a mdf file on my desktop is there no way to just open that file in python.




Well, yes, you could open it as a binary file but then you'd need to write the code to interpret the contents of the file. In other words, you would need to reverse-engineer the logic that SQL Server uses to write database objects to the .mdf file.



It would probably be easier for you to just install SQL Server Express Edition, attach the .mdf file, and then access the database as usual.



Or, instead of manually attaching the .mdf file to the SQL Server instance you could use code like this:



import pandas as pd
import pyodbc

cnxn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)SQLEXPRESS;'
r'Trusted_Connection=yes;'
r'AttachDbFileName=C:UsersGordDesktopzzz.mdf;'
)
cnxn = pyodbc.connect(cnxn_str)
df = pd.read_sql("SELECT * FROM Table1", cnxn)





share|improve this answer

























  • Thanks I think I'm starting to understand how this works.

    – Matthew
    Nov 15 '18 at 15:29











  • Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

    – Matthew
    Nov 15 '18 at 15:41











  • You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

    – Gord Thompson
    Nov 15 '18 at 15:50











  • Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

    – Matthew
    Nov 15 '18 at 16:02











  • Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

    – Gord Thompson
    Nov 15 '18 at 16:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307559%2fhow-to-open-a-sql-server-mdf-file-with-python-pandas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















I have a mdf file on my desktop is there no way to just open that file in python.




Well, yes, you could open it as a binary file but then you'd need to write the code to interpret the contents of the file. In other words, you would need to reverse-engineer the logic that SQL Server uses to write database objects to the .mdf file.



It would probably be easier for you to just install SQL Server Express Edition, attach the .mdf file, and then access the database as usual.



Or, instead of manually attaching the .mdf file to the SQL Server instance you could use code like this:



import pandas as pd
import pyodbc

cnxn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)SQLEXPRESS;'
r'Trusted_Connection=yes;'
r'AttachDbFileName=C:UsersGordDesktopzzz.mdf;'
)
cnxn = pyodbc.connect(cnxn_str)
df = pd.read_sql("SELECT * FROM Table1", cnxn)





share|improve this answer

























  • Thanks I think I'm starting to understand how this works.

    – Matthew
    Nov 15 '18 at 15:29











  • Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

    – Matthew
    Nov 15 '18 at 15:41











  • You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

    – Gord Thompson
    Nov 15 '18 at 15:50











  • Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

    – Matthew
    Nov 15 '18 at 16:02











  • Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

    – Gord Thompson
    Nov 15 '18 at 16:15















0















I have a mdf file on my desktop is there no way to just open that file in python.




Well, yes, you could open it as a binary file but then you'd need to write the code to interpret the contents of the file. In other words, you would need to reverse-engineer the logic that SQL Server uses to write database objects to the .mdf file.



It would probably be easier for you to just install SQL Server Express Edition, attach the .mdf file, and then access the database as usual.



Or, instead of manually attaching the .mdf file to the SQL Server instance you could use code like this:



import pandas as pd
import pyodbc

cnxn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)SQLEXPRESS;'
r'Trusted_Connection=yes;'
r'AttachDbFileName=C:UsersGordDesktopzzz.mdf;'
)
cnxn = pyodbc.connect(cnxn_str)
df = pd.read_sql("SELECT * FROM Table1", cnxn)





share|improve this answer

























  • Thanks I think I'm starting to understand how this works.

    – Matthew
    Nov 15 '18 at 15:29











  • Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

    – Matthew
    Nov 15 '18 at 15:41











  • You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

    – Gord Thompson
    Nov 15 '18 at 15:50











  • Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

    – Matthew
    Nov 15 '18 at 16:02











  • Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

    – Gord Thompson
    Nov 15 '18 at 16:15













0












0








0








I have a mdf file on my desktop is there no way to just open that file in python.




Well, yes, you could open it as a binary file but then you'd need to write the code to interpret the contents of the file. In other words, you would need to reverse-engineer the logic that SQL Server uses to write database objects to the .mdf file.



It would probably be easier for you to just install SQL Server Express Edition, attach the .mdf file, and then access the database as usual.



Or, instead of manually attaching the .mdf file to the SQL Server instance you could use code like this:



import pandas as pd
import pyodbc

cnxn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)SQLEXPRESS;'
r'Trusted_Connection=yes;'
r'AttachDbFileName=C:UsersGordDesktopzzz.mdf;'
)
cnxn = pyodbc.connect(cnxn_str)
df = pd.read_sql("SELECT * FROM Table1", cnxn)





share|improve this answer
















I have a mdf file on my desktop is there no way to just open that file in python.




Well, yes, you could open it as a binary file but then you'd need to write the code to interpret the contents of the file. In other words, you would need to reverse-engineer the logic that SQL Server uses to write database objects to the .mdf file.



It would probably be easier for you to just install SQL Server Express Edition, attach the .mdf file, and then access the database as usual.



Or, instead of manually attaching the .mdf file to the SQL Server instance you could use code like this:



import pandas as pd
import pyodbc

cnxn_str = (
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=(local)SQLEXPRESS;'
r'Trusted_Connection=yes;'
r'AttachDbFileName=C:UsersGordDesktopzzz.mdf;'
)
cnxn = pyodbc.connect(cnxn_str)
df = pd.read_sql("SELECT * FROM Table1", cnxn)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:30

























answered Nov 15 '18 at 14:20









Gord ThompsonGord Thompson

77.6k1493218




77.6k1493218












  • Thanks I think I'm starting to understand how this works.

    – Matthew
    Nov 15 '18 at 15:29











  • Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

    – Matthew
    Nov 15 '18 at 15:41











  • You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

    – Gord Thompson
    Nov 15 '18 at 15:50











  • Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

    – Matthew
    Nov 15 '18 at 16:02











  • Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

    – Gord Thompson
    Nov 15 '18 at 16:15

















  • Thanks I think I'm starting to understand how this works.

    – Matthew
    Nov 15 '18 at 15:29











  • Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

    – Matthew
    Nov 15 '18 at 15:41











  • You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

    – Gord Thompson
    Nov 15 '18 at 15:50











  • Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

    – Matthew
    Nov 15 '18 at 16:02











  • Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

    – Gord Thompson
    Nov 15 '18 at 16:15
















Thanks I think I'm starting to understand how this works.

– Matthew
Nov 15 '18 at 15:29





Thanks I think I'm starting to understand how this works.

– Matthew
Nov 15 '18 at 15:29













Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

– Matthew
Nov 15 '18 at 15:41





Using this code im now getting this error: InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

– Matthew
Nov 15 '18 at 15:41













You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

– Gord Thompson
Nov 15 '18 at 15:50





You may have a different version of the ODBC driver installed. Check the list returned by pyodbc.drivers() to see which drivers are available to your Python app.

– Gord Thompson
Nov 15 '18 at 15:50













Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

– Matthew
Nov 15 '18 at 16:02





Thanks that fixed the driver error. In order to run this code do I need to have an instance of sql express running?

– Matthew
Nov 15 '18 at 16:02













Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

– Gord Thompson
Nov 15 '18 at 16:15





Yes, an instance of SQL Server will have to be running in order for your Python app to communicate with it. It could be a normal SQL Server Express instance running as a service, or it could be a LocalDB instance that "spins up" when you start to use it and shuts down when you're finished.

– Gord Thompson
Nov 15 '18 at 16:15



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307559%2fhow-to-open-a-sql-server-mdf-file-with-python-pandas%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric