0

I'm using Python 3.5.1 with Anaconda package 2.4.0 and try to make a connection to local SQL Server (2008 R2)

So doing the next things:

import pypyodbc
connection_string =pypyodbc.connect('Driver={SQL Server};Server=PC\MSSQLSERVER,1433;Database=localbase;Uid=name;Pwd=pass')
connection_string=connection_string.decode(encodind='utf-8',errors='replace')

After all the manipulations, receive the error:

'utf-32-le' codec can't decode bytes in position 0-1: truncated data

Why's that and what should I perform to avoid it and run the connection properly?

Gord Thompson
125k39 gold badges252 silver badges458 bronze badges
asked Jul 11, 2016 at 12:54

2 Answers 2

1
import pymssql
conn = pymssql.connect(server='servername', user='username', 
password='password', database='databasename')
print("Connected to Database")

Use this string connection to connect do database :)

answered Jul 26, 2022 at 9:46
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.
0

You seem to have misunderstood what a "connection string" is. It is the text string that you pass to the .connect method, not what is returned from the .connect method. (What gets returned is a connection object.)

So, you need to do something more like this:

import pypyodbc
connection_string = "DRIVER={SQL Server};SERVER= ...(and so on)..."
conn = pypyodbc.connect(connection_string)
crsr = conn.cursor()
crsr.execute("SELECT stuff FROM tablename")
# etc.
answered Jul 11, 2016 at 19:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.