简体   繁体   English

将本地数据库sqlite与生产服务器(MySQL)同步的最佳方法是什么?

[英]What is the best approach for sync local database sqlite with Production server (MySQL)?

I am executing a sql query using transaction.executeSql() for sqlite db with Javascript (Cordova). 我正在使用transaction.executeSql()为sqlite db和Javascript(Cordova)执行sql查询。 I need the last executed query to store in table for future use (sync). 我需要将最后执行的查询存储在表中以供将来使用(同步)。 I didn't find anything like that. 我没有找到类似的东西。 Is there any suggestion? 有什么建议吗?

db.transaction(function(tx) {
 tx.executeSql("Insert into tablename (id, name) Values(?,?);", values, function(tx, results) 
{
    // I need the last executed query here.
});

Data is being updated in the sqlite offline. 数据正在sqlite离线更新。 When user clicks on "Sync" button I will have to push all updates by this user to the server. 当用户点击“同步”按钮时,我将不得不将该用户的所有更新推送到服务器。 So, I thought I will store all queries executed by this user in a table. 所以,我以为我会将这个用户执行的所有查询存储在一个表中。 When he/she clicks on sync button I will just execute those query on live mysql. 当他/她点击同步按钮时,我将在实时mysql上执行这些查询。

Edit 1: This is not for backup purpose only. 编辑1:这不仅用于备份目的。 User can add some new item and update those. 用户可以添加一些新项目并更新它们。 all of the other users should get this changes in their db after sync. 所有其他用户应在同步后在其数据库中获得此​​更改。 And noteworthy, communication will be bidirectional. 值得注意的是,沟通将是双向的。 At first, the sqlitedb will be updated from live server, then I will execute all of the saved queries in the live server. 首先,sqlitedb将从实时服务器更新,然后我将在实时服务器中执行所有已保存的查询。 This is the plan. 这是计划。

Is there any better approach? 有没有更好的方法?

If the size of your database is reasonable, you can compress it and synchronize the ".db.gz" file. 如果数据库的大小合理,您可以压缩它并同步“.db.gz”文件。 If you store each SQL query and sync it, you might end up with something like this 如果您存储每个SQL查询并同步它,您最终可能会得到类似的结果

DELETE FROM x where a=1
UPDATE x set a=2,b=3
INSERT INTO x set c=2

But if you sync the ".db" file it would ONLY contain INSERT statements, along with the schema definition. 但是,如果同步“.db”文件,它将只包含INSERT语句以及模式定义。 However,On the server side, you would need to efficiently import this into MYSQL. 但是,在服务器端,您需要有效地将其导入MYSQL。

If it's only for backup purposes, and you wouldn't need to generate any reports or you don't need to merge all those user data, you can survive with just storing the SQLite on the server. 如果它仅用于备份目的,并且您不需要生成任何报告,或者您不需要合并所有这些用户数据,那么只需将SQLite存储在服务器上即可生存。 But I don't know what exactly is your project about. 但我不知道你的项目到底是什么。

I'm using sync in several enterprise apps with backends, this is my way: 我在几个带有后端的企业应用程序中使用同步,这是我的方式:

  1. Use SSL, if somehow possible. 如果可能的话,使用SSL。

  2. In my workflow, the devices are registrate by scanning a barcode in the backend website. 在我的工作流程中,设备通过扫描后端网站中的条形码进行注册。 As a fall back the registration can be done on the device in a form. 作为后退,可以在设备上以表格形式进行注册。

  3. I use encoding for the transfer datas. 我使用编码来传输数据。 Every device has it's own code. 每个设备都有自己的代码。 The key comes via barcode/form to the device. 密钥通过条形码/表格到达设备。

  4. I have the following client tables in the backend: 我在后端有以下客户端表:

    • tblClients: deviceUUID,deviceType, owner, encryptKey, createdDateTime, … tblClients:deviceUUID,deviceType,owner,encryptKey,createdDateTime,...
    • tblClientLogs: Logs all connections between device and backend tblClientLogs:记录设备和后端之间的所有连接
    • tblClientDelete: deviceUUID, tableName, rowUUID tblClientDelete:deviceUUID,tableName,rowUUID
  5. In every table which has to be synced, I have a created and modified column. 在每个必须同步的表中,我都有一个创建和修改的列。 It's a datetime and created/modified are inserted by triggers. 它是一个日期时间,并且由触发器插入创建/修改。

  6. On a sync task, the device is sending the UUID to the server, the server looks for a valid registration, rows which are created since last sync, rows which are modified since last sync and the row-delete-jobs. 在同步任务中,设备正在将UUID发送到服务器,服务器查找有效的注册,自上次同步以来创建的行,自上次同步以来修改的行以及行删除作业。

  7. The sync datetime is saved in tblClientLogs 同步日期时间保存在tblClientLogs中

8.The encrypted data, the backends send's to the client are objects and looking like: 8.加密数据,后端发送到客户端是对象,看起来像:

tblWhatEver -> create -> {key:value, key:value, …} tblWhatEver - > create - > {key:value,key:value,...}

or 要么

tblWhatEver -> modify -> rowUUID {key:value, key:value, …} tblWhatEver - > modify - > rowUUID {key:value,key:value,...}

or 要么

tblWhatEver -> delete -> rowUUID tblWhatEver - > delete - > rowUUID

  1. How the delete jobs are working. 删除作业如何工作。 If some data are deleted in the backend, I'm looking for all deviceUUIDs and save in tblDeleteJobs: 如果在后端删除了一些数据,我正在寻找所有deviceUUID并保存在tblDeleteJobs中:

deviceUUID, tableName, rowUUID deviceUUID,tableName,rowUUID

After a sync task the device-delete-job is deleted in tblClientDelete. 在同步任务之后,将在tblClientDelete中删除device-delete-job。

  1. After receiving the data to the device, I decrypt it and create a sql statement for every data. 在将数据接收到设备后,我解密它并为每个数据创建一个sql语句。 The sql jobs are done by a loop. sql作业由循环完成。

  2. After all jobs are done on the device I send a small report to the backend for checking if everything goes well. 在设备上完成所有作业后,我会向后端发送一个小报告,以检查一切是否顺利。

  3. In every device table I use UUIDs instead of integers to avoid collisions. 在每个设备表中,我使用UUID而不是整数来避免冲突。 On my devices you can send also datas to the server, it's working similar to the «downloads». 在我的设备上,您也可以将数据发送到服务器,它的工作方式类似于«下载»。

  4. Timezone of my apps and the servers are the same, independent of the device location. 我的应用和服务器的时区是相同的,与设备位置无关。

  5. In the backend and the apps I have included a force sync from beginning, if something went's wrong. 在后端和应用程序中,如果出现问题,我会从头开始包含强制同步。

  6. In some apps I have some more options, there I can change the database, table structure via backend. 在某些应用程序中,我有更多选项,我可以通过后端更改数据库,表结构。

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

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