How to increase the upper limit of the data size stored in the Sqlite database
By intercepting the initialization of the SQLite database, dynamically adjusting the memory allocation value and using reflection technology to modify the maximum allocable memory value, it solves the crash problem caused by reading content exceeding 2MB in Android and improves the stability and capacity limit of database data reading.
Patent Information
- Application Number
- CN202111399013.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-19
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2041-11-19
AI Technical Summary
When using a SQLite database in Android, when reading content exceeding 2MB, the CursorWindow cannot hold it, causing the Android framework to throw an exception and crash.
By intercepting the initialization of the SQLite database, the memory allocation algorithm is used to dynamically adjust the maximum allocable memory value, and the threshold is proportionally reduced when allocation fails. Reflection technology is used to modify the maximum allocable memory value of the SQLite database to ensure that exceptions are avoided when reading data.
It effectively increases the upper limit of data size for reading Sqlite database, avoids crash problems caused by memory overlimit, and ensures stable operation of the application.
Smart Images

Figure CN114138474B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of databases, and in particular relates to a method for increasing the upper limit of memory size for reading Sqlite database storage, a computer-readable storage medium and a computer device. Background Art
[0002] When using the SQLite database in Android, if you insert content exceeding 2MB of memory into the SQLite database, everything works fine without crashing. However, when reading a field exceeding 2MB, the CursorWindow cannot accommodate it, and the Android framework directly throws an exception, causing the application to crash. Summary of the invention
[0003] The purpose of the present invention is to provide a method for increasing the storage upper limit of the data size read from a Sqlite database, a computer-readable storage medium and a computer device, aiming to solve the problem that when reading content exceeding 2MB of memory size from a SQLite database, an exception occurs in the Android framework, causing a flash back.
[0004] In a first aspect, the present invention provides a method for increasing the upper limit of the size of data stored in a Sqlite database, comprising:
[0005] S101. When the Sqlite database is used for the first time after the application is started, the initialization of the Sqlite database is intercepted;
[0006] S102, when intercepting the initialization of the Sqlite database, using a memory allocation algorithm to obtain a threshold value of allocatable memory according to the size of the current application's available memory;
[0007] S103, modifying the value of the maximum allocatable memory of the Sqlite database according to the threshold of allocatable memory;
[0008] S104. Allocate the corresponding value of memory to the Sqlite database window according to the modified value of the maximum allocatable memory; if no exception occurs, the allocation is successful and the process ends; if the application recognizes an exception, the allocation fails, the threshold of the allocatable memory is reduced, and then return to S103.
[0009] Furthermore, the threshold value of allocatable memory obtained by using a memory allocation algorithm according to the size of the current application's available memory is specifically: a maximum threshold value of allocatable memory corresponding to the current application's available memory is obtained according to multiple model verifications, and then a threshold value of allocatable memory corresponding to the proportionally reduced available memory obtained by using the memory allocation algorithm is obtained.
[0010] Furthermore, the threshold of allocatable memory corresponding to the proportionally reduced available memory obtained by using the memory allocation algorithm is specifically: when the available memory is greater than 2000MB, the threshold of allocatable memory is 112MB; when the available memory is less than 2000MB but greater than 512MB, the threshold of allocatable memory is 88MB; when the available memory is less than 512MB but greater than 256MB, the threshold of allocatable memory is 50MB; when the available memory is less than 256MB but greater than 128MB, the threshold of allocatable memory is 20MB; when the available memory is less than 128MB, 10MB is uniformly allocated as a bottom line.
[0011] Furthermore, the method also includes that when multiple allocations fail, the current allocation is considered to have failed, all previous steps are canceled, and the value of the allocated memory adopts the default value of the available memory of the current application.
[0012] Furthermore, the S103 is specifically as follows: in the running state of the application, the threshold value of the allocatable memory is obtained and called by using the reflection technology, and the value of the maximum allocatable memory of the Sqlite database is modified to the threshold value of the allocatable memory.
[0013] In a second aspect, the present invention provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the method for increasing the upper limit of the size of data stored in a Sqlite database.
[0014] In a third aspect, the present invention provides a computer device, comprising: one or more processors, a memory, and one or more computer programs, wherein the processor and the memory are connected via a bus, wherein the one or more computer programs are stored in the memory and are configured to be executed by the one or more processors, and when the processor executes the computer program, the steps of the method for increasing the upper limit of the data size read from a Sqlite database storage are implemented.
[0015] In the present invention, the value of the maximum allocatable memory is modified according to the reflection technology and allocated to the Sqlite database window. If the allocation fails, the threshold of the allocatable memory is reduced according to the ratio, the value of the maximum allocatable memory of the Sqlite database is modified again, the size of the database memory is dynamically modified, the upper limit of the size of the data stored in the Sqlite database is increased, and the problem of reading the SQLite database when the content exceeds the 2MB memory size is solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 The present invention is a flowchart of a method for increasing the upper limit of the size of data stored in a Sqlite database.
[0017] Figure 2 It is a specific structural block diagram of a computer device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0018] In order to make the purpose, technical solution and beneficial effects of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0019] In order to illustrate the technical solution of the present invention, a specific embodiment is provided below for illustration.
[0020] See also Figure 1 , a method for increasing the upper limit of the size of data stored in a Sqlite database provided by an embodiment of the present invention comprises the following steps: It should be noted that if there is substantially the same result, the method for increasing the upper limit of the size of data stored in a Sqlite database of the present invention is not based on Figure 1 The process sequence shown is limited.
[0021] S101. When the Sqlite database is used for the first time after the application is started, the initialization of the Sqlite database is intercepted;
[0022] S102, when intercepting the initialization of the Sqlite database, using a memory allocation algorithm to obtain a threshold value of allocatable memory according to the size of the current application's available memory;
[0023] S103, modifying the value of the maximum allocatable memory of the Sqlite database according to the threshold of allocatable memory;
[0024] S104. Allocate the corresponding value of memory to the Sqlite database window according to the modified value of the maximum allocatable memory; if no exception occurs, the allocation is successful and the process ends; if the application recognizes an exception, the allocation fails, the threshold of the allocatable memory is reduced, and then return to S103.
[0025] In one embodiment of the present invention, the threshold value of allocatable memory obtained by using a memory allocation algorithm based on the size of the current application's available memory is specifically a threshold value of the maximum allocatable memory corresponding to the current application's available memory obtained based on multiple model verifications, and then a threshold value of the allocatable memory corresponding to the proportionally reduced available memory obtained by using the memory allocation algorithm.
[0026] In one embodiment of the present invention, the threshold of the allocatable memory corresponding to the proportionally reduced available memory obtained by using the memory allocation algorithm is specifically: when the available memory is greater than 2000MB, the threshold of the allocatable memory is 112MB; when the available memory is less than 2000MB but greater than 512MB, the threshold of the allocatable memory is 88MB; when the available memory is less than 512MB but greater than 256MB, the threshold of the allocatable memory is 50MB; when the available memory is less than 256MB but greater than 128MB, the threshold of the allocatable memory is 20MB; when the available memory is less than 128MB, 10MB is uniformly allocated as a bottom line.
[0027] In an embodiment of the present invention, the method further includes: when multiple allocations fail, the current allocation is considered to have failed, all previous steps are canceled, and the value of the allocated memory adopts the default value of the available memory of the current application.
[0028] In one embodiment of the present invention, S103 specifically includes: in the running state of the application, using reflection technology to obtain and call the threshold of allocatable memory, and modifying the value of the maximum allocatable memory of the Sqlite database to the threshold of allocatable memory.
[0029] An embodiment of the present invention provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps of the method for increasing the upper limit of the size of data stored in a Sqlite database as provided in an embodiment of the present invention are implemented.
[0030] Figure 2 A specific structural block diagram of a computer device provided in one embodiment of the present invention is shown. A computer device 100 includes: one or more processors 101, a memory 102, and one or more computer programs, wherein the processor 101 and the memory 102 are connected via a bus, the one or more computer programs are stored in the memory 102, and are configured to be executed by the one or more processors 101, and when the processor 101 executes the computer program, the steps of the method for increasing the upper limit of the size of data stored in a Sqlite database as provided in one embodiment of the present invention are implemented.
[0031] The computer device includes a server and a terminal, etc. The computer device may be a desktop computer, a mobile terminal or a vehicle-mounted device, and the mobile terminal includes at least one of a mobile phone, a tablet computer, a personal digital assistant or a wearable device, etc.
[0032] In an embodiment of the present invention, the value of the maximum allocatable memory is modified according to the reflection technology and allocated to the Sqlite database window. If the allocation fails, the threshold of the allocatable memory is reduced according to the ratio, the value of the maximum allocatable memory of the Sqlite database is modified again, the size of the database memory is dynamically modified, and the upper limit of the size of the data stored in the Sqlite database is increased, thereby solving the problem of reading the SQLite database when the content exceeds the 2MB memory size.
[0033] A person skilled in the art may understand that all or part of the steps in the various methods of the above embodiments may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the storage medium may include: a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0034] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for increasing the upper limit of the size of data stored in a Sqlite database, characterized in that: include: S101. When the Sqlite database is used for the first time after the application is started, the initialization of the Sqlite database is intercepted; S102. When intercepting the initialization of the Sqlite database, a threshold value of allocatable memory is obtained by using a memory allocation algorithm according to the size of the current application's available memory; the method of obtaining the threshold value of allocatable memory by using a memory allocation algorithm according to the size of the current application's available memory is specifically as follows: a maximum threshold value of allocatable memory corresponding to the current application's available memory is obtained according to multiple machine model verifications, and then a threshold value of allocatable memory corresponding to the proportionally reduced available memory obtained by using the memory allocation algorithm is obtained; S103, modifying the maximum allocatable memory value of the Sqlite database according to the allocatable memory threshold, specifically: in the running state of the application, using reflection technology to obtain and call the allocatable memory threshold, and modifying the maximum allocatable memory value of the Sqlite database to the allocatable memory threshold; S104, allocating the corresponding value of memory to the Sqlite database window according to the modified value of the maximum allocatable memory; if no exception occurs, the allocation is successful and the process ends; If the application recognizes an exception, the allocation fails, the threshold of the allocatable memory is lowered, and then the process returns to S103.
2. The method according to claim 1, characterized in that The threshold of the allocatable memory corresponding to the proportionally reduced available memory obtained by the memory allocation algorithm is specifically: when the available memory is greater than 2000MB, the threshold of the allocatable memory is 112MB; when the available memory is less than 2000MB and greater than 512MB, the threshold of the allocatable memory is 88MB; When the available memory is less than 512MB and greater than 256MB, the threshold of allocatable memory is 50MB; When the available memory is less than 256MB and greater than 128MB, the threshold of allocatable memory is 20MB; When the available memory is less than 128MB, 10MB is allocated as a backup.
3. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method for increasing the upper limit of the size of data stored in a Sqlite database as described in claim 1 or 2 are implemented.
4. A computer device comprising: One or more processors, a memory and one or more computer programs, wherein the processor and the memory are connected via a bus, wherein the one or more computer programs are stored in the memory and are configured to be executed by the one or more processors, wherein when the processor executes the computer program, the steps of the method for increasing the upper limit of the size of data stored in a Sqlite database as described in claim 1 or 2 are implemented.
