0

Windows XP Pro ActiveState Perl 5.14.4 M$ Access 2010

我有一个 accdb 格式的 Access db,其中包含许多存储的查询。我想使用 Perl 将它们提取到平面文件中。

查询的名称出现在 [MSysObjects].[Name] 中,而查询本身似乎以某种方式存储在 MSysQueries 中。

但我的问题是上游 - 我无法阅读这些表中的任何一个。当我直接打开数据库时,我已经确认用户名是“Admin”。

谷歌搜索出现了http://t1134.codeinpro.us/q/5080f5c04f1eba38a4d1547b,这解释了“管理员”没有对 MSysObjects 的 SELECT 权限,所以我需要执行一个 DDL 语句来 GRANT SELECT ON MSysObjects TO Admin

更多的谷歌搜索出现了http://www.connectionstrings.com/access-2010,它说要启用某些管理语句,如 GRANT 我需要添加 Uid=Admin; 密码=; 扩展AnsiSQL=1;到我的连接字符串

并且同一页面有一个简介,以明确说明工作组文件在连接字符串中位于 SystemDB=C:\mydatabase.mdw 的位置。稍后再详细介绍。

所以我有这个

#! /home/gnu/bin/perl
#

package main;

use strict;
use autouse 'Data::Dumper' => qw(Dumper);
use DBI;
use DBD::ODBC;    # works for both access and sql server
use Win32::ODBC;
use OLE;
use Win32::OLE;

# prototypes
sub _setUpDatabaseHandle_Access_Local_EnableAdminStatements($);
sub setPermissionsOnTables($);
sub readTableMSysObjects($);

#globals
my $debug = 1;

my $targetDbFile  = undef;
my $dbh_C = undef;

print "version of DBD::ODBC   is ".$DBD::ODBC::VERSION."\n";
print "version of Win32::ODBC is ".$Win32::ODBC::VERSION."\n";
print "version of Win32::OLE  is ".$Win32::OLE::VERSION."\n";

$targetDbFile  = "C:/putyer/msaccessdb/here.accdb";

$dbh_C = _setUpDatabaseHandle_Access_Local_EnableAdminStatements( $targetDbFile);

setPermissionsOnTables( $dbh_C);
readTableMSysObjects( $dbh_C);

$dbh_C->disconnect() if( defined( $dbh_C));

exit(0);
#-------------------------------
#  when you use perl to open an .accdb  Access database the Uid is always Admin
#  But user Admin doesn't have SELECT permission on MSysObjects by default you must first
#  grant the permission on the object for the user
#  to enable certain admin commands like CREATE USER, GRANT, REVOKE, DEFAULTS when using CREATE TABLEs
sub _setUpDatabaseHandle_Access_Local_EnableAdminStatements($)
{
  my ( $dbFile) = @_;

  my $errorHit = 0;

  my $conn_str = "DBI:ODBC:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};dbq=".$dbFile;
  $conn_str   .= "; Uid=Admin; Pwd=; ExtendedAnsiSQL=1";

  my $dbh = DBI->connect( $conn_str);
  if( !defined( $dbh)) {
    print "db connection failed - ".$DBI::errstr."\n";
    $errorHit = 1;
  }

  print "connection _setUpDatabaseHandle_Access_Local_EnableAdminStatements failed\n" if( $errorHit);

  return $dbh;
}
#-------------------------------
sub setPermissionsOnTables($)
{
  my ( $dbh) = @_;

  print "entering setPermissionsOnTables\n" if $debug;

  my $errorHit = 0;

  my $q   = undef;
  my $sth = undef;

  unless( $errorHit) {
    $q = "GRANT SELECT ON MSysObjects TO Admin";
    $sth = $dbh->prepare($q);
    if( !defined( $sth)) {
      print "sth failed to define - ".$DBI::errstr."\n";
      $errorHit = 1;
    } else {
      $sth->execute();                        # <===  FAILS HERE
      if( my $errorMsg = $dbh->errstr ) {
        print "select statement failed - ".$errorMsg."\n";
        $errorHit = 1;
      }
    }
  }

  unless( $errorHit) {
    $q = "GRANT SELECT ON MSysQueries TO Admin";
    $sth = $dbh->prepare($q);
    if( !defined( $sth)) {
      print "sth failed to define - ".$DBI::errstr."\n";
      $errorHit = 1;
    } else {
      $sth->execute();
      if( my $errorMsg = $dbh->errstr ) {
        print "select statement failed - ".$errorMsg."\n";
        $errorHit = 1;
      }
    }
  }

  print "exiting  setPermissionsOnTables with errorHit=".$errorHit."\n" if $debug;
  return $errorHit;
}
#-------------------------------
sub readTableMSysObjects($)
{
  my ($dbh) = @_;

  print "entering readTableMSysObjects\n" if $debug;

  my $errorHit = 0;

  my $q   = undef;
  my $sth = undef;

  unless( $errorHit) {
    $q = "SELECT * FROM MSysObjects";  # remember that db nulls come over as perl undefs
    $sth = $dbh->prepare($q);
    if( !defined( $sth)) {
      print "sth failed to define - ".$DBI::errstr."\n";
      $errorHit = 1;
    } else {
      $sth->execute();
      if( my $errorMsg = $dbh->errstr ) {
        print "select statement failed - ".$errorMsg."\n";
        $errorHit = 1;
      } else {
        if( $sth->rows == 0 ) {
          print "select statement returned 0 rows\n";
          $errorHit = 1;
        } else {
          my $row_hashref = undef;
          while( $row_hashref = $sth->fetchrow_hashref() ) {
            print Dumper( $row_hashref);
          }
        }
      }
      $sth->finish();
    }
  }

  print "exiting  readTableMSysObjects\n" if $debug;
  return $errorHit;
}

尝试使用臭名昭著的无法打开 Microsoft Access 数据库引擎工作组信息文件消息执行第一个 GRANT 时失败。

所以看来我也需要这个文件,我似乎在我的盒子上找不到它。或者我可以/应该使用不同的连接吗?喜欢其中之一?

#-------------------------------
sub _setUpDatabaseHandle_Access_Local_ADO_ACE($)
{
  my ( $dbFile) = @_;

  my $errorHit = 0;

  my $conn_str = "DBI:ADO:Provider=Microsoft.ACE.OLEDB.12.0;Data Source=".$dbFile;
  $conn_str   .= "; Persist Security Info=False";
# print $conn_str."\n";

  my $dbh = DBI->connect( $conn_str);
  if( !defined( $dbh)) {
    _pushErrorMsg("db connection failed - ".$DBI::errstr);
    $errorHit = 1;
  }

  print "connection _setUpDatabaseHandle_Access_Local_ACE failed\n" if( $errorHit);

  return $dbh;
}
#-------------------------------
sub _setUpDatabaseHandle_Access_Local_ADO($)
{
  my ( $dbFile) = @_;

  my $errorHit = 0;

  my $conn_str = "DBI:ADO:File Name=".$dbFile;

  my $username = undef;
  my $password = undef;

  my $dbh = DBI->connect( $conn_str, $username, $password);
  if( !defined( $dbh)) {
    _pushErrorMsg("db connection failed - ".$DBI::errstr);
    $errorHit = 1;
  }

  print "connection _setUpDatabaseHandle_Access_Local_ADO failed\n" if( $errorHit);

  return $dbh;
}

欢迎任何指针/提示/建设性批评,尤其是示例。

TIA,

仍在学习的史蒂夫

4

1 回答 1

1

我的经验是,Access 数据库引擎和相关驱动程序似乎可以访问在 ADO 和 OleDb 中被严格锁定的 [MSys...] 表。它们似乎仍然可以通过 DAO 访问,但如果您使用的是 DAO,那么您实际上不需要对 [MSysObjects] 大惊小怪,因为您可以遍历QueryDefs集合。

以下 VBScript 将每个查询的 SQL 代码转储到单独的文本文件中。您可以通过以下方式将其集成到您的 Perl 项目中

  • 将代码移植到 Perl(并使用 Perl 中可用的任何机制来操作 COM 对象),或
  • 从您的 Perl 脚本中取出并运行 VBScript 本身(通过cscript.exe)。
Option Explicit
Dim dbe  ' As DAO.DBEngine
Dim db  ' As DAO.Database
Dim qdf  ' As DAO.QueryDef
Dim fso  ' As FileSystemObject
Dim f  ' As TextStream

Set fso = CreateObject("Scripting.FileSystemObject")  ' New FileSystemObject
Set dbe = CreateObject("DAO.DBEngine.120")  ' New DAO.DBEngine
Set db = dbe.OpenDatabase("C:\__tmp\main.accdb")
For Each qdf In db.QueryDefs
    Set f = fso.CreateTextFile("C:\__tmp\" & qdf.Name & ".sql")
    f.WriteLine qdf.SQL
    f.Close
    Set f = Nothing
Next
db.Close
Set db = Nothing
Set dbe = Nothing
Set fso = Nothing
于 2013-10-25T09:27:30.047 回答