-->

How do i correctly query a sql ce 4.0 database fil

2019-08-24 02:11发布

问题:

I have the following method:

def open(self, filename):
    if not os.path.exists(filename):
        raise IOError("Cannot find Game Database file {0}").__format__(filename)

    connstr = "Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0; 
               Data Source={0};".format(filename)
    conn = adodbapi.connect(connstr)
    curs = conn.cursor()
    query = "Select * from Patient;"
    curs.execute(query)
    results = curs.fetchall()
    for r in results:
        print r

When this runs, the following error is rased on curs.execute(query):

(<class 'adodbapi.adodbapi.DatabaseError'>, u"(-2147352567, 'Exception occurred.', (0, u'Microsoft Cursor Engine', u'Multiple-step operation generated errors. Check each status value.', None, 0, -2147217887), None)\nCommand:\nSelect * from Patient;\nParameters:\n[]")

I can run this exact query in compactView successfully.

What obvious syntactic sugar am i not seeing? ( running: win7 pro x64, python 2.7.x, pywin32 and adodbapi installed successfully. The connection string seems to work -- i can connect and get a cursor just fine)

回答1:

Using adodbapi-2.6.0.7 I solved this using the following:

connection.connector.CursorLocation = 2

For example:

import adodbapi

file = r'FILEPATH'
connection_string = (
    'Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5; Data Source={};'.format(file))
print(connection_string)
connection = adodbapi.connect(connection_string)
connection.connector.CursorLocation = 2
connection.get_table_names()


回答2:

It could be that one of the table columns you are selecting is of type NVARCHAR(N) where N is larger than 127 [1]. Using the server-side cursor will solve the problem.

conn.adoConn.CursorLocation = adodbapi.adUseServer