CN113742361B - Method and system for storing JSON data by using SQL under JS development environment - Google Patents

Method and system for storing JSON data by using SQL under JS development environment Download PDF

Info

Publication number
CN113742361B
CN113742361B CN202110985601.2A CN202110985601A CN113742361B CN 113742361 B CN113742361 B CN 113742361B CN 202110985601 A CN202110985601 A CN 202110985601A CN 113742361 B CN113742361 B CN 113742361B
Authority
CN
China
Prior art keywords
data
json
key
sql
value
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202110985601.2A
Other languages
Chinese (zh)
Other versions
CN113742361A (en
Inventor
王建国
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tibet Ningsuan Technology Group Co ltd
Original Assignee
Tibet Ningsuan Technology Group Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tibet Ningsuan Technology Group Co ltd filed Critical Tibet Ningsuan Technology Group Co ltd
Priority to CN202110985601.2A priority Critical patent/CN113742361B/en
Publication of CN113742361A publication Critical patent/CN113742361A/en
Application granted granted Critical
Publication of CN113742361B publication Critical patent/CN113742361B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/242Query formulation
    • G06F16/2433Query languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/25Integrating or interfacing systems involving database management systems

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Databases & Information Systems (AREA)
  • Data Mining & Analysis (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Stored Programmes (AREA)

Abstract

The invention provides a method and a system for storing JSON data by using SQL under a JS development environment. The method comprises the following steps: establishing a data model corresponding to the JSON data, and mapping the JSON data structure into an SQLite structure; according to the JSON data and the data model, generating SQL sentences by using the corresponding data; the generated SQL statement is executed by calling the interface specification of the uni-app to insert the data into the database. The invention realizes the operation of directly storing the JSON data structure into the database by establishing the object mapping relation between the JSON and the SQL database, does not need a programmer to do various complicated operations such as type conversion, handwriting SQL and the like for various scenes, and can directly perform database operation on the JSON data.

Description

Method and system for storing JSON data by using SQL under JS development environment
Technical Field
The invention relates to the technical field of database operation, in particular to a method and a system for storing JSON data by using SQL in a JS development environment.
Background
Currently, the database storage is MySQL, redis, mongoDB, SQLite. Among them, SQLite is already used in many embedded products, which occupy very low resources, and in embedded devices, it may be enough to only require several hundred K of memory. The system can support mainstream operating systems such as Windows/Linux/Unix and the like, can be combined with a plurality of programming languages such as Tcl, C#, PHP, java and the like, and also has an ODBC interface, and the processing speed is faster than that of two open-source database management systems such as Mysql, postgreSQL.
At present, XML and JSON are the most commonly used data exchange formats of cross-language and cross-platform. JSON has the characteristics of good readability and convenience for quick writing, and can exchange data between different platforms. JSON adopts a very compatible, completely independent language text format, and simultaneously has a behavior similar to that of a habit (comprising C, c++, c#, java, javaScript, perl, python and the like) system of a C language. These characteristics make JSON an ideal data exchange language.
The prior art does not have a complete JS-based technical scheme for directly converting JSON into SQL sentences and inserting the SQL sentences into a database.
Disclosure of Invention
The invention aims to: the invention aims to provide a method and a system for storing JSON data by using SQL under a JS development environment, which can realize the process of directly inserting a JSON data structure into an SQL database by an adapter and a manager under the JS development environment, simplify the process of storing the JSON data by using SQL and facilitate program development.
The technical scheme is as follows: in order to achieve the above purpose, the present invention adopts the following technical scheme:
According to a first aspect, there is provided a method for storing JSON data using SQL in a JS development environment, including the steps of:
Establishing a data model corresponding to the JSON data, and mapping the JSON data structure into an SQLite structure;
According to the JSON data and the data model, corresponding data are generated into SQL sentences;
The generated SQL statement is executed by calling the interface specification of the uni-app to insert the data into the database.
According to a second aspect, there is provided a system for storing JSON data using SQL in a JS development environment, including:
The data model module is used for establishing a data model corresponding to the JSON data and mapping the JSON data structure into the SQLite structure;
The adapter module is used for generating SQL sentences from the corresponding data according to the JSON data and the data model;
and the execution module is used for executing the generated SQL statement by calling the interface specification of the uni-app and inserting the data into the database.
According to a third aspect, there is provided a computer device comprising: one or more processors; a memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, which when executed by the processors implement a method for storing JSON data using SQL in the JS development environment in accordance with the first aspect of the present invention.
The beneficial effects are that: the invention realizes the operation of directly storing the JSON data structure into the database by establishing the object mapping relation between the JSON and the SQL database, does not need a programmer to do various complicated operations such as type conversion, handwriting SQL and the like for various scenes, and can directly perform database operation on the JSON data.
Drawings
FIG. 1 is a flow chart of a method for storing JSON data using SQL according to an embodiment of the invention;
Fig. 2 is a schematic diagram of a data query result according to an embodiment of the present invention.
Detailed Description
The technical scheme of the invention is further described below with reference to the accompanying drawings.
Abbreviations and technical terms involved in the present invention will be explained first.
In the description, the following abbreviations are referred to.
SQL, an abbreviation for SQLite, is a lightweight database, is an ACID-compliant relational database management system, and is contained in a relatively small C-library.
JS, an abbreviation of JavaScript, is a lightweight, interpreted, or just-in-time compiled programming language with function prioritization.
JSON, collectively JavaScript Object Notation, is a lightweight data interchange format that stores and presents data in a text format that is completely independent of the programming language, based on a subset of ECMAScript (js specification by the european computer institute).
The uni-app, a unified front end framework that develops all front end applications using vue.js, uses JS language to write code logic. A developer writes a set of code that can be distributed to multiple platforms iOS, android, web (responsive), as well as various applets (WeChat/Payment treasures/hundred degrees/header strips/QQ/nail/Taobao, etc.), quick applications, etc.
Key terms used in the present invention are defined as follows.
Model: the representative model is described by JS language, which is a mapping relation between JSON objects and database fields, and simultaneously records the related attributes of the database fields.
Adapter: the representative adapter is a tool for converting JSON data structures into SQL statements through model mappings.
Exec: and the representative executor realizes a tool for executing SQL sentences to add, delete and search the SQLite database.
Aiming at the problem that the prior art lacks a complete JS-based technical scheme for directly converting JSON into SQL sentences and inserting the SQL sentences into a database, the invention mainly aims to quickly convert JSON data into database data for storage and quickly find out the database data in a uni-app development environment. Meanwhile, in the scheme, complex codes are modularized, simplified and development difficulty of programmers is reduced. And unify the interface of the data storage, facilitate the maintenance and management of the post code logic.
In order to achieve the purpose, the scheme of the invention mainly aims at a series of encapsulation and processing of the database storage of the JSON data, and comprises the steps of realizing the formulation of the mapping relation between the JSON data and the SQLite data; an adapter for realizing the mutual conversion of JSON data and SQLite sentences; an executor of interfacing the uni-app interface specification is implemented.
Referring to fig. 1, for a uni-app development environment, under a mobile terminal device, a scenario in which JSON data needs to be stored in a database is required. As an example, there is JSON data User, the data is as follows:
{ "id":1234, "username": "Xiaoming", "age":3}
The data needs to be inserted into the SQLite database.
The method for storing JSON data by using SQL under the JS development environment comprises the following steps:
(1) A data model module 100 is built corresponding to the given JSON data for mapping the JSON data structure into the SQLite structure.
The data model is based on JSON objects to establish corresponding js objects. Each JSON object is a value, which may be an array or object, or may be an original type of value. JSON has strict specifications on the type and format of values. According to the rule of JSON, the mapping relation between JSON and js can be established, specifically as follows: each key of js object represents a key of JSON object after conversion into database data, and the value corresponding to each key of js object stores three fixed attributes representing the attributes of the corresponding JSON object key and value. Three fixed attributes are: transValue: names of JSON keys corresponding to keys representing js dataType: data types of JSON keys corresponding to js keys are represented, options: represents the additional properties of the JSON key corresponding to js key.
In this embodiment, a data model built according to JSON data User is as follows:
(2) An adapter module 200 is established to generate SQL statements from the corresponding data according to the JSON data and model provided by the data model module 100.
Specifically, each key and value of the data model are traversed, what type of the corresponding JSON data value is judged, the corresponding type is recorded, then according to the attribute of the key and value record of the data model, the corresponding JSON data value is taken out, all the JSON values and the value types are circularly taken out, and the JSON values and the value types are spliced into an SQL database statement.
In this embodiment, the adaptation operation of JSON data User according to the established data model is as follows:
the SQL statement generated after the execution of the above procedure is:
insert inter user (id, user name, age) values (1234, 'Xiaoming', 3)
The function of the SQL sentence is to insert a piece of data into the SQL database, wherein the data comprises three fields and corresponding values as follows: id 1234, username, xiaoming, age 3.
(3) The executor exec module 300 is established, and the SQL statement generated in the last step is executed by calling the interface specification of the uni-app to insert data into a database:
Specifically, an API capable of executing the database statement is called uniapp, the SQL database statement acquired in the last step is executed, and if the execution is successful, the data is successfully inserted into the database.
In this embodiment, the specific code for executing the query sql is as follows:
(4) And verifying the operation result, checking the SQL database after calling the operation, and inserting data as shown in the result figure 2.
According to another embodiment, there is provided a system for storing JSON data using SQL in a JS development environment, including:
The data model module is used for establishing a data model corresponding to the JSON data and mapping the JSON data structure into the SQLite structure;
The adapter module is used for generating SQL sentences from the corresponding data according to the JSON data and the data model;
and the execution module is used for executing the generated SQL statement by calling the interface specification of the uni-app and inserting the data into the database.
Specifically, the process of the data model module to build the model is as follows:
establishing a corresponding js object according to the JSON object, wherein each key of the js object represents a key after the key of the JSON object is converted into database data, each value corresponding to each key of the js object comprises three attributes, representing attributes corresponding to the key and the value of the JSON object, and the three attributes are as follows:
transValue: the name of the JSON key corresponding to the js key,
DataType: the data type of the JSON key corresponding to the js key,
Options: represents the additional properties of the JSON key corresponding to js key.
The process of the adapter module generating the SQL statement is as follows:
Traversing each key and value of the data model, judging what type the value of the corresponding JSON data is, recording the corresponding type, and then taking out the value of the corresponding JSON data according to the attribute of the key and value record of the data model;
All values and types of values of all JSONs are circularly fetched and spliced into SQL database statements.
The execution module calls uniapp an API that can execute the database statement, executes the SQL database statement generated by the adapter module, and inserts the data into the database.
According to another embodiment, there is provided a computer device including: one or more processors; a memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, which when executed by the processors implement a method for storing JSON data using SQL in a JS development environment as previously described.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
Finally, it should be noted that: the above embodiments are only for illustrating the technical aspects of the present invention and not for limiting the same, and although the present invention has been described in detail with reference to the above embodiments, it should be understood by those of ordinary skill in the art that: modifications and equivalents may be made to the specific embodiments of the invention without departing from the spirit and scope of the invention, which is intended to be covered by the claims.

Claims (3)

1. The method for storing the JSON data by using SQL under the JS development environment is characterized by comprising the following steps of:
Establishing a data model corresponding to the JSON data, and mapping the JSON data structure into the SQLite structure, wherein the method comprises the following steps: establishing a corresponding js object according to the JSON object, wherein each key of the js object represents a key after the key of the JSON object is converted into database data, each value corresponding to each key of the js object comprises three attributes, representing attributes corresponding to the key and the value of the JSON object, and the three attributes are as follows: transValue: names of JSON keys corresponding to keys representing js dataType: data types of JSON keys corresponding to js keys are represented, options: additional attributes of the JSON key corresponding to the js key;
According to the JSON data and the data model, generating the corresponding data into an SQL sentence comprises: traversing each key and value of the data model, judging what type the value of the corresponding JSON data is, recording the corresponding type, and then taking out the value of the corresponding JSON data according to the attribute of the key and value record of the data model; circularly taking out all values and types of values of all JSONs, and splicing the values and the types of the values into SQL database sentences;
The generated SQL statement is executed by calling the interface specification of the uni-app to insert the data into the database.
2. A system for storing JSON data using SQL in a JS development environment, comprising:
The data model module is used for establishing a data model corresponding to the JSON data and mapping the JSON data structure into the SQLite structure;
The adapter module is used for generating SQL sentences from the corresponding data according to the JSON data and the data model;
the execution module is used for executing the generated SQL sentence by calling the interface specification of the uni-app and inserting the data into the database;
the process of establishing the model by the data model module is as follows:
establishing a corresponding js object according to the JSON object, wherein each key of the js object represents a key after the key of the JSON object is converted into database data, each value corresponding to each key of the js object comprises three attributes, representing attributes corresponding to the key and the value of the JSON object, and the three attributes are as follows:
transValue: the name of the JSON key corresponding to the js key,
DataType: the data type of the JSON key corresponding to the js key,
Options: additional attributes of the JSON key corresponding to the js key;
The process of the adapter module generating the SQL statement is as follows:
Traversing each key and value of the data model, judging what type the value of the corresponding JSON data is, recording the corresponding type, and then taking out the value of the corresponding JSON data according to the attribute of the key and value record of the data model;
All values and types of values of all JSONs are circularly fetched and spliced into SQL database statements.
3. A computer device, the device comprising:
one or more processors;
A memory; and
One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, which when executed by the processor implement the method for storing JSON data using SQL in a JS development environment as set forth in claim 1.
CN202110985601.2A 2021-08-25 2021-08-25 Method and system for storing JSON data by using SQL under JS development environment Active CN113742361B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110985601.2A CN113742361B (en) 2021-08-25 2021-08-25 Method and system for storing JSON data by using SQL under JS development environment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110985601.2A CN113742361B (en) 2021-08-25 2021-08-25 Method and system for storing JSON data by using SQL under JS development environment

Publications (2)

Publication Number Publication Date
CN113742361A CN113742361A (en) 2021-12-03
CN113742361B true CN113742361B (en) 2024-05-28

Family

ID=78733059

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110985601.2A Active CN113742361B (en) 2021-08-25 2021-08-25 Method and system for storing JSON data by using SQL under JS development environment

Country Status (1)

Country Link
CN (1) CN113742361B (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108984541A (en) * 2017-05-31 2018-12-11 陈瑞 A kind of Object Relation Mapping method and device based on object data model
CN110442331A (en) * 2019-08-11 2019-11-12 西藏宁算科技集团有限公司 It is a kind of to automate the method and system for building code skeleton
CN111125215A (en) * 2019-12-06 2020-05-08 厦门天锐科技股份有限公司 Method capable of configuring JSON to convert database
CN111291074A (en) * 2020-02-27 2020-06-16 北京思特奇信息技术股份有限公司 Database query method, system, medium and device
CN112799670A (en) * 2021-04-14 2021-05-14 盛威时代科技集团有限公司 Method and system for unified multi-terminal logic development
CN112799966A (en) * 2021-03-29 2021-05-14 广州嘉为科技有限公司 Method, system, equipment and medium for generating test data in batches by extensible plug-in

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108984541A (en) * 2017-05-31 2018-12-11 陈瑞 A kind of Object Relation Mapping method and device based on object data model
CN110442331A (en) * 2019-08-11 2019-11-12 西藏宁算科技集团有限公司 It is a kind of to automate the method and system for building code skeleton
CN111125215A (en) * 2019-12-06 2020-05-08 厦门天锐科技股份有限公司 Method capable of configuring JSON to convert database
CN111291074A (en) * 2020-02-27 2020-06-16 北京思特奇信息技术股份有限公司 Database query method, system, medium and device
CN112799966A (en) * 2021-03-29 2021-05-14 广州嘉为科技有限公司 Method, system, equipment and medium for generating test data in batches by extensible plug-in
CN112799670A (en) * 2021-04-14 2021-05-14 盛威时代科技集团有限公司 Method and system for unified multi-terminal logic development

Also Published As

Publication number Publication date
CN113742361A (en) 2021-12-03

Similar Documents

Publication Publication Date Title
CN110968601A (en) Data query processing method and device
CN112765023A (en) Test case generation method and device
US10678514B2 (en) Method and device for generating code assistance information
CN112187713B (en) Message conversion method, device, computer equipment and storage medium
CN108920566B (en) Method, device and equipment for operating SQLite database
CN109032612B (en) Interface calling method and device of hybrid application and computer readable storage medium
CN110941655A (en) Data format conversion method and device
CN113703862A (en) Configuration-based interface calling method, device, equipment and storage medium
CN111274144B (en) Unit testing method and system based on network file system operation word interface
CN117785723A (en) Dynamic interface parameter association method and device and electronic equipment
CN113742361B (en) Method and system for storing JSON data by using SQL under JS development environment
CN109474822B (en) Android television multi-language automatic testing method and device
CN116432185B (en) Abnormality detection method and device, readable storage medium and electronic equipment
CN112181951A (en) Heterogeneous database data migration method, device and equipment
CN107451050B (en) Function acquisition method and device and server
CN116414689A (en) Interface parameter verification method and system based on reflection mechanism
CN113176877B (en) Entity class generation method, device and storage medium
CN114579466A (en) Method, device, equipment and medium for constructing test case and code test
CN112418930B (en) Test method, system and computer equipment
CN112416362B (en) PDK compiling function implementation method
CN111538714B (en) Instruction execution method and device, electronic equipment and storage medium
EP1183596B1 (en) Generating optimized computer data field conversion routines
US20240281737A1 (en) Data compilation and execution device and data compilation and execution method
CN116881309A (en) Data operation method and device based on SQLite database
CN114416090A (en) Conversion method and device for single-page application framework Vue component

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right
TA01 Transfer of patent application right

Effective date of registration: 20230406

Address after: 851414 11th Floor, Liuwu Building, Liuwu New District, Lhasa, Tibet Autonomous Region

Applicant after: Tibet ningsuan Technology Group Co.,Ltd.

Address before: Floor 11, building A1, Huizhi Science Park, No. 8, Hengtai Road, Nanjing Economic and Technological Development Zone, Nanjing, Jiangsu 210033

Applicant before: DILU TECHNOLOGY Co.,Ltd.

GR01 Patent grant
GR01 Patent grant