MS Access 和 MySQL 之间同步数据的最佳方法是什么?

发布于 2024-07-11 23:21:01 字数 384 浏览 5 评论 0原文

我在 Windows 机器上有一个 access 数据库,我必须将其导入到 Linux Web 服务器上的 mysql 中。 目前access dabatbase表导出为文本文件,用ftp自动复制,然后加载到mysql中。

有没有更好的方法来做到这一点,也许使用 ODBC 连接或其他方式?

限制复制已经存在的信息的最佳方法是什么,即仅传输在ac​​cess数据库中但尚未在mysql数据库中的记录。

Access 数据库由另一个程序处理,如果我不必对其进行更改,甚至不必打开它,那就最好了。 不可能需要从 Access 数据库导入更新的记录。 mysql数据库将是主要来源,一些记录将被删除或更改。 我只想导入mysql数据库中从未出现过的记录,而不是恢复那些故意删除的记录。

I have an access database on a windows machine, which I must import into mysql on a linux webserver. At the moment the access dabatbase table is exported as a text file, automatically copied with ftp, and then loaded into mysql.

Is there a better way to do this, perhaps using an ODBC connection or something else?

What is the best way to limit copying information that is already present, ie to only transfer records that are in the access database but not yet in the mysql one.

The access database is handled by another program, and it would be best if I did not have to makes changes to it, or even open it. There is no possibility of needing to import updated records from the access database. The mysql database will be the main source, and some records will be deleted or changed. I only want to import records that were never in the mysql database, not restore the ones that were purposefully deleted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫弦 2024-07-18 23:21:02

为什么不按照 Keltia 的建议通过 ODBC 链接表,然后使用一系列查询来添加丢失的记录并更新更改的记录。 可以通过 VBA 运行查询。 ADO 还可以与 MySQL 和 Access 很好地配合使用。

有关 Access 中使用的代码的一些注释:

   Set db = CurrentDb
   strSQL = "Insert INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
   & "Select AnyField As NameOfMySQLField FROM AnyAccessTable;"

   db.Execute strSQL, dbFailOnError
   End Sub

-- http://forum. lessthandot.com/viewtopic.php?f=95&t=3862

Why not link the tables through ODBC as suggested by Keltia, and then use a series of queries to add records that are missing and to update changed records. The queries can be run through VBA. ADO will also work well with MySQL and Access.

Some notes on code used in Access:

   Set db = CurrentDb
   strSQL = "Insert INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
   & "Select AnyField As NameOfMySQLField FROM AnyAccessTable;"

   db.Execute strSQL, dbFailOnError
   End Sub

-- http://forum.lessthandot.com/viewtopic.php?f=95&t=3862

盛夏尉蓝 2024-07-18 23:21:02

如果您确实需要增量更新,则可以编写一个脚本来连接到两个数据库(至少在 Access 端通过 ODBC)并比较所有表。 复制整个内容的优点是您确保不会忘记任何内容,缺点可能是大小以及 mysql 在重新加载期间不可用的事实。

If you do want incremental updates, the way to do it is writing a script that does connect to both databases (through ODBC at least on the Access side) and compare all tables. The advantage of copying the whole thing is that you are sure not to forget anything, downside may be the size and the fact that the mysql will not be available during the reload.

離人涙 2024-07-18 23:21:02

为了帮助仅传输更改,我建议您向 Access 数据库添加一个名为 RecordChanges 之类的新表。

表结构如下:

RecordChangeID (int) - Primary Key (Autonumber)
TableName (varchar(250)) - Name of table that changed
RecordID (int) - ID of the record in that table that was added / modified
RecordAction (char(1)) - 'A' if add, 'M' if modified or 'D' if deleted

注意 - 通过向其中添加用户 ID 和其他详细信息,您可以获得良好的审计跟踪。

这是痛苦的部分 - 但我会在您的应用程序中创建一个子例程,以便在每次您想要与 MySQL 数据库同步的表中的记录发生更改时向该表添加一条记录。

完成此操作后,我将创建另一个只有一条记录的表,称为 ExportStatus,具有以下结构:

LastRecordChangeID (int) - ID of the last Record Change 
                           you exported in the Record Changes table

然后创建一个子例程来遍历自上次导出以来的所有记录更改(从 ExportStatus 表中检索此记录)并生成 SQL 语句更新您的 MySQL 数据库,完成后请务必更新您的 ExportStatus 表。 您可以删除所有成功导出的 RecordChange 记录,或将它们保留作为审核跟踪。

在实现此操作之前,您需要按照当前的方式进行初始同步。

To help transfer only changes, I suggest you add a new table to your Access database called something like RecordChanges.

The table structure would be as follows:

RecordChangeID (int) - Primary Key (Autonumber)
TableName (varchar(250)) - Name of table that changed
RecordID (int) - ID of the record in that table that was added / modified
RecordAction (char(1)) - 'A' if add, 'M' if modified or 'D' if deleted

Note - by adding a user ID and other details to this you could have a nice audit trail.

This is the painful part - but I would create a subroutine in your application to add a record to this table every time a record is changed in a table you want to synchronize with your MySQL database.

Once this is done I would create another table with only one record, called ExportStatus, with the following structure:

LastRecordChangeID (int) - ID of the last Record Change 
                           you exported in the Record Changes table

Then create a subroutine to go through all Record Changes since the last export (you retrieve this from your ExportStatus table) and generate SQL statements to update your MySQL database, being sure to update your ExportStatus table when done. You could delete all the RecordChange records that were successfully exported, or leave them in as an audit trail.

Before implementing this, you would need to do an initial synchronise the way you are currently doing it.

嘿咻 2024-07-18 23:21:02

我会执行以下操作:

将所有 Access 数据转储到 MySQL 中的临时表中,该表暂时将成为您的“主”数据源。 您应该能够通过设置指向 MySQL 的 ODBC 数据源、右键单击 Access 表并单击“导出”来完成此操作。

  1. 在 MySql 中运行 UPDATE 脚本以
    更新匹配记录
  2. 在 MySql 中运行 INSERT 脚本以
    插入新记录
  3. (可选,取决于这是否
    就是你想要的):运行 DELETE
    MySql中删除记录的脚本
    在新版本中没有找到的
    导入的Access表。
  4. 删除 MySql 中的临时表。

可以通过链接表从 MS Access 内部完成所有这些操作。 但性能不会那么好,并且当/如果您的 MySql 表发生更改时,将需要更改 Access 语句。

这个答案可能对您也有帮助

I would do the following:

Dump all your Access data into a temp table in MySQL, which will be your "master" source of data for a moment. You should be able to do this by setting up an ODBC data source pointed at MySQL, right-clicking on the Access table, and hitting "Export".

  1. Run an UPDATE script in MySql to
    update matching records
  2. Run an INSERT script in MySql to
    insert new records
  3. (Optional, depending on whether this
    is what you want): Run a DELETE
    script in MySql to delete records
    that AREN'T found in the newly
    imported Access table.
  4. Drop the temp table in MySql.

It is possible to do all of this from inside MS Access, via linked tables. But the performance won't be as good, and it will require changing the Access statements when/if your MySql tables change.

This answer may be helpful to you, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文