How do i correctly query a sql ce 4.0 database file using adodbapi?

你离开我真会死。 提交于 2019-12-13 04:52:57

问题


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


来源:https://stackoverflow.com/questions/20021025/how-do-i-correctly-query-a-sql-ce-4-0-database-file-using-adodbapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!