【问题标题】:Pyodbc with BigQuery带有 BigQuery 的 Pyodbc
【发布时间】:2018-09-14 16:59:41
【问题描述】:

我正在尝试让 Pyodbc 与 Google BigQuery 一起使用。 我安装的 ODBC 管理器是 unixodbc (ubuntu) Simba 驱动程序的配置应该没问题,因为 SQL 命令有效,我可以从那里执行查询。

但是,当我使用 Pyodbc 时,我卡住了。 这是代码:

import pyodbc

dbname = 'testdb'
driver_str = '{Simba ODBC Driver for Google BigQuery 64-bit}'
cnxn = pyodbc.connect(driver=driver_str, database=dbname)

c = conn.cursor()
c.execute('SELECT * FROM tablename')
print(c.fetchone())

它会产生以下错误:

Traceback (most recent call last):
  File "/home/virus/work/lutech/wind/usecase3/test_odbc.py", line 48, in <module>
    cnxn = pyodbc.connect(driver=driver_str, database=dbname)
pyodbc.OperationalError: ('08001', '[08001] [unixODBC][Simba][DSI] An error occurred while attempting to retrieve the error message for key \'UnableToEstConn\' and component ID 1: Could not open error message files - Check that "/home/virus/work/lutech/wind/simba/googlebigqueryodbc/lib/64/$(INSTALLDIR)/ErrorMessages/en-US/ODBCMessages.xml" or "/home/virus/work/lutech/wind/simba/googlebigqueryodbc/lib/64/$(INSTALLDIR)/ErrorMessages/ODBCMessages_en-US.xml" exists and are accessible. MessageParameters=["{[Catalog] [OAuthMechanism]}"] (-1) (SQLDriverConnect)')

我不明白这是什么意思,但它指的是 Simba Error 文件夹中的一个文件。

有什么帮助吗?

【问题讨论】:

    标签: google-bigquery odbc pyodbc unixodbc


    【解决方案1】:

    我通过非常密集的尝试和错误方法解决了问题。现在很清楚了。 我使用本地用户配置文件,以避免权限问题。 /etc/ 中的那些是空的。

    这是我的 .odbcinst.ini 文件的内容:

    $ cat .odbcinst.ini 
    
    [ODBC Drivers]
    Simba ODBC Driver for Google BigQuery 64-bit=Installed
    [Simba ODBC Driver for Google BigQuery 64-bit]
    Description=Simba ODBC Driver for Google BigQuery (64-bit)
    Driver=<local user installation path>/simba/googlebigqueryodbc/lib/64/libgooglebigqueryodbc_sb64.so
    

    这里是我的 .odbc.ini:

    $ cat .odbc.ini 
    
    [bigquery_odbc]
    Driver=Simba ODBC Driver for Google BigQuery 64-bit
    Catalog=<gcp project id>
    OAuthMechanism=0
    Email= <email service account>
    KeyFilePath=<path to the json file downloaded when creating the service account>
    

    这里应该可以成功执行 isql -v bigquery_odbc

    现在,如果我尝试使用 pyodbc 进行连接,则会收到上述错误。 首先修复配置文件中表示的安装路径以及指定的UTF编码here

    $ cat <local user installation path>/simba/googlebigqueryodbc/lib/64/simba.googlebigqueryodbc.ini
    
    # To use this INI file, replace $(INSTALLDIR) with the
    # directory the tarball was extracted to.
    
    [Driver]
    DriverManagerEncoding=UTF-16
    
    ErrorMessagesPath=<local user installation path>simba/googlebigqueryodbc/ErrorMessages
    LogLevel=0
    LogPath=<log path>
    LogFileCount=5
    LogFileSize=10
    

    调用pyodbc时,它起作用了:

    dataset_name = <bigquery dataset name>
    DSN = 'bigquery_odbc'
    conn_str = "DSN={}".format(DSN)
    cnxn = pyodbc.connect(conn_str, autocommit=True) # DO NOT forget autocommit param
    cursor = cnxn.cursor()
    cursor.execute('select * from {}.table;'.format(dataset_name))
    print(cursor.fetchone())
    

    我在这个配置上遇到了很多困难。希望对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多