Disclosure of Invention
In view of this, the embodiments of the present invention provide a database query method and apparatus, which can be applied to database query of multiple systems.
In a first aspect, an embodiment of the present invention provides a database query method, including:
Receiving a database query statement input by a user, wherein the database query statement corresponds to a target table;
acquiring system information of a current operating system;
Compiling and executing the database query statement according to the system information so as to acquire target record data from the target table;
converting the value of a target field in the target record data into a preset type;
Determining a preset number of current record data from the target record data after the conversion type;
and displaying the preset number of current record data in a preset display format.
Optionally, after displaying the preset number of current recorded data in a preset display format, the method further includes:
receiving a display instruction aiming at the target record data;
Responding to the display request, and determining the follow-up record data of the current record data from the target record data after the conversion type, wherein the number of the follow-up record data is the preset number;
and displaying the follow-up record data in the preset display format.
Optionally, after the executing the database query statement, the method further includes:
Acquiring the execution time of executing the database query statement and the record statistical information of the target record data;
and displaying the execution time and the record statistical information.
Optionally, after the executing the database query statement, the method further includes:
receiving a save instruction aiming at the target record data, wherein the save instruction comprises save path information;
and responding to the save instruction, generating a text file aiming at the target record data, and saving the text file in a path corresponding to the save path information.
Optionally, after receiving the database query statement input by the user, the method further includes:
receiving a database operation instruction input by a user;
determining database rights of the user;
Determining whether the user has permission to execute the database operation instruction according to the database permission;
executing the database operation instruction under the condition that the user has permission to execute the database operation instruction, and returning an execution result corresponding to the database operation instruction to the user;
and sending prompt information of execution failure to the user under the condition that the user has no authority to execute the database operation instruction.
Optionally, after receiving the database query statement input by the user, the method further includes:
and acquiring and displaying the resource use condition of the database object, wherein the database object comprises at least one of a database query statement, a database operation statement, a database related program and a database index.
Optionally, after receiving the database query statement input by the user, the method further includes:
receiving a statement checking instruction sent by a user;
Acquiring and displaying a plurality of historical database operation sentences;
receiving a selected instruction for the historical database operation statement;
Selecting a target database operation statement corresponding to the selected instruction from the plurality of historical database operation statements;
And displaying and/or executing the target database operation statement according to the statement selection instruction.
Optionally, the receiving the database query statement input by the user includes:
Receiving a database query instruction input by a user, wherein the database query instruction corresponds to at least one target field in the target table;
and generating the database query statement according to the target table and the at least one target field.
In a second aspect, an embodiment of the present invention provides a database query apparatus, including:
The sentence receiving module is used for receiving a database query sentence input by a user, wherein the database query sentence corresponds to a target table;
The execution module is used for acquiring the system information of the current operating system;
Compiling and executing the database query statement according to the system information so as to acquire target record data from the target table;
the conversion module is used for converting the value of the target field in the target record data into a preset type;
The data determining module is used for determining a preset number of current record data from the target record data after the conversion type;
and the display module is used for displaying the preset number of current record data in a preset display format.
Optionally, the display module is further configured to:
receiving a display instruction aiming at the target record data;
Responding to the display request, and determining the follow-up record data of the current record data from the target record data after the conversion type, wherein the number of the follow-up record data is the preset number;
and displaying the follow-up record data in the preset display format.
Optionally, the display module is further configured to:
Acquiring the execution time of executing the database query statement and the record statistical information of the target record data;
and displaying the execution time and the record statistical information.
Optionally, the display module is further configured to:
Acquiring the execution time of executing the database query statement and the record statistical information of the target record data;
and displaying the execution time and the record statistical information.
In a third aspect, an embodiment of the present invention provides an electronic device, including:
One or more processors;
storage means for storing one or more programs,
The one or more programs, when executed by the one or more processors, cause the one or more processors to implement the methods of any of the embodiments described above.
In a fourth aspect, embodiments of the present invention provide a computer readable medium having stored thereon a computer program which, when executed by a processor, implements a method as described in any of the above embodiments.
In a fifth aspect, embodiments of the present invention provide a computer program product comprising a computer program which, when executed by a processor, implements a method as described in any of the above embodiments.
The method has the advantages that the database query statement input by the user is received, and the query statement is compiled and executed through the system information of the current operating system, so that the method of the embodiment of the invention can be operated in different systems, and the function of multi-system support is realized.
In addition, a database query statement input by the user is executed, and target record data is acquired from the target table. The preset type is the display type of the target field in the target record data, the preset number is the display number of the target record data, and the preset display format is used for controlling the display form of the target record data. The preset types, the preset numbers and the preset display formats can be set according to specific requirements, so that the query results of the database can be displayed flexibly, and convenience is brought to the use of the database.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
The technical scheme of the application obtains, stores, uses, processes and the like the data, which all meet the relevant regulations of national laws and regulations.
The embodiment of the invention provides a database query method which can bring convenience to technical developers. Fig. 1 is a flow chart of a database query method according to a first embodiment of the present invention, as shown in fig. 1, where the method includes:
Step 101, receiving a database query statement input by a user, wherein the database query statement corresponds to a target table.
The method of the embodiment of the invention can be applied to various databases, such as Oracle, mysql, sqlServer or NoSQL, etc. The database query statement is used for querying the related data record corresponding to the database query statement from the target table.
In one embodiment of the invention, receiving a database query statement input by a user comprises receiving a database query instruction input by the user, wherein the database query instruction corresponds to at least one target field in a target table, and generating the database query statement according to the target table and the at least one target field.
Multiple tables in the database and multiple fields in the tables may be visually presented on the interface. The user may select a target table from a plurality of tables and select a target field from a plurality of fields in the target table. The system automatically generates a data query statement according to the target table and the target field. In addition, the user can input screening conditions based on the target field, such as field 1= "men", field 2= "abc", and the like, and the system automatically generates a data query statement according to the target table, the target field and the screening conditions.
Step 102, acquiring system information of the current operating system, compiling and executing a database query statement according to the system information to acquire target record data from a target table.
The current operating system may include linux, unix, ubuntu, centos, windows or the like. The bottom language of the scheme of the embodiment of the invention can be C language, can be recompiled under multiple systems, and can support the use of different systems.
And executing the database query statement in the target database to acquire target record data corresponding to the database query statement from a target table in the target database. The target record data may have one or more pieces. Target record data corresponding to the database query statement may not exist, and prompt information of no query result is returned to the user.
And 103, converting the value of the target field in the target record data into a preset type.
The preset type is a presentation type of a target field in target record data. The user can set the preset type of each target field according to the requirement. For example, the target field is of a numeric type, and the preset type may be set to a character type or a date type. The target field is of boolean type, and the preset type may be set to a numeric type or a character type. The value of the target field is converted into the preset type, so that the target record data can be displayed more flexibly according to the intention of a user.
Step 104, determining a preset number of current record data from the target record data after the conversion type.
The preset number is the display number of the target record data. The user can set the preset number as desired. The preset number may be 5, 10, 20, etc. The current record data is the target record data which needs to be displayed currently.
The method comprises the steps of displaying a preset number of current recorded data in a preset display format, receiving a display instruction aiming at target recorded data, responding to a display request, determining follow-up recorded data of the current recorded data from the target recorded data after the conversion type, wherein the number of the follow-up recorded data is the preset number, and displaying the follow-up recorded data in the preset display format.
For example, if there are 56 entries of the record data in total and the preset number is 10, the 56 entries of the record data can be divided into 6 groups, namely, 1 st to 10 th entries of the record data, 11 th to 20 th entries of the record data. And taking the first group of target record data as current record data, and displaying the current record data on an interface. And after receiving the display instruction, taking the second group of target record data as current record data, and displaying the current record data on an interface. And so on until all the target record data are displayed.
The presentation instructions may be entered in a variety of ways, such as by carriage return, clicking a left mouse button, clicking a designated key such as F1, F2, etc., or directly clicking a presentation button in a presentation interface.
Step 105, displaying the preset number of current record data in a preset display format.
The preset display format is used for controlling the display form of the target record data. The user can set the display format as desired. The presentation format may include the font, font size, size of each target field, how each target field is aligned, the number of bytes occupied by each target field, and so forth.
In the development process of the database application program, accessing the database object and writing the SQL program is a tedious and time-consuming task, daily management of the database can be completed by a plurality of SQL scripts, after the command line execution database is connected with the database by the self-contained tool mysql or sqlplus, the fields and the query result are often difficult to correspond one to one, and the historical SQL sentences cannot be traced back, so that great trouble and inconvenience are brought to technical developers.
In the embodiment of the invention, a database query statement input by a user is executed, and target record data is obtained from a target table. The preset type is the display type of the target field in the target record data, the preset number is the display number of the target record data, and the preset display format is used for controlling the display format of the target record data. The preset types, the preset numbers and the preset display formats can be set according to specific requirements, so that the query results of the database can be flexibly displayed, and convenience is brought to the application of the database. Specifically, by referring to the header file of the database, executing an instruction to obtain the field name of the target table, performing data type conversion on each field of the query result, and finally outputting the field name and the corresponding value. And the counter is used for limiting each page to only output the preset number of item mark record data, and the carriage returns to change pages.
In addition, a database query statement input by a user is received, and the query statement is compiled and executed through the system information of the current operating system, so that the method of the embodiment of the invention can be operated in different systems to realize the function of multi-system support.
In addition, after the database query statement is input, each record of the output page can display the target field name and the corresponding value, the number of the query strips is controlled at the preset number each time, and each display instruction input can display a plurality of preset number strips, so that the continuous screen brushing caused by too many query results is avoided.
In one embodiment of the invention, after the database query statement is executed, the method further comprises the steps of obtaining the execution time of the database query statement and the record statistics of the target record data, and displaying the execution time and the record statistics. The record statistics are relevant statistics of the target record data. The record statistics may include the total number of target record data, space usage, storage information, etc. And after each query, the execution time and the record statistics information are displayed, so that a developer can evaluate the query statement performance conveniently.
In one embodiment of the invention, after executing the database query statement, the method further comprises the steps of receiving a save instruction for target record data, wherein the save instruction comprises save path information, responding to the save instruction, generating a text file for the target record data, and saving the text file in a path corresponding to the save path information.
Specifically, when it is monitored that the beginning of a sentence input by a user contains keywords such as storage, the method enters an export function, and also refers to a header file of a database, data type conversion is performed on each field of a query result, then all the query results are written into a file designated by the user, and separators can be set according to requirements. The method of the embodiment of the invention can export the query result into the text file, store the text file locally and support the self definition of the separator of the file.
In one embodiment of the invention, after receiving the database query statement input by the user, the method further comprises the step of acquiring and displaying the resource use condition of the database object, wherein the database object comprises at least one of the database query statement, the database operation statement, a database related program and a database index.
The system can encapsulate SQL statement sets with various resource usage orders, can list SQL statements with the most resources occupied by the current database in a descending order, is convenient for developers to adjust SQL execution performance, supports execution plans and analyzes various index performances.
The method provided by the embodiment of the invention can help related personnel analyze the database performance. If an execution plan is detected, keywords such as explain for are output, the execution plan of the sentence following the keywords is output. Detecting a key such as show tablespace in the tablespace will output the space usage of the current database. Detecting a lock, such as a lock, outputs the SQL statement that is currently in deadlock.
Fig. 2 is a flowchart of a database operation method according to a second embodiment of the present invention. As shown in fig. 2, the method includes:
step 201, receiving a database operation instruction input by a user.
Database operation instructions are instructions for operating a database object. Database objects may include databases, tables, fields, indexes, tablespaces, and the like. The database operation instructions may include instructions to create a delete table, instructions to add or delete a modified data record, instructions to create a delete index, and the like.
Step 202, determining database rights of the user.
The database rights are used to define the rights of database operations that a user may perform. The database rights include having login rights, having super user rights, having database creation rights, having role creation rights, and the like.
Step 203, determining whether the user has authority to execute the database operation instruction according to the database authority.
And 204, executing the database operation instruction under the condition that the user has permission to execute the database operation instruction, and returning an execution result corresponding to the database operation instruction to the user.
And step 205, sending prompt information of failure execution to the user under the condition that the user has no authority to execute the database operation instruction.
Prior to step 201, a step of assigning different database rights to different users may also be included. Receiving a database permission allocation instruction from a user with database management permission or super user permission, wherein the database permission allocation instruction comprises a target database permission and an allocation user, and determining the database permission of the allocation user as the target database permission.
The scheme of the embodiment of the invention provides a permission control function, if the function is opened, when keywords such as modification (update), deletion (delete), insertion (insert) and the like are contained in a statement input by a user, the execution is automatically refused, and the user is prompted that the execution cannot be performed due to permission problems. And shielding part of rights, such as in a production environment, can shield sentences of modification, insertion, DDL and the like, so that a user only has query rights and does not modify rights. In order to ensure the operation safety of the database, different authorities are allocated for different users, and after receiving the database operation instruction, the user sending the database operation instruction is checked to have the authority required by the database operation instruction, so that the safety of the production environment and the database is ensured.
Fig. 3 is a flowchart of a database operation method according to a third embodiment of the present invention. As shown in fig. 3, the method includes:
step 301, receiving a statement check instruction sent by a user.
Statement view instructions are used to view historical database operation statements of a database. The condition field in the statement view instruction may be set according to specific needs. For example, a target table may be included in the statement view instruction, where the statement view instruction is used to view historical database operation instructions of the target table. The statement checking instruction can comprise a starting time and an ending time, and the statement checking instruction is used for checking historical database operation instructions in the database from the starting time to the ending time.
Step 302, acquiring and displaying a plurality of historical database operation sentences.
Step 303, receiving a selected instruction for a historical database operation statement.
The selected instruction is used to select a target database operation statement from the historical database operation statements. The selected instruction can be input in a mode of carriage return, left click of a mouse, specific case clicking and the like, or direct clicking of a certain database operation sentence in the display interface and the like.
Step 304, selecting a target database operation statement corresponding to the selected instruction from a plurality of historical database operation statements.
Step 305, displaying and/or executing the target database operation statement according to the statement selection instruction.
The method provided by the embodiment of the invention can display all the database operation sentences which are executed currently, and is convenient for developers to quickly edit and test by utilizing the historical database operation sentences. Specifically, the array may be utilized to save SQL statements that the user executes during the history period, and then monitor selected instructions entered by the user. The user may enter the selected instruction in a variety of ways, such as a mouse click, a designated key, etc. For example, the input of the user is received, the last SQL sentence is reflected if the letter l, all SQL sentences which are currently executed by the user are reflected if the letter ll, and the last SQL sentence is re-executed if the letter ll.
The scheme of the embodiment of the invention provides functions of formatting output, data export, authority control, history backtracking, performance analysis and the like aiming at the command line operation database, can help developers to easily and simply develop and rapidly manage the change database, and promotes the realization of higher-level code quality, performance and maintainability.
Fig. 4 is a schematic structural diagram of a database query apparatus according to an embodiment of the present invention, as shown in fig. 4, where the apparatus includes:
a sentence receiving module 401, configured to receive a database query sentence input by a user, where the database query sentence corresponds to a target table;
the execution module 402 is used for acquiring the system information of the current operating system, compiling and executing the database query statement according to the system information so as to acquire target record data from the target table;
a conversion module 403, configured to convert a value of a target field in the target record data into a preset type;
The data determining module 404 is configured to determine a preset number of current record data from the target record data after the conversion type;
and the display module 405 is configured to display the preset number of current record data in a preset display format.
Optionally, the display module 405 is further configured to:
receiving a display instruction aiming at the target record data;
Responding to the display request, and determining the follow-up record data of the current record data from the target record data after the conversion type, wherein the number of the follow-up record data is the preset number;
and displaying the follow-up record data in the preset display format.
Optionally, the display module 405 is further configured to:
Acquiring the execution time of executing the database query statement and the record statistical information of the target record data;
and displaying the execution time and the record statistical information.
Optionally, the apparatus further comprises:
The storage module is used for receiving a storage instruction aiming at the target record data, wherein the storage instruction comprises storage path information;
and responding to the save instruction, generating a text file aiming at the target record data, and saving the text file in a path corresponding to the save path information.
Optionally, the apparatus further comprises:
the right item judging module is used for receiving a database operation instruction input by a user;
determining database rights of the user;
Determining whether the user has permission to execute the database operation instruction according to the database permission;
executing the database operation instruction under the condition that the user has permission to execute the database operation instruction, and returning an execution result corresponding to the database operation instruction to the user;
and sending prompt information of execution failure to the user under the condition that the user has no authority to execute the database operation instruction.
Optionally, the display module 405 is further configured to:
and acquiring and displaying the resource use condition of the database object, wherein the database object comprises at least one of a database query statement, a database operation statement, a database related program and a database index.
Optionally, the apparatus further comprises:
the operation module is used for receiving statement checking instructions sent by a user;
Acquiring and displaying a plurality of historical database operation sentences;
receiving a selected instruction for the historical database operation statement;
Selecting a target database operation statement corresponding to the selected instruction from the plurality of historical database operation statements;
And displaying and/or executing the target database operation statement according to the statement selection instruction.
Optionally, the sentence receiving module 401 is specifically configured to:
Receiving a database query instruction input by a user, wherein the database query instruction corresponds to at least one target field in the target table;
and generating the database query statement according to the target table and the at least one target field.
The embodiment of the invention provides electronic equipment, which comprises:
One or more processors;
storage means for storing one or more programs,
When the one or more programs are executed by the one or more processors, the one or more processors are caused to implement the methods of any of the embodiments described above.
An embodiment of the present invention provides a computer program product comprising a computer program which, when executed by a processor, implements a database query method in an embodiment of the present invention.
Referring now to FIG. 5, there is illustrated a schematic diagram of a computer system 500 suitable for use in implementing an embodiment of the present invention. The terminal device shown in fig. 5 is only an example, and should not impose any limitation on the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 5, the computer system 500 includes a Central Processing Unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 502 or a program loaded from a storage section 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the system 500 are also stored. The CPU 501, ROM 502, and RAM 503 are connected to each other through a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
Connected to the I/O interface 505 are an input section 506 including a keyboard, a mouse, and the like, an output section 507 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, a speaker, and the like, a storage section 508 including a hard disk, and the like, and a communication section 509 including a network interface card such as a LAN card, a modem, and the like. The communication section 509 performs communication processing via a network such as the internet. The drive 510 is also connected to the I/O interface 505 as needed. A removable medium 511 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 510 as needed so that a computer program read therefrom is mounted into the storage section 508 as needed.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 509, and/or installed from the removable media 511. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 501.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of a computer-readable storage medium may include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, and may be described as, for example, a statement receiving module, an executing module, a converting module, a data determining module, and a presentation module. Where the names of the modules do not constitute a limitation on the module itself in some cases, for example, the sentence receiving module may also be described as "receiving a database query sentence entered by a user, the database query sentence corresponding to a module of the target table".
As a further aspect, the invention also provides a computer readable medium which may be comprised in the device described in the above embodiments or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to include:
Receiving a database query statement input by a user, wherein the database query statement corresponds to a target table;
acquiring system information of a current operating system;
Compiling and executing the database query statement according to the system information so as to acquire target record data from the target table;
converting the value of a target field in the target record data into a preset type;
Determining a preset number of current record data from the target record data after the conversion type;
and displaying the preset number of current record data in a preset display format.
According to the technical scheme of the embodiment of the invention, the database query statement input by the user is received, and the query statement is compiled and executed through the system information of the current operating system, so that the method of the embodiment of the invention can be operated in different systems to realize the function of multi-system support.
In addition, a database query statement input by the user is executed, and target record data is acquired from the target table. The preset type is the display type of the target field in the target record data, the preset number is the display number of the target record data, and the preset display format is used for controlling the display format of the target record data. The preset types, the preset numbers and the preset display formats can be set according to specific requirements, so that the query results of the database can be flexibly displayed, and convenience is brought to the application of the database.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.