简体   繁体   English

我正在尝试通过ODBC将Sage Line 50 Access数据库与Mysql同步

[英]I am trying to synchronise a Sage Line 50 Access database with Mysql via ODBC

I am trying to source the structure and data from a sage line 50 database but am having trouble with my update/create script. 我试图从Sage第50行数据库中获取结构和数据,但是我的更新/创建脚本遇到了麻烦。

Basically, I am writing a tool so that the local intranet site can display data sourced from sage to normal employees without using up a sage login (orders due in/stock levels etc). 基本上,我正在写一个工具,以便本地Intranet站点可以显示从贤哲到普通员工的数据,而无需占用贤哲的登录信息(由于库存/库存水平等导致的订单)。 I am having trouble with this because it seems that the Sage50 database was developed by total morons. 我对此感到麻烦,因为似乎Sage50数据库是由白痴开发的。 There are no Unique keys in this database, or, more accurately, very very few. 此数据库中没有唯一键,或更准确地说,很少。 The structure is really old school you can find the structure on pastebin HERE (bit too big for here). 该结构的确是老派,您可以在pastebin上找到该结构( 此处太大了)。 You'll notice that there are some tables with 300+ columns, which I think is a bit stupid. 您会注意到有些表包含300多个列,我认为这有点愚蠢。 However, I have to work with this and so I need a solution. 但是,我必须处理这个问题,因此我需要一个解决方案。

There are a few problems syncing that I have encountered. 我遇到了一些同步问题。 Primarily it's the fact ODBC can't limit statements to 1 row so I can check data type, and secondly, with there being no IDs, I can't check if it's a duplicate when doing the insert. 首先,这是ODBC无法将语句限制为1行的事实,因此我可以检查数据类型,其次,由于没有ID,因此在插入时无法检查它是否重复。 At the moment, this is what I have: 目前,这是我所拥有的:

$rConn = odbc_connect("SageLine50", "user", "password");

if ($rConn == 0) {
    die('Unable to connect to the Sage Line 50 V12 ODBC datasource.');
}

$result = odbc_tables($rConn);

$tables = array();

while (odbc_fetch_row($result)){
    if(odbc_result($result,"TABLE_TYPE")=="TABLE") {
        $tables[odbc_result($result,"TABLE_NAME")] = array();
    }
}

This produces the first level of the list you see on pastebin. 这将产生您在pastebin上看到的列表的第一级。

A foreach statement is then run to produce the next level with the columns within the table 然后运行一个foreach语句以产生表中各列的下一个层次

foreach($tables as $k=> $v) {
    $query = "SELECT * FROM ".$k;
    $rRes = odbc_exec($rConn,$query);
    $rFields = odbc_num_fields ($rRes);
    $i = 1;
    while($i <= $rFields) {
        $tables[$k][] = odbc_field_name($rRes, $i);
        $i++;   
    }
    CreateTableandRows($k,$tables[$k]);
}

At the moment, I then have a bodged together function to create each table (not that I like the way it does it). 此刻,我有了一个合并在一起的函数来创建每个表(不是我喜欢它的方式)。

Because I can't automatically grab back one row (or a few rows), to check the type of data with get_type() to then automatically set the row type, it means the only way I can figure out to do this is to set the row type as text and then change them retrospectively based upon a Mysql query. 因为我无法自动获取一行(或几行),所以要使用get_type()检查数据类型然后自动设置行类型,这意味着我唯一可以确定的方法是设置该行类型为文本,然后根据Mysql查询进行追溯更改。

Here is the function that's called for the table creation after the foreach above. 在上面的foreach之后,这是用于创建表的函数。

function CreateTableandRows($table,$rows) {
    $db = array(
        "host" => '10.0.0.200',
        "user" => 'user',
        "pass" => 'password',
        "table" => 'ccl_sagedata'
    );

$DB = new Database($db);

    $LocSQL = "CREATE TABLE IF NOT EXISTS `".$table."` (
            `id` int(11) unsigned NOT NULL auto_increment,
            PRIMARY KEY  (`id`),";  

    foreach($rows as $k=>$v) {
            $LocSQL .= "
            ".$v." TEXT NOT NULL default '',";
    }

    $LocSQL = rtrim($LocSQL, ',');

    $LocSQL .= "
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

    echo '<pre>'.$LocSQL.'</pre>';

    $DB->query($LocSQL);

}

I then need/want a function that takes each table at a time and synchronizes the data to the ccl_sagedata database. 然后,我需要/想要一个函数,一次获取每个表并将数据同步到ccl_sagedata数据库。 However, it needs to make sure it's not inserting duplicates, ie this script will be run to sync the sage database at the start or end of each day and without ID numbers INSERT REPLACE won't work. 但是,它需要确保没有插入重复项,即,该脚本将在每天的开始或结束时运行以同步sage数据库,并且没有ID号INSERT REPLACE将不起作用。 I am obviously implementing auto inc primary ID's for each new table in the ccl_sagedata db. 我显然在为ccl_sagedata db中的每个新表实现auto inc主ID。 But I needs to be able to reference something static in each table that I can identify through ODBC (I hope that makes sense). 但是我需要能够在我可以通过ODBC识别的每个表中引用静态内容(我希望这是有道理的)。 In my current function, it has to call the mysql database for each row on the sage database and see if there is a matching row. 在我当前的函数中,它必须为sage数据库的每一行调用mysql数据库,并查看是否有匹配的行。

function InsertDataFromSage($ODBCTable) {
    $rConn = odbc_connect("SageLine50", "user", "pass");
    $query = "SELECT * FROM ".$ODBCTable;
    $rRes = odbc_exec($rConn,$query);
    $rFields = odbc_num_fields ($rRes);
    while( $row = odbc_fetch_array($rRes) ) {
        $result[] = $row;
    } 
    $DB = new Database($db);
    foreach($result as $k => $v) {
        $CHECKEXISTS = "SELECT * FROM ".$ODBCTable." WHERE";
        $DB->query($CHECKEXISTS);
        // HERE WOULD BE A PART THAT PUTS DATA INTO THE DATABASE IF IT DOESN'T ALREADY EXIST
    }
}

The only other thing I can think to note is that the 'new Database' class is simply just a functionalised standard mysqli database class. 我能想到要注意的唯一另一件事是,“新数据库”类仅仅是功能化的标准mysqli数据库类。 It's not something I'm having problems with. 这不是我遇到的问题。

So to re-cap. 所以要重新封顶。

  1. I am trying to create a synchronization script that creates (if not exists) tables within a mysql database and then imports/syncs the data. 我正在尝试创建一个同步脚本,该脚本在mysql数据库中创建(如果不存在)表,然后导入/同步数据。
  2. ODBC Can't limit the output so I can't figure out the data types in the columns automatically (can't do it manually because it's a massive db with 80+ tables ODBC无法限制输出,因此我无法自动找出列中的数据类型(无法手动完成,因为这是一个拥有80多个表的庞大数据库
  3. I can't figure out how to stop the script creating duplicates because there are no IDs in the sage source database. 我无法弄清楚如何停止脚本创建重复的脚本,因为贤哲源数据库中没有ID。
  4. For those of you not in the UK, Sage is a useless accounting package that runs on water and coal. 对于不在英国的人来说,Sage是一种无用的会计程序,可用于水和煤。
  5. The Sage database only provides data, it doesn't allow you to input data outside of csv files in the actual program. Sage数据库仅提供数据,它不允许您在实际程序中的csv文件之外输入数据。

I know this is a bit late but Im already doing the same thing but with MS SQL. 我知道这有点晚了,但是我已经在做同样的事情,但是使用MS SQL。

Ive used a DTS package that truncates known copies of the tables (ie AUDIT_JOURNAL ) and then copies everything in daily. Ive使用了DTS包,该包会截断表的已知副本(即AUDIT_JOURNAL ),然后每天复制所有内容。

I also hit a bit of a wall trying to handle updates of these tables hence the truncate and re-create. 我也碰壁了一下,试图处理这些表的更新,因此进行了截断并重新创建。 Sync time is seconds so its not a bad option. 同步时间是几秒钟,因此它不是一个坏选择。 It may be a bit of a ball ache but I say design your sync tables manually. 可能会有点痛,但是我说要手动设计同步表。

As you rightly point out, sage is not very friendly to being poked, so id say don't try to sync it all either. 正如您正确指出的那样,鼠尾草对戳戳不是很友好,因此id表示也不要尝试全部同步。

Presumably you'll need reports to present to users but you don't need that much to do this. 大概您需要将报告呈现给用户,但是您不需要那么多操作。 I sync COMPANY,AUDIT_JOURNAL, AUDIT_USAGE, CAT_TITLE,CAT_TITLE_CUS, CHART_LIST,CHART_LIST_CUS, BANK,CATEGORY,CATEGORY_CUS,DEPARTMENT, NOMINAL_LEDGER,PURCHASE_LEDGER,SALES_LEDGER . 我同步了COMPANY,AUDIT_JOURNAL, AUDIT_USAGE, CAT_TITLE,CAT_TITLE_CUS, CHART_LIST,CHART_LIST_CUS, BANK,CATEGORY,CATEGORY_CUS,DEPARTMENT, NOMINAL_LEDGER,PURCHASE_LEDGER,SALES_LEDGER

This allows recreation of all the main reports (balance sheet, trial balance, supplier balances, etc all with drill down). 这样就可以重新浏览所有主要报表(资产负债表,试算表,供应商余额等),并进行深入分析。 If you need more help this late on let me know. 如果您需要更多帮助,请稍后告诉我。 I have a web app called MIS that you could install locally but the sync is a combo of ODBC and the DTS. 我有一个名为MIS的Web应用程序,您可以在本地安装它,但是同步是ODBC和DTS的组合。

OK you do not need to create a synchronisation script you can query ODBC in real time you can even do joins like you do in SQL to retrieve data from multiple tables. 好的,您不需要创建同步脚本,您可以实时查询ODBC,甚至可以像在SQL中一样执行联接以从多个表中检索数据。 The only thing you cannot do is write data back to sage. 您唯一不能做的就是将数据写回到贤者。

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

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