A database interface and interactive script generation method and system
By generating Python scripts that generate database table structure information, creating API and database interaction scripts, the problem of low efficiency in database operation and interface development in web back-end development is solved, and an efficient and pluggable coding solution is realized.
Patent Information
- Application Number
- CN202111539071.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-15
- Publication Date
- 2025-05-02
- Estimated Expiration
- 2041-12-15
AI Technical Summary
In web backend development, when adding, deleting, modifying, checking database tables and providing APIs to front-end calls, there are a lot of cumbersome and repetitive coding work, resulting in low code development efficiency and easy to cause functional modules to be unable to be used normally due to human negligence.
By obtaining database table structure information, creating Python script files, referring to the built-in library of time, declaring relevant variables that change in the database table, and assigning values according to the passed parameters, creating API file total multiplexing templates and database atomic operations total multiplexing templates, and receiving and saving these templates through variables.
It reduces the workload of manual coding, improves coding efficiency, avoids errors caused by human factors, and the interactive scripts are pluggable and compatible with any Python-based FastAPI framework, does not affect the previous code structure and is not coupled.
Smart Images

Figure CN114416162B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the computer field, and in particular relates to a database interface and interactive script generation method and system. Background Art
[0002] In the computer field, web backend development is a very important part of web service development. However, during the development process, there will be many operations such as adding, deleting, modifying, and checking database tables, as well as providing corresponding APIs for front-end calls. During the entire coding process, there will be a lot of tedious and repetitive coding work, resulting in low code development efficiency, and some functional modules may not be used normally due to human negligence. Summary of the invention
[0003] In view of this, an embodiment of the present invention provides a method and system for generating a database interface and an interactive script, which are used to solve the problem of low efficiency in database operation and interface development.
[0004] In a first aspect of an embodiment of the present invention, a method for generating a database interface and an interactive script is provided, comprising:
[0005] Get database table structure information;
[0006] Create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables;
[0007] Call the file operation function, create an API reuse template and an atomic operation reuse template, write variable data and save it.
[0008] In a second aspect of an embodiment of the present invention, a database interface and interactive script generation system is provided, including:
[0009] Acquisition module, used to obtain database table structure information;
[0010] A creation module is used to create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables;
[0011] Generate a saving module to call the file operation function, create an API reuse template and an atomic operation reuse template to write variable data and save it.
[0012] In a third aspect of an embodiment of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method described in the first aspect of the embodiment of the present invention when executing the computer program.
[0013] In a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method provided in the first aspect of the embodiment of the present invention are implemented.
[0014] In the embodiment of the present invention, the external API and database interaction script of the FastAPI framework are automatically generated based on Python programming and database tables, which can reduce the workload of manual coding, improve coding efficiency, and effectively avoid human errors. At the same time, the interactive script can be used in a plug-in manner, and has strong compatibility with any FastAPI framework based on Python, without affecting the previous code structure and without coupling. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0016] Figure 1 A flowchart of a database interface and interactive script generation method provided by an embodiment of the present invention;
[0017] Figure 2 A schematic diagram of the structure of a database interface and interactive script generation system provided by one embodiment of the present invention;
[0018] Figure 3 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0019] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0020] It should be understood that the term "including" and other similar expressions in the specification or claims of the present invention and the above-mentioned drawings are intended to cover non-exclusive inclusions, such as a process, method, system, or device including a series of steps or units is not limited to the listed steps or units. In addition, "first" and "second" are used to distinguish different objects, not to describe a specific order.
[0021] See also Figure 1 , a flowchart of a database interface and interactive script generation method provided by an embodiment of the present invention includes:
[0022] S101, obtaining database table structure information;
[0023] The database table is usually a collection of two-dimensional data, used to represent and store the relationship between data objects, and is generally composed of vertical columns and horizontal rows. The number of columns is generally fixed in advance and identified by column names.
[0024] The database table structure information at least includes the data in each column of the database table, the data type of the column, the unique auto-increment primary key ID, the identification field of the class name, and the function field.
[0025] In addition to the data in each column and its data type, the database table also includes a unique self-incrementing primary key ID, a unique and non-repeating class name identification field, and other functional fields, which are used for adding, deleting, modifying, and querying operations and provide corresponding interfaces.
[0026] S102, create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables;
[0027] Python is a commonly used scripting language. In this embodiment, an external API interface can be provided based on Python scripts, and external database operations (atomic operations) can be provided.
[0028] The Python script file includes referencing the time built-in library, configuring parameters, and creating a reused template. The time built-in library is used to record timestamps, and the relevant variables of the database table changes and the parameters passed in are assigned values through parameter configuration declarations, and the reused template is created to provide external APIs and database interaction operations.
[0029] For example, the table_name variable is assigned the name of the database table, the desc variable is assigned the description, the have_name variable is assigned the field used to check whether the record exists in the database table, generally a unique and non-repeating identifier of the class name, the all_column variable is assigned an array of all columns of the database table except the unique primary key ID column, and the all_data_type variable is assigned the data type that corresponds to all_column.
[0030] Among them, through dynamic loops, the column data and column data types in the database table are assembled into an external API that conforms to the FastAPI framework according to predetermined rules, and a data template for atomic operations that interact with the database is generated; the data template is passed into the database atomic operation total reuse template.
[0031] That is, all_column and all_data_type will be dynamically assembled according to the rules through the for loop into data templates that conform to the FastAPI generation of external APIs and the methods of adding, deleting, modifying, and querying that interact with the database. Then these data templates are passed into the total reuse template as variables for use.
[0032] Among them, the new feature of Python 3.6 version f-string is used to add f in front of the string (starting with f means that Python expressions in curly brackets are supported in the string) to create the API file total reuse template and the database addition, deletion, modification and query total reuse template, which has better readability and facilitates script maintenance. The auto_api_text variable is used to receive the API file total reuse template, and the auto_sql_text variable is used to receive the database operation total reuse template. The total reuse template is mainly the code that conforms to the FastAPI coding rules, expressed in the form of a string.
[0033] Preferably, the API file total reuse template and the database atomic operation total reuse template are codes that comply with FastAPI encoding rules and are expressed in the form of strings.
[0034] S103, calling the file operation function, creating an API reuse template and an atomic operation reuse template, writing variable data and saving them.
[0035] In Python, the file operation function is the with open() function, through which a reuse template, namely an API reuse template and a database operation reuse template (atomic operation) can be created, and the total reuse template data received by the variable in S102 can be written.
[0036] For example, call the python with open() function to create 'database table name + _api.py', create the 'database table name + db.py' file, write auto_api_text into the 'database table name + _api.py' file, save it, write auto_sql_text into the 'database table name + _api.py' file, and save the reuse template.
[0037] It can be understood that the script file in this embodiment can be used in a pluggable manner, has strong compatibility with any python-based FastAPI framework, is plug-and-play, does not affect the previous code structure, and has no coupling.
[0038] It can automatically generate commonly used related interfaces: data paging query interface, data addition query interface, data modification query interface, data status modification interface, data deletion interface, and data drop-down interface; it can automatically generate commonly used database query related operations: add data, modify data, modify status, delete data, and query data by conditions.
[0039] In this embodiment, the external API and database interaction scripts of the FastAPI framework are automatically generated based on python and database tables, which can reduce the workload of manual coding, improve coding efficiency, and avoid errors caused by human factors.
[0040] It should be understood that the serial numbers of the steps in the above embodiments do not imply a sequence of execution, and the execution sequence of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0041] Figure 2 A schematic diagram of the structure of a database interface and interactive script generation system provided by an embodiment of the present invention, the system comprising:
[0042] The acquisition module 210 is used to acquire database table structure information;
[0043] The database table structure information at least includes the data in each column of the database table, the data type of the column, the unique auto-increment primary key ID, the identification field of the class name, and the function field.
[0044] A creation module 220 is used to create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables;
[0045] Among them, through dynamic loops, the column data and column data types in the database table are assembled into an external API that conforms to the FastAPI framework according to predetermined rules, and a data template for atomic operations that interact with the database is generated; the data template is passed into the database atomic operation total reuse template.
[0046] Generate and save module 230, which is used to call file operation functions, create API reuse templates and atomic operation reuse templates, write variable data and save them.
[0047] Preferably, the API file total reuse template and the database atomic operation total reuse template are codes that comply with FastAPI encoding rules and are expressed in the form of strings.
[0048] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working process of the above-described system and module can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0049] Figure 3 1 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. The electronic device is used to generate a database interface and an interactive script under the FastAPI framework. Figure 3 As shown, the electronic device 3 of this embodiment includes: a memory 310, a processor 320 and a system bus 330, wherein the memory 310 includes an executable program 3101 stored thereon, and those skilled in the art can understand that Figure 3 The electronic device structure shown in the figure does not constitute a limitation of the electronic device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0050] Combine the following Figure 3 A detailed introduction to the various components of electronic equipment:
[0051] The memory 310 can be used to store software programs and modules, and the processor 320 executes various functional applications and data processing of the electronic device by running the software programs and modules stored in the memory 310. The memory 310 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device (such as cache data), etc. In addition, the memory 310 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0052] The memory 310 contains an executable program 3101 of the signboard extraction method, which can be divided into one or more modules / units, which are stored in the memory 310 and executed by the processor 320 to realize the generation of a database interface, i.e., an interactive script, etc. The one or more modules / units can be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program 3101 in the electronic device 3. For example, the computer program 3101 can be divided into an acquisition module, a creation module, and a generation and storage module.
[0053] The processor 320 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. By running or executing software programs and / or modules stored in the memory 310, and calling data stored in the memory 310, it performs various functions of the electronic device and processes data, thereby monitoring the overall status of the electronic device. Optionally, the processor 3520 may include one or more processing units; preferably, the processor 320 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, application programs, etc., and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 320.
[0054] The system bus 330 is used to connect the various functional components inside the computer, and can transmit data information, address information, and control information. Its types can be, for example, PCI bus, ISA bus, VESA bus, etc. The instructions of the processor 320 are transmitted to the memory 310 through the bus, and the memory 310 feeds back data to the processor 320. The system bus 330 is responsible for the data and instruction exchange between the processor 320 and the memory 310. Of course, the system bus 330 can also be connected to other devices, such as network interfaces, display devices, etc.
[0055] In the embodiment of the present invention, the executable program executed by the processor 320 included in the electronic device includes:
[0056] Get database table structure information;
[0057] Create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables;
[0058] Call the file operation function, create an API reuse template and an atomic operation reuse template, write variable data and save it.
[0059] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0060] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0061] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A database interface and interactive script generation method, characterized in that: include: Get database table structure information; Create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables; Wherein, the declaration of the relevant variables changed in the database table and the assignment of values to the relevant variables according to the input parameters include: Through dynamic loops, the column data and column data types in the database table are assembled into an external API that complies with the FastAPI framework according to predetermined rules, and a data template for atomic operations that interact with the database is generated; the data template is passed into the database atomic operation total reuse template; Call the file operation function, create an API reuse template and an atomic operation reuse template, write variable data and save it.
2. The method according to claim 1, characterized in that: The database table structure information at least includes data in each column of the database table, data type of the column, unique auto-increment primary key ID, identification field of the class name and function field.
3. The method according to claim 1, characterized in that The API file total reuse template and the database atomic operation total reuse template are codes that comply with FastAPI encoding rules and are expressed in string form.
4. A database interface and interactive script generation system, characterized in that: include: Acquisition module, used to obtain database table structure information; A creation module is used to create a Python script file, reference the time built-in library in the script file, declare the relevant variables that change in the database table, assign values to the relevant variables according to the input parameters, create an API file total multiplexing template and a database atomic operation total multiplexing template respectively, and receive the API file total multiplexing template and the database atomic operation total multiplexing template respectively through variables; Wherein, the declaration of the relevant variables changed in the database table and the assignment of values to the relevant variables according to the input parameters include: Through dynamic loops, the column data and column data types in the database table are assembled into an external API that complies with the FastAPI framework according to predetermined rules, and a data template for atomic operations that interact with the database is generated; the data template is passed into the database atomic operation total reuse template; Generate a saving module to call the file operation function, create an API reuse template and an atomic operation reuse template to write variable data and save it.
5. The system according to claim 4, characterized in that The database table structure information at least includes data in each column of the database table, data type of the column, unique auto-increment primary key ID, identification field of the class name and function field.
6. The system according to claim 4, characterized in that The API file total reuse template and the database atomic operation total reuse template are codes that comply with FastAPI encoding rules and are expressed in string form.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of a database interface and interactive script generation method as described in any one of claims 1 to 3 are implemented.
8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed, the steps of a database interface and interactive script generation method as described in any one of claims 1 to 3 are implemented.
Citation Information
Patent Citations
Real-time calculation and data interaction method and system and medium
CN112148270A
Database script generation method, system and equipment, storage medium and program product
CN113553337A