简体   繁体   English

通过 ODBC 将 Excel 连接到 PostgreSQL

[英]Connect Excel to PostgreSQL via ODBC

I am trying to connect to a PostgreSQL database table from Excel via the PostgreSQL ODBC 32-bit driver.我正在尝试通过 PostgreSQL ODBC 32 位驱动程序从 Excel 连接到 PostgreSQL 数据库表。

In Excel, I go to Data>Get Data> From Other Sources> From ODBC.在 Excel 中,我转到数据>获取数据>从其他来源>从 ODBC。 I navigate to the ODBC data source I set up, enter the credentials, and it clearly connects as the available tables appear.我导航到我设置的 ODBC 数据源,输入凭据,并在可用表出现时清楚地连接。 The preview fails and the query fails when I hit "Load" giving the error:当我点击“加载”并给出错误时,预览失败并且查询失败:

DataSource.Error: ODBC: ERROR [HY000] Error while executing the query
Details:
    DataSourceKind=Odbc
    DataSourcePath=dsn=PostgreSQL
    OdbcErrors=Table

错误信息图片

When I test the connection in ODBC admin it is successful.当我在 ODBC admin 中测试连接时,它成功了。 I have tried both the ANSI and Unicode drivers.我已经尝试过 ANSI 和 Unicode 驱动程序。 TIBCO Spotfire connects to the ODBC datasource and pulls the data in just fine. TIBCO Spotfire 连接到 ODBC 数据源并提取数据就好了。

Any help you can provide would be greatly appreciated.您能提供的任何帮助将不胜感激。

This appears to be a bug with the latest psqlODBC driver, which is psqlodbc_09_06_0500 at the time I'm writing this.这似乎是最新的 psqlODBC 驱动程序的一个错误,在我写这篇文章时它是psqlodbc_09_06_0500 I have access to my PostgreSQL server logs.我可以访问我的 PostgreSQL 服务器日志。 Here's the error message and the offending query:这是错误消息和有问题的查询:

ERROR:  syntax error at or near "ta" at character 553
STATEMENT:  select ta.attname, ia.attnum, ic.relname, n.nspname, tc.relname from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class tc, pg_catalog.pg_index i, pg_catalog.pg_namespace n, pg_catalog.pg_class ic where tc.relname = 'rates' AND n.nspname = 'public' AND tc.oid = i.indrelid AND n.oid = tc.relnamespace AND i.indisprimary = 't' AND ia.attrelid = i.indexrelid AND ta.attrelid = i.indrelid AND ta.attnum = i.indkey[ia.attnum-1] AND (NOT ta.attisdropped) AND (NOT ia.attisdropped) AND ic.oid = i.indexrelid order by ia.attnumselect ta.attname, ia.attnum, ic.relname, n.nspname, NULL from pg_catalog.pg_attribute ta, pg_catalog.pg_attribute ia, pg_catalog.pg_class ic, pg_catalog.pg_index i, pg_catalog.pg_namespace n where ic.relname = 'rates_pkey' AND n.nspname = 'public' ANDic.oid = i.indexrelid AND n.oid = ic.relnamespace AND ia.attrelid = i.indexrelid AND ta.attrelid = i.indrelid AND ta.attnum = i.indkey[ia.attnum-1] AND (NOT ta.attisdropped) AND (NOT ia.attisdropped) order by ia.attnum

Here's the context around character 553: order by ia.attnumselect ta.attname, ia.attnum .这是字符 553 的上下文: order by ia.attnumselect ta.attname, ia.attnum Note that it's missing a comma between two field names.请注意,它在两个字段名称之间缺少逗号。

I was able to get it working with psqlodbc_09_06_0200 , which is about a year old.我能够让它与psqlodbc_09_06_0200一起工作,它大约有一年的历史。 Since it sounds like you use 32-bit Office, you can download psqlodbc_09_06_0200-x86.zip from https://www.postgresql.org/ftp/odbc/versions/msi/ .由于听起来您使用的是 32 位 Office,您可以从https://www.postgresql.org/ftp/odbc/versions/msi/下载psqlodbc_09_06_0200-x86.zip (Use x64 if you have 64-bit Office installed.) (如果您安装了 64 位 Office,请使用 x64。)

You might be able to experiment with driver versions between psqlodbc_09_06_0200-x86.zip and psqlodbc_09_06_0500-x86.zip as the bug was presumably introduced somewhere between those two versions.您也许可以尝试psqlodbc_09_06_0200-x86.zippsqlodbc_09_06_0500-x86.zip之间的驱动程序版本,因为该错误可能是在这两个版本之间的某个地方引入的。

You can skip this error by removing the navigate step and querying the information you need throw an SQL statement.您可以通过删除导航步骤并查询您需要抛出 SQL 语句的信息来跳过此错误。

For example if you want to retry some information from LineItemExport the default query is something like this:例如,如果您想重试 LineItemExport 中的某些信息,则默认查询如下所示:

let
    Source = Odbc.DataSource("dsn=name", [HierarchicalNavigation=true]),
    quickbase_Database = Source{[Name="quickbase",Kind="Database"]}[Data],
    public_Schema = quickbase_Database{[Name="public",Kind="Schema"]}[Data],
    LineItemExport = public_Schema{[Name="LineItemExport",Kind="View"]}[Data]
in
    LineItemExport

You have to change the source step in the query editor or change the code to something like this:您必须在查询编辑器中更改源步骤或将代码更改为如下所示:

let
    Source = Odbc.Query("dsn=name", "select * from public.LineItemExport")
in
    Source

This is the way you can came over the error, otherwise you should try to change your drivers version.这是您解决错误的方法,否则您应该尝试更改驱动程序版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM