title: pyodbc can't connect to database
body: <p>I'm using <code>pyodbc</code> library from <a href="https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Windows" rel="nofollow noreferrer">here</a> and I'm connecting this way:</p>
  
  <pre><code>conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};Server=(localdb)\MSSQLLocalDB;Integrated Security=true; database = online_banking; autocommit = True')
  </code></pre>
  
  <p>I use <code>MSSQLLocalDB</code>because it's the default instance name for SQL Server 2014. And this last version of Python 2.7.</p>
  
  <p>However I cant run any simple query, every if them raise the error, saying that there is no such object or in that particular case database:</p>
  
  <pre><code>cursor.execute('use online_banking;')
  </code></pre>
  
  <p>The full error:</p>
  
  <pre><code>pyodbc.Error: ('08004', "[08004] [Microsoft][SQL Server Native Client 11.0][SQL Server]Database 'online_banking' does not exist. Make sure that the name is entered correctly. (911) (SQLExecDirectW)")
  </code></pre>
  
  <p>So what is wrong here?</p>
  
  <p>There is only 1 instance installed and such <code>databases(.mdf)</code>
  <a href="https://i.stack.imgur.com/7utvc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7utvc.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/HLEpu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HLEpu.png" alt="enter image description here"></a></p>
  
  <p>As you can see only 1 engine:</p>
  
  <p><a href="https://i.stack.imgur.com/eBN7f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eBN7f.png" alt="enter image description here"></a></p>
  
  <p>Selecting that engine will allow me to see <code>online_banking</code> db
  <a href="https://i.stack.imgur.com/RBlH4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RBlH4.png" alt="enter image description here"></a></p>
  
  <p><strong>upd1</strong> Database've been created this way:</p>
  
  <pre><code>CREATE DATABASE [online_banking]
   ON  PRIMARY 
  ( NAME = N'online_banking', FILENAME = N'C:\...\online_banking.mdf' , 
    SIZE = 512000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 30%)
   LOG ON 
  ( NAME = N'online_banking_log', FILENAME = N'C:\...\online_banking_log.ldf' , 
    SIZE = 1024KB , MAXSIZE = 20GB , FILEGROWTH = 10%)
  GO
  </code></pre>
  
  <p><strong>upd2</strong> I've used built-in tool <code>sqlcmd</code>. 
  So this <code>sqlcmd  -S (LocalDB)\MSSQLLocalDB -i C:\Users\1.sql -E</code> have shown, that 
  <code>MSSQLLocalDB</code> doesn't have my database.</p>
  
  <p>However <code>sqlcmd  -S localhost -i C:\Users\1.sql -E</code> performed successfully.
  I'm totally confused, I' ve installed only one server, moreover SQL Management studio sees only one local server with my <code>online_banking</code> DB. This is look really weird to me.</p>
  
  <p>Trying to use this connection string in Python </p>
  
  <pre><code>conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};Server=localhost;Integrated Security=true; database = online_banking; autocommit = True')
  </code></pre>
  
  <p>causes the error below:</p>
  
  <pre><code>pyodbc.Error: ('28000', '[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]\x... "". (18456) (SQLDriverConnect); [01S00] [Microsoft][SQL Server Native Client 11.0]\xcd\xe5\xe....xe8\xff (0); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]\xce...ff "". (18456); [01S00] [Microsoft][SQL Server Native Client 11.0]\xcd\xe.... (0)'
  </code></pre>
  
  <p><strong>upd3:</strong> Specified mdf should be attached, got it:
  Tried several ways, always errors (with database specified or not in connection string): </p>
  
  <pre><code>conn = pyodbc.connect( 
  
         r'Driver={SQL Server Native Client 11.0};Server=(localdb)\MSSQLLocalDB; database =online_banking; AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL12.SQLSERVERINSAF\MSSQL\DATA\online_banking.mdf;Trusted_Connection=Yes; Integrated Security=true; database = online_banking;')
  
  error: A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
  </code></pre>
  
  <p>I found out, that may be related with parent server which already have attached this db, but failed to solve this.</p>
  
  <p><strong>upd4</strong>
  I tried simple code from <a href="http://pastebin.com/nJr4Pdge" rel="nofollow noreferrer">here</a>  to see if "online_banking" shows up in the list of databases for that instance. But faced another error:</p>
  
  <blockquote>
    <p>pyodbc.Error: ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]\ - unreadable error </p>
  </blockquote>
  
  <p>In addition that database <a href="https://i.stack.imgur.com/VCm6j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VCm6j.png" alt="enter image description here"></a> according to SSMS seems have already attached by <code>online_banking</code>  DB</p>