Summary of the invention
For the above-mentioned defect of prior art, the present invention proposes a kind of SQLITE database application method under embedded system, in order to utilize the advantage of storage resources to greatest extent, in the application of SQLITE database, keep the balance between access speed and security of system.SQLITE database application method under described embedded system comprises: step 1, structure SQLITE database, comprise: step 1.1, establishment one publicly-owned database file and a private data library file in internal storage, wherein publicly-owned data are stored in total database file, and private data is stored in private data library file; Step 1.2, employing AES symmetric cryptography mode carry out entirety encryption to publicly-owned database file; Step 1.3, employing RAS asymmetric encryption mode carry out entirety encryption to private data library file; Step 2, access SQLITE database, comprising: step 2.1, when publicly-owned data inquired about by needs, perform following steps: step 2.1.1, obtain symmetric key; Step 2.1.2, according to described symmetric key decryption publicly-owned database file; If step 2.1.3 successful decryption, then using described publicly-owned database file as database file buffer memory to be visited in internal memory; Step 2.2, when needs inquiry private data time, perform following steps: step 2.2.1, obtain deciphering unsymmetrical key; Step 2.2.2, decipher described private data library file according to described unsymmetrical key; If step 2.2.3 successful decryption, then using described private data library file as database file buffer memory to be visited in internal memory; The copy of the database file described to be visited in step 2.3, data base manipulation SQL statement audit memory is to obtain Query Result.Query Result described in step 3, buffer memory, comprising: if do not store HashMap mapping table in step 3.1 internal memory, then in internal memory, create HashMap mapping table; If do not store HashMap mapping table in step 3.2 external memory storage, then in external memory storage, create HashMap mapping table; Step 3.3, to map to Query Result respectively in internal memory and external memory storage HashMap mapping table, with index entry and data item one to one in the pair wise storing queries result of key and value in described HashMap mapping table, described HashMap mapping table stores the attribute corresponding with each data item simultaneously, and described attribute comprises file ID, Table I D, major key, data item type; Step 3.4, described database file to be visited to be removed from internal memory.
Preferably, SQLITE database application method under described embedded system also comprises: step 4, when again accessing SQLITE database, performs following steps: step 4.1, in internal memory buffer memory HashMap mapping table in the data of accessing needed for fast finding; If step 4.2 is searched unsuccessful, then return step 2.
Preferably, SQLITE database application method under described embedded system also comprises: if step 5 receives the request of more new database, then perform following steps: step 5.1, according to the data upgraded needed for fast finding in the HashMap mapping table of index entry buffer memory in internal memory of the data of required renewal; Step 5.2, according to the data upgraded needed for fast finding in the HashMap mapping table of index entry buffer memory in external memory storage of the data of required renewal; If the data item that step 5.3 finds in step 5.1 with step 5.2 is identical, then performs and upgrade statement; If the data item that step 5.4 finds in step 5.1 with step 5.2 is not identical, then the nonsynchronous miscue of return data item.
Preferably, the SQLITE database application method under described embedded system also comprises: step 6, when the timestamp of symmetric key or unsymmetrical key lost efficacy, perform following steps: step 6.1, remove the HashMap mapping table of buffer memory in internal memory; The HashMap mapping table of buffer memory in step 6.2, removing external memory storage.
The present invention compared to existing technology, has the following advantages:
By setting publicly-owned database file and private data library file, adopt AES symmetric cryptography mode and RAS asymmetric encryption mode to be encrypted respectively to publicly-owned database file and private data library file simultaneously, and effectively coordinate, realize the system equalization of speed and safety.
By being mapped in internal memory and external memory storage by Query Result with HashMap mapping mode simultaneously, and carry out synchronization map, avoid malice to distort the negative effect caused.
By Query Result being mapped to HashMap mapping table and TreeMap mapping table simultaneously, and access HashMap mapping table and TreeMap mapping table respectively according to query type, improve entire system performance.
Embodiment
Detailed description to one or more embodiment of the present invention is hereafter provided together with the accompanying drawing of the diagram principle of the invention.Describe the present invention in conjunction with such embodiment, but the invention is not restricted to any embodiment.Scope of the present invention is only defined by the claims, and the present invention contain many substitute, amendment and equivalent.Set forth many details in the following description to provide thorough understanding of the present invention.These details are provided for exemplary purposes, and also can realize the present invention according to claims without some in these details or all details.
Fig. 1 shows the process flow diagram according to the SQLITE database application method under the embedded system of the embodiment of the present invention.As shown in Figure 1, the SQLITE database application method under embedded system of the present invention at least comprises: step 1, structure SQLITE database.Step 2, access SQLITE database.Query Result described in step 3, buffer memory.Wherein, described SQLITE database comprises two database files, be respectively publicly-owned database file and private data library file, respectively publicly-owned data and private data are kept at publicly-owned database and private data storehouse respectively, although such set-up mode sacrifices a little storage resources, but bring the benefit that security significantly increases thus.Wherein, if the data of accessing belong to the data of accessing first, then described data are carried out buffer memory, particularly be cached in internal memory and external memory storage simultaneously, arranged by this, not only increase the access speed of SQLITE database, and the system that is arranged so that of dual buffer memory can identify the loss of some data automatically, even whether mistake is maliciously tampered.
In embodiments of the present invention, described step 1 specifically comprises: step 1.1, establishment one publicly-owned database file and a private data library file in internal storage, wherein publicly-owned data are stored in total database file, and private data is stored in private data library file; Step 1.2, employing AES symmetric cryptography mode carry out entirety encryption to publicly-owned database file; Step 1.3, employing RAS asymmetric encryption mode carry out entirety encryption to private data library file.On the one hand, due to AES encryption mode, realize speed fast, can improve the efficiency of whole encryption system, but encryption and decryption uses same secret key, security is not enough.And RAS cipher mode, security is higher, but it is slow to realize speed.On the other hand, publicly-owned data often require that but access speed is fast not high to security requirement, and what private data was primary is security requirement.Therefore, the present invention, by arranging different cipher modes for publicly-owned database file and private data library file, makes to be balanced in speed and security the access of SQLITE database.
In embodiments of the present invention, described step 2 specifically comprises: step 2.1, when publicly-owned data inquired about by needs, performs following steps: step 2.1.1, obtains symmetric key; Step 2.1.2, according to described symmetric key decryption publicly-owned database file; If step 2.1.3 successful decryption, then using described publicly-owned database file as database file buffer memory to be visited in internal memory.Step 2.2, when needs inquiry private data time, perform following steps: step 2.2.1, obtain deciphering unsymmetrical key; Step 2.2.2, decipher described private data library file according to described unsymmetrical key; If step 2.2.3 successful decryption, then using described private data library file as database file buffer memory to be visited in internal memory.The copy of the database file described to be visited in step 2.3, data base manipulation SQL statement audit memory is to obtain Query Result.That is for the data type needing access, access different database files respectively, and adopt different decruption keys according to the cipher mode of different database files.And by database file buffer memory to be visited to internal memory, and the inquiry realizing data in internal memory makes inquiry velocity greatly improve.
In embodiments of the present invention, described step 3 specifically comprises: if do not store HashMap mapping table in step 3.1 internal memory, then in internal memory, create HashMap mapping table, that is, SQLITE database is accessed first if belonged to, then need to create HashMap mapping table in internal memory, the present invention adopts HashMap mapping table to further increase the access speed of SQLITE database in data cached mode.If do not store HashMap mapping table in step 3.2 external memory storage, then in external memory storage, create HashMap mapping table, that is make full use of system memory resources, to realize buffer memory verifying function.Step 3.3, to map to Query Result respectively in internal memory and external memory storage HashMap mapping table, with index entry and data item one to one in the pair wise storing queries result of key and value in described HashMap mapping table, described HashMap mapping table stores the attribute corresponding with each data item simultaneously, and described attribute comprises file ID, Table I D, major key, data item type.Step 3.4, described database file to be visited to be removed from internal memory.Wherein, HashMap mapping table is the container as Query Result, HashMap mapping table in the present invention is different from HashMap mapping table of the prior art, in order to usability is stronger, HashMap mapping table in the present invention not only comprises key/value pair, and comprising attribute corresponding to each data item, of the present invention this arranges the type and an attribute column based on chained list that can also replace and become the correspondence that value provides.In a word, make usability stronger by the HashMap mapping table strengthened, such as, can directly locate a certain data item place database file by HashMap mapping table, even database table.
In embodiments of the present invention, also comprising step 4, when again accessing SQLITE database, performing following steps: step 4.1, in internal memory buffer memory HashMap mapping table in the data of accessing needed for fast finding; If step 4.2 is searched unsuccessful, then return step 2.That is, when the non-SQLITE database of access first, first judge the data that whether there is desired seek in the HashMap mapping table of buffer memory in internal memory, in the present invention, term " data ", " data item " all refer to same content, are only different appellations under various circumstances.
In embodiments of the present invention, if also comprise the request that step 5 receives more new database, then following steps are performed: step 5.1, according to the data upgraded needed for fast finding in the HashMap mapping table of index entry buffer memory in internal memory of the data of required renewal; Step 5.2, according to the data upgraded needed for fast finding in the HashMap mapping table of index entry buffer memory in external memory storage of the data of required renewal; If the data item that step 5.3 finds in step 5.1 with step 5.2 is identical, then performs and upgrade statement; If the data item that step 5.4 finds in step 5.1 with step 5.2 is not identical, then the nonsynchronous miscue of return data item.Should be noted that, when normal access SQLITE database, first in internal memory buffer memory HashMap mapping table in search desired data, if find and need to revise desired data, then first to revise in internal memory corresponding data item in HashMap mapping table, and to revise in the storer of inside and outside corresponding data item in HashMap mapping table simultaneously, that is, under normal circumstances, in internal memory, in HashMap mapping table and external memory storage, HashMap mapping table keeps synchronized update.When receiving the request of more new database, then perform above step 5.1-5.4.
In embodiments of the present invention, also comprise step 6, when the timestamp of symmetric key or unsymmetrical key lost efficacy, perform following steps: step 6.1, remove the HashMap mapping table of buffer memory in internal memory; The HashMap mapping table of buffer memory in step 6.2, removing external memory storage.This setting ensure that the security of system, avoids unsafe cache access.
In embodiments of the present invention, further comprising the steps of between step 3.2 and step 3.3: if do not store TreeMap mapping table in internal memory, then in internal memory, to create TreeMap mapping table; Further comprising the steps of between step 3.3 and step 3.4: Query Result to be mapped in the TreeMap mapping table in internal memory, with index entry and data item one to one in the pair wise storing queries result of key and value in described TreeMap mapping table.Described step 4.1 specifically comprises: if search statement during the statement of access SQLITE database, then in internal memory buffer memory HashMap mapping table in the data of accessing needed for fast finding; If traversal statement during the statement of access SQLITE database, then in internal memory buffer memory TreeMap mapping table in the data of the required access of traversal according to the order of sequence.Described step 6 also comprises: the TreeMap mapping table of buffer memory in step 6.3, removing internal memory.Wherein in internal memory, in HashMap mapping table and internal memory, TreeMap mapping table keeps synchronized update.Due to the existence of above-mentioned steps, although occupy a little storage space, but what bring is the very big raising of access speed, especially access HashMap mapping table respectively for different access types and TreeMap mapping table makes the overall performance of system be improved.Wherein, because HashMap mapping table has more advantage in searching, the object therefore searching access is set as HashMap mapping table, and the advantage due to TreeMap mapping table is ranking function, and traversal access often needs orderly traversal, the object therefore traveling through access is set as TreeMap mapping table.
Be to be understood that the embedded system designed by the present invention comprises ANDROID system, IOS system etc.External memory storage designed by the present invention comprises SD card memory etc.
In sum, the SQLITE database application method under the embedded system that the present invention proposes, achieves the system equalization of speed and safety; Avoid malice and distort the negative effect caused; And improve systematic entirety.
Should be understood that, above-mentioned embodiment of the present invention only for exemplary illustration or explain principle of the present invention, and is not construed as limiting the invention.Therefore, any amendment made when without departing from the spirit and scope of the present invention, equivalent replacement, improvement etc., all should be included within protection scope of the present invention.In addition, claims of the present invention be intended to contain fall into claims scope and border or this scope and border equivalents in whole change and modification.