Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
To facilitate understanding of the present embodiment, a brief description of the client database is first provided. The client database is generally composed of a plurality of data tables, and one column of the data tables is called a field and is used for defining the attribute of the data in one dimension, such as 'name', 'school number', 'grade', 'gender', and the like; one row of the data table is called a record and is used for storing a piece of user data, and the user data comprises the dimension information of each field. Typically, when an upgrade is applied, the table structure of some or all of the data tables may be modified, such as deleting certain fields or adding certain fields. And modifying the table structure in the data table by updating the database. In practical application, the data table may be a data table related to a service (referred to as a service table for short), or may be a data table related to other aspects.
An embodiment of the present invention provides a method for updating a client database, as shown in fig. 1, the method includes:
101. and comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table to determine whether the stored old version of the data table needs to be updated.
The upgrade package carries first signature information of each data table in the database, and the first signature information is obtained by calculation according to the table structure information of the data tables recorded in the upgrade package. The table structure defines information such as a name, a field, a type, a primary key, a foreign key, an index, etc. of a data table, for example, the table structure information records the name of the data table, and what fields, data tables, or data types of the fields are included. In this embodiment, signature calculation is performed on the table structure information according to a preset signature algorithm. The signature algorithm has the characteristic of one data one signature, namely the signature information of the same data is the same, and the signature information of different data is different. When the table structure information changes, for example, when fields are increased or decreased, the signature information for the new table structure information will change. By utilizing the characteristic, the embodiment of the invention can judge whether the upgrade package changes the table structure of the data table, thereby determining whether the data table of the old version needs to be updated.
It should be noted that the first signature information obtained from the upgrade package may be directly carried in the upgrade package after being calculated by the network side, or may be carried in the upgrade package and calculated by the client, which is not limited in this embodiment.
In order to facilitate the judgment of whether the table structure information of the data table is changed, a management table for recording the mapping relationship between the identifier of the data table and the signature information is established in the database in addition to the data table for storing data, and the corresponding second signature information can be searched in the management table through the identifier (such as the table name) of the data table.
The second signature information is essentially the same as the first signature information, and is obtained by calculating the table structure information through a signature algorithm, and the difference is that the second signature information is the signature information stored in the management table, and the signature information may be the first signature information obtained from the upgrade package at the last update. After each update of the data table, the latest first signature information needs to be updated into the management table.
After obtaining the upgrade package and the management table, the client may compare first signature information of each data table in the upgrade package with second signature information corresponding to the identifier of the data table in the management table; if the first signature information is the same as the second signature information, the table structure information of the data table is changed, so that the data table of the old version can be determined to need to be updated; if the first signature information is different from the second signature information, it indicates that the table structure information of the data table is not changed, so that it can be determined that the old version of the data table does not need to be updated.
It should be added that, in practical applications, a hash Algorithm may be used as the signature Algorithm, for example, MD5 (chinese name is fifth edition of Message Digest Algorithm, Message Digest Algorithm) Algorithm may be used for signature, and this embodiment does not specifically limit the specific signature Algorithm.
102. And if the old version of the data table is determined to need updating, creating a new version of the data table, and replacing the old version of the data table with the new version of the data table.
If the first signature information is different from the second signature information, the table structure of the data table in the new version is changed (including but not limited to field increase and decrease, field type change, change of the external key of the main key, modification of the index information and the like), so that the data table in the old version is determined to need to be updated. In this case, the client creates a new version data table according to the latest table structure information in the upgrade package, and replaces the old version data table stored in the target directory with the new version data table.
103. And if the data table of the old version does not need to be updated, not performing replacement operation on the data table of the old version.
If the first signature information is the same as the second signature information, the table structure of the data table in the new version is not changed, so that the data table of the old version is determined not to need to be updated, and further, the data table of the old version does not need to be replaced. Compared with the prior art, the data table which does not need to be updated is not repeatedly replaced, so that the user data (each record) recorded in the data table can be prevented from being lost due to the coverage of the data table.
The method for updating the client database provided by the embodiment of the invention can record the signature information of each data table through the management table, and the signature information is calculated according to the table structure information of the corresponding data table. When the database needs to be updated, whether the stored data table of the old version needs to be updated or not can be determined by comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table; when it is determined that a certain old version of the data table needs to be updated, the new version of the data table created based on the new table structure information is directly replaced with the old version of the data table without paying attention to the table structure change history related to past version iteration, so that the table structure of the data table cannot be influenced even if the certain version of the data table is deleted for updating, and the method is suitable for the process of cross-version updating; when determining that the data table of an old version does not need to be updated, the data table is not operated, and the loss of user data in the part of the data table is avoided.
Further, as an extension to the method shown in fig. 1, an embodiment of the present invention further provides a method for updating a client database, as shown in fig. 2, where the method includes:
201. and after receiving the installation package, creating the management table.
When an application is first started after installation, the client creates a local database. The local database generally exists in a file form, and the created database file is the created database. Taking the SQLite relational database as an example, the Application program installation package carries an API (Application Programming Interface) for opening the database: the client calls the API to complete the creation of the database, either sqlite3_ open or sqlite3_ open _ v 2. Since the database is not created in the current directory, the database can be created by opening the database for the first time. The creation of the database can be accomplished by calling either of these two APIs. For sqlite3_ open, if the local database does not exist, then calling sqlite3_ open will default to creating the database; for SQLITE3_ OPEN _ v2, the transfer of the flags parameter in SQLITE3_ OPEN _ v2 into SQLITE _ OPEN _ READWRITE | SQLITE _ OPEN _ CREATE CREATEs the database by default.
The created database only contains some basic data tables of the database and does not contain data tables related to the application program.
After creating the database, the client creates a management table. As shown in table 1, the management table includes two fields, one field is a data table name, and the other field is signature information (hash value) corresponding to the data table. The name and hash value of the data table are not recorded in the newly created management table.
TABLE 1
The fields in the management table are relatively fixed and usually do not change, so the table structure of the management table is usually not changed after the management table is created for the first time.
202. And respectively creating each data table according to the table structure information of each data table in the installation package.
The installation package carries table structure information of a data table required to be established in the current version, the table structure information is recorded in a database character string of the installation package, and for an SQL relational database, the table structure information is recorded in the SQL character string. After the SQL character string is taken from the installation package, the client checks whether the corresponding table name exists in the management table (does not exist), then creates a corresponding data table in a mode of executing the SQL character string, and then carries out hash calculation on the SQL character string to obtain a corresponding hash value.
In practical applications, please refer to the prior art for an implementation manner of executing the SQL string to create the data table.
203. And adding the mapping relation between the identification and the signature information of each data table in the installation package into the management table.
And adding a data table name in the management table, and adding a hash value of the data table corresponding to the data table name to complete the establishment of the mapping relation. When a plurality of data tables need to be established, the mapping relation of each data table is added in sequence. In an example of this embodiment, the management table after the mapping relationship is established is shown in table 2:
TABLE 2
Data table names
|
Hash value
|
TABLE A
|
a
|
TABLE B
|
b
|
Watch C
|
c |
After step 203 is executed, the establishment of the management table and the data table is completed, and when the application upgrade is subsequently required, steps 204 to 209 are executed.
204. And acquiring first signature information of each data table from the upgrade package, and searching second signature information corresponding to the identifier of the data table from the management table.
In this embodiment, the upgrade package carries table structure information of the data table, where the table structure information is recorded in a database string of the upgrade package, and for the SQL relational database, the table structure information is recorded in an SQL string. And the client performs signature operation on the table structure information carried in the upgrade package to obtain first signature information. For example, hash calculation is performed on the SQL string in the upgrade package to obtain a corresponding hash value. In practical application, the upgrade package can also directly carry the first signature information calculated by the network side server, and the client side can directly obtain the first signature information. The signature algorithm used by the server and the obtained signature information are the same as those of the client side and will not be described herein.
After the client acquires the table structure information of a certain data table from the upgrade package and acquires the first signature information of the data table, the client may first search for an identifier (e.g., a table name) of the data table from the table structure information, and then search for corresponding second signature information in the management table according to the identifier of the data table.
205. And if the second signature information is found, comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table, and determining whether the stored data table of the old version needs to be updated.
The specific implementation manner of this step is the same as that of step 101, and is not described herein again.
206. And if the old version of the data table is determined to need updating, creating a new version of the data table, replacing the old version of the data table with the new version of the data table, and replacing the second signature information in the management table with the first signature information.
After the data table of the old version is determined to need to be updated, the data table of the old version needs to be replaced by the data table of the new version, so that the data table of the old version is updated, and in order to accurately judge whether the data table needs to be updated or not in the next database updating process, the first signature information needs to be used for replacing the second signature information in the management table, so that the management table is also updated in time.
It should be noted that, the two operations of updating the data table and updating the management table may be performed simultaneously, or the data table may be updated first and then the management table may be updated first, or the management table may be updated first and then the data table may be updated first, and the execution order of the two operations is not limited.
Illustratively, the hash value obtained by the client from the upgrade package is: tables A-a, B-B, C-cc. After the management table is compared, it is determined that the signature information of the table C is changed, and the table C needs to be updated.
The upgrade package carries table structure information of the data table C, which is recorded in the database string of the installation package. And after the SQL character string of the table C is taken from the installation package, the client side creates a corresponding data table in a mode of executing the SQL character string to obtain the table C with the updated table structure, and then replaces the table C stored in the original target directory with the updated table C to finish updating.
In practical applications, please refer to the prior art for an implementation manner of executing the SQL string to create the data table.
If the client replaces the hash value c in table 2 with cc, the management table after replacement is shown in table 3:
TABLE 3
Data table names
|
Hash value
|
TABLE A
|
a
|
TABLE B
|
b
|
Watch C
|
cc |
207. And if the data table of the old version does not need to be updated, not performing replacement operation on the data table of the old version.
208. And if the second signature information is not found (namely the mapping relation between the identifier of the data table and the second signature information is not recorded in the management table), creating the data table according to the table structure information of the data table recorded in the upgrade package.
In another case of this embodiment, when a new data table is added to the upgrade package, the client cannot look up the identifier and the hash value of the table in the management table, in which case the data table needs to be added newly. Specifically, the method comprises the following steps:
assuming that a table D is newly added to the upgrade package, the client retrieves the SQL string of the table D from the upgrade package, creates a corresponding data table D by executing the SQL string, then stores the table D in the target directory, and then performs step 209.
209. And adding the mapping relation between the identification of the data table and the first signature information into the management table.
If based on table 3, the client adds a new record of data table D in the management table, and the management table after adding the record is shown in table 4:
TABLE 4
Further, when a certain data table in the management tables does not have corresponding table structure information in the upgrade package, it indicates that the upgraded version needs to be deleted. And after the client finishes traversing the records in the management table, deleting the records of the data tables and deleting the corresponding data tables in the target directory.
The method for updating the client database provided by the embodiment of the invention can acquire the first signature information of each data table from the upgrade package after the upgrade package is acquired, search the second signature information corresponding to the identifier of the data table from the management table, determine whether the data table of the old version needs to be updated or not by comparing the first signature information with the second signature information when the second signature information can be searched, and directly create the corresponding data table according to the table structure information of the data table recorded in the upgrade package when the second signature information cannot be searched, thereby ensuring that various types of data tables can be quickly and accurately established. In addition, when the original data table is updated and a brand-new data table is created, the management table is updated, so that accurate updating basis is provided for next updating of the database.
Further, as an implementation of the foregoing method, an embodiment of the present invention further provides a device for updating a client database, where the device is located on a client side, and as shown in fig. 3, the device includes:
a determining unit 31, configured to determine whether the saved old version of the data table needs to be updated by comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table; the management table is used for recording the mapping relation between the identification of the data table and the signature information, the first signature information is obtained by calculation according to the table structure information of the data table recorded in the upgrade package, and the second signature information is obtained by calculation according to the table structure information of the data table in the currently installed old version database;
a creating unit 32, configured to create a new version of the data table when the determining unit 31 determines that the old version of the data table needs to be updated;
a replacing unit 33 for replacing the old version of the data table with the new version of the data table created by the creating unit 32;
the replacing unit 33 is further configured to not perform a replacing operation on the old version of the data table when the determining unit 31 determines that the old version of the data table does not need to be updated.
Further, the replacing unit 33 is further configured to replace the second signature information in the management table with the first signature information when the determining unit 31 determines that the old version of the data table needs to be updated.
Further, as shown in fig. 4, the determining unit 31 includes:
a comparing module 311, configured to compare the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table;
a determining module 312, configured to determine that the old version of the data table needs to be updated when the comparison result of the comparing module 311 is that the first signature information is the same as the second signature information;
the determining module 312 is configured to determine that the old version of the data table does not need to be updated when the comparison result of the comparing module 311 is that the first signature information is different from the second signature information.
Further, the creating unit 32 is further configured to create the data table according to the table structure information of the data table recorded in the upgrade package when the mapping relationship between the identifier of the data table and the second signature information is not recorded in the management table;
as shown in fig. 4, the apparatus further includes:
a first adding unit 34, configured to add a mapping relationship between the identifier of the data table and the first signature information to the management table.
Further, the creating unit 32 is further configured to create the management table after receiving the installation package before determining whether the saved old version of the data table needs to be updated by comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table; respectively creating each data table according to the table structure information of each data table in the installation package;
as shown in fig. 4, the second adding unit 35 is configured to add the mapping relationship between the identifier and the signature information of each data table in the installation package to the management table.
The device for updating the client database provided by the embodiment of the invention can record the signature information of each data table through the management table, and the signature information is calculated according to the table structure information of the corresponding data table. When the database needs to be updated, whether the stored data table of the old version needs to be updated or not can be determined by comparing the first signature information of each data table in the upgrade package with the second signature information corresponding to the identifier of the data table in the management table; when it is determined that a certain old version of the data table needs to be updated, the new version of the data table created based on the new table structure information is directly replaced with the old version of the data table without paying attention to the table structure change history related to past version iteration, so that the table structure of the data table cannot be influenced even if the certain version of the data table is deleted for updating, and the method is suitable for the process of cross-version updating; when determining that the data table of an old version does not need to be updated, the data table is not operated, and the loss of user data in the part of the data table is avoided.
In the foregoing embodiments, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
It will be appreciated that the relevant features of the method and apparatus described above are referred to one another. In addition, "first", "second", and the like in the above embodiments are for distinguishing the embodiments, and do not represent merits of the embodiments.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
The algorithms and displays presented herein are not inherently related to any particular computer, virtual machine, or other apparatus. Various general purpose systems may also be used with the teachings herein. The required structure for constructing such a system will be apparent from the description above. Moreover, the present invention is not directed to any particular programming language. It is appreciated that a variety of programming languages may be used to implement the teachings of the present invention as described herein, and any descriptions of specific languages are provided above to disclose the best mode of the invention.
In the description provided herein, numerous specific details are set forth. It is understood, however, that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures and techniques have not been shown in detail in order not to obscure an understanding of this description.
Similarly, it should be appreciated that in the foregoing description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof for the purpose of streamlining the disclosure and aiding in the understanding of one or more of the various inventive aspects. However, the disclosed method should not be interpreted as reflecting an intention that: that the invention as claimed requires more features than are expressly recited in each claim. Rather, as the following claims reflect, inventive aspects lie in less than all features of a single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of this invention.
Those skilled in the art will appreciate that the modules in the device in an embodiment may be adaptively changed and disposed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and furthermore they may be divided into a plurality of sub-modules or sub-units or sub-components. All of the features disclosed in this specification (including any accompanying claims, abstract and drawings), and all of the processes or elements of any method or apparatus so disclosed, may be combined in any combination, except combinations where at least some of such features and/or processes or elements are mutually exclusive. Each feature disclosed in this specification (including any accompanying claims, abstract and drawings) may be replaced by alternative features serving the same, equivalent or similar purpose, unless expressly stated otherwise.
Furthermore, those skilled in the art will appreciate that while some embodiments described herein include some features included in other embodiments, rather than other features, combinations of features of different embodiments are meant to be within the scope of the invention and form different embodiments. For example, in the following claims, any of the claimed embodiments may be used in any combination.
The various component embodiments of the invention may be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art will appreciate that a microprocessor or Digital Signal Processor (DSP) may be used in practice to implement some or all of the functions of some or all of the components of the method and apparatus for client database updates according to embodiments of the present invention. The present invention may also be embodied as apparatus or device programs (e.g., computer programs and computer program products) for performing a portion or all of the methods described herein. Such programs implementing the present invention may be stored on computer-readable media or may be in the form of one or more signals. Such a signal may be downloaded from an internet website or provided on a carrier signal or in any other form.
It should be noted that the above-mentioned embodiments illustrate rather than limit the invention, and that those skilled in the art will be able to design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in a claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The invention may be implemented by means of hardware comprising several distinct elements, and by means of a suitably programmed computer. In the unit claims enumerating several means, several of these means may be embodied by one and the same item of hardware. The usage of the words first, second and third, etcetera do not indicate any ordering. These words may be interpreted as names.