PDO:如何在Linux*问Microsoft access文件?

时间:2021-01-03 04:35:09

I have managed to read data of Microsoft Access file (.accdb) on Windows using PDO but I'm having problem getting to work on Linux (CentOS). I can see modules is installed:

我已经成功地使用PDO读取了Windows上的Microsoft Access file (.accdb)数据,但在Linux上工作却遇到了问题。我可以看到模块已经安装:

[root@rapid host]# php -m | grep PDO
PDO
PDO_ODBC
[root@rapid host]# php -m | grep odbc
odbc

code:

代码:

<?php
    try{
        $dbhAccess = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=/root/access/data.accdb;Uid=Admin");
    }
    catch(PDOException $e){
        echo $e->getMessage();
        exit();
    }

I get an erro when I execute (CLI) PHP file:

当我执行(CLI) PHP文件时,会得到一个错误:

[root@rapid host]# php access.php
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified

3 个解决方案

#1


2  

you should install the MDB driver.

应该安装MDB驱动程序。

I have no way to try it now, but I think that MDB Tools (specifically the ODBC driver) can do what you' re interested to.

我现在没有办法尝试它,但是我认为MDB工具(特别是ODBC驱动程序)可以做您感兴趣的事情。

#2


1  

I've never worked with a Microsoft Access database but I do regularly connect to DB2 (on IBM AS/400's) and MS SQL servers from Linux (Ubuntu) servers. Your error seems to indicate you don't have a MS Access driver installed - the only one I'm aware of is: http://www.easysoft.com/products/data_access/odbc-access-driver/index.html

我从未使用过Microsoft Access数据库,但我经常从Linux (Ubuntu)服务器连接到DB2(在IBM AS/400上)和MS SQL服务器。您的错误似乎表明您没有安装MS访问驱动程序——我只知道http://www.easysoft.com/products/data_access/odbc-access-driver/index.html

Your error message also says the "Data source name not found" - in order for me to connect to DB2 or MSSQL I have to add some information to /etc/odbc.ini and /etc/odbcinst.ini.

您的错误消息还说“没有找到数据源名称”——为了连接到DB2或MSSQL,我必须向/etc/odbc添加一些信息ini /etc/odbcinst.ini。

/etc/odbcinst.ini - this is where you describe where to find the drivers for ODBC. Here's an example of what I use for DB2 and MSSQL:

/etc/odbcinst.这是您描述如何为ODBC找到驱动程序的地方。下面是我在DB2和MSSQL中使用的示例:

[iseries]
Description     = iSeries Access for Linux ODBC Driver
Driver          = /usr/lib/libcwbodbc.so
Setup           = /usr/lib/libcwbodbcs.so
NOTE1           = If using unixODBC 2.2.11 or later and you want the 32 and 64-bit ODBC drivers to share DSN's,
NOTE2           = the following Driver64/Setup64 keywords will provide that support.
Driver64        = /usr/lib/lib64/libcwbodbc.so
Setup64         = /usr/lib/lib64/libcwbodbcs.so
Threading       = 2
DontDLClose     = 1
UsageCount      = 1

# Define where to find the driver for the Free TDS connections.
[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount      = 1

[ODBC]
Trace           = no
TraceFile       = /tmp/odbc.log

In that last section ([ODBC]) I currently have Trace = no - if you change that to Trace = yes you will get some helpful debugging information in the /tmp/odbc.log file.

在上一节([ODBC])中,我当前有Trace = no—如果将其更改为Trace = yes,您将在/tmp/ ODBC中获得一些有用的调试信息。日志文件。

/etc/odbc.ini - this is where you define your data sources. Here's an example of what I use for DB2 and MSSQL:

/etc/odbc.ini——这是您定义数据源的地方。下面是我在DB2和MSSQL中使用的示例:

[primary]
Description             = primary
Driver                  = iseries
System                  = XXX.XXX.XXX.XXX
UserID                  = XXXXXXXXXX
Password                = XXXXXXXXXX
Naming                  = 0
DefaultLibraries        = QGPL
Database                = MYLIB
ConnectionType          = 0
CommitMode              = 2
ExtendedDynamic         = 0
DefaultPkgLibrary       = QGPL
DefaultPackage          = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression    = 1
LibraryView             = 0
AllowUnsupportedChar    = 0
ForceTranslation        = 0
Trace                   = 0

# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description             = MSSQL Server
Driver                  = freetds
Database                = MyDatabase
ServerName              = mssql
TDS_Version             = 8.0

I've seen a number of questions here on * talking about using MSFT Access databases from a Linux machine - and there doesn't seem to ever be a happy ending. If there is any way you can port the data to a different, better supported, database system (like MySQL) I think you'll save yourself some headaches. Good luck!

我在*网站上看到过很多关于使用Linux机器上的MSFT访问数据库的问题——似乎从来没有一个圆满的结局。如果有任何方法可以将数据移植到另一个更好支持的数据库系统(如MySQL),我认为您可以省去一些麻烦。好运!

#3


1  

You can not use {Microsoft Access Driver (*.mdb, *.accdb)} as part of your data source because Microsoft do not make an MS Access ODBC driver for Linux. There are 2 ODBC drivers for MS Access as far as I know of. The MDB Tools and the Easysoft ODBC-Access Driver.

不能使用{Microsoft Access Driver(*)。作为数据源的一部分,因为微软没有为Linux提供访问ODBC驱动程序。据我所知,有两个用于MS访问的ODBC驱动程序。MDB工具和Easysoft odbc访问驱动程序。

If you install either driver you can either use a DSN which you setup in your odbc.ini file or as you prefer a DSN-Less connection. Here is an example of the an Easysoft DSN-Less connection to an MS Access database

如果您安装了驱动程序,您可以使用您在odbc中设置的DSN。ini文件或您更喜欢无dsn连接。下面是一个例子,说明了与MS访问数据库的无Easysoft dsn连接

PDO("odbc:Driver={Driver=Easysoft ODBC-ACCESS};Dbq=/root/access/data.accdb");

PDO(“odbc:司机= {司机= Easysoft ODBC-ACCESS };Dbq = /根/访问/ data.accdb”);

More information on connecting and getting data back using PDO-ODBC can be found on Easysoft PHP guide There is a section about half way down that talks about PDO-ODBC.

关于使用PDO-ODBC连接和获取数据的更多信息可以在Easysoft PHP指南上找到。

#1


2  

you should install the MDB driver.

应该安装MDB驱动程序。

I have no way to try it now, but I think that MDB Tools (specifically the ODBC driver) can do what you' re interested to.

我现在没有办法尝试它,但是我认为MDB工具(特别是ODBC驱动程序)可以做您感兴趣的事情。

#2


1  

I've never worked with a Microsoft Access database but I do regularly connect to DB2 (on IBM AS/400's) and MS SQL servers from Linux (Ubuntu) servers. Your error seems to indicate you don't have a MS Access driver installed - the only one I'm aware of is: http://www.easysoft.com/products/data_access/odbc-access-driver/index.html

我从未使用过Microsoft Access数据库,但我经常从Linux (Ubuntu)服务器连接到DB2(在IBM AS/400上)和MS SQL服务器。您的错误似乎表明您没有安装MS访问驱动程序——我只知道http://www.easysoft.com/products/data_access/odbc-access-driver/index.html

Your error message also says the "Data source name not found" - in order for me to connect to DB2 or MSSQL I have to add some information to /etc/odbc.ini and /etc/odbcinst.ini.

您的错误消息还说“没有找到数据源名称”——为了连接到DB2或MSSQL,我必须向/etc/odbc添加一些信息ini /etc/odbcinst.ini。

/etc/odbcinst.ini - this is where you describe where to find the drivers for ODBC. Here's an example of what I use for DB2 and MSSQL:

/etc/odbcinst.这是您描述如何为ODBC找到驱动程序的地方。下面是我在DB2和MSSQL中使用的示例:

[iseries]
Description     = iSeries Access for Linux ODBC Driver
Driver          = /usr/lib/libcwbodbc.so
Setup           = /usr/lib/libcwbodbcs.so
NOTE1           = If using unixODBC 2.2.11 or later and you want the 32 and 64-bit ODBC drivers to share DSN's,
NOTE2           = the following Driver64/Setup64 keywords will provide that support.
Driver64        = /usr/lib/lib64/libcwbodbc.so
Setup64         = /usr/lib/lib64/libcwbodbcs.so
Threading       = 2
DontDLClose     = 1
UsageCount      = 1

# Define where to find the driver for the Free TDS connections.
[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount      = 1

[ODBC]
Trace           = no
TraceFile       = /tmp/odbc.log

In that last section ([ODBC]) I currently have Trace = no - if you change that to Trace = yes you will get some helpful debugging information in the /tmp/odbc.log file.

在上一节([ODBC])中,我当前有Trace = no—如果将其更改为Trace = yes,您将在/tmp/ ODBC中获得一些有用的调试信息。日志文件。

/etc/odbc.ini - this is where you define your data sources. Here's an example of what I use for DB2 and MSSQL:

/etc/odbc.ini——这是您定义数据源的地方。下面是我在DB2和MSSQL中使用的示例:

[primary]
Description             = primary
Driver                  = iseries
System                  = XXX.XXX.XXX.XXX
UserID                  = XXXXXXXXXX
Password                = XXXXXXXXXX
Naming                  = 0
DefaultLibraries        = QGPL
Database                = MYLIB
ConnectionType          = 0
CommitMode              = 2
ExtendedDynamic         = 0
DefaultPkgLibrary       = QGPL
DefaultPackage          = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression    = 1
LibraryView             = 0
AllowUnsupportedChar    = 0
ForceTranslation        = 0
Trace                   = 0

# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description             = MSSQL Server
Driver                  = freetds
Database                = MyDatabase
ServerName              = mssql
TDS_Version             = 8.0

I've seen a number of questions here on * talking about using MSFT Access databases from a Linux machine - and there doesn't seem to ever be a happy ending. If there is any way you can port the data to a different, better supported, database system (like MySQL) I think you'll save yourself some headaches. Good luck!

我在*网站上看到过很多关于使用Linux机器上的MSFT访问数据库的问题——似乎从来没有一个圆满的结局。如果有任何方法可以将数据移植到另一个更好支持的数据库系统(如MySQL),我认为您可以省去一些麻烦。好运!

#3


1  

You can not use {Microsoft Access Driver (*.mdb, *.accdb)} as part of your data source because Microsoft do not make an MS Access ODBC driver for Linux. There are 2 ODBC drivers for MS Access as far as I know of. The MDB Tools and the Easysoft ODBC-Access Driver.

不能使用{Microsoft Access Driver(*)。作为数据源的一部分,因为微软没有为Linux提供访问ODBC驱动程序。据我所知,有两个用于MS访问的ODBC驱动程序。MDB工具和Easysoft odbc访问驱动程序。

If you install either driver you can either use a DSN which you setup in your odbc.ini file or as you prefer a DSN-Less connection. Here is an example of the an Easysoft DSN-Less connection to an MS Access database

如果您安装了驱动程序,您可以使用您在odbc中设置的DSN。ini文件或您更喜欢无dsn连接。下面是一个例子,说明了与MS访问数据库的无Easysoft dsn连接

PDO("odbc:Driver={Driver=Easysoft ODBC-ACCESS};Dbq=/root/access/data.accdb");

PDO(“odbc:司机= {司机= Easysoft ODBC-ACCESS };Dbq = /根/访问/ data.accdb”);

More information on connecting and getting data back using PDO-ODBC can be found on Easysoft PHP guide There is a section about half way down that talks about PDO-ODBC.

关于使用PDO-ODBC连接和获取数据的更多信息可以在Easysoft PHP指南上找到。