WebAPI interface test database operations

We are generally not recommended to directly view the contents of the database to check the function, but in the absence of external interface or GUI verification can only be verified by querying the database.

For example, we need to manually add a million data from the interface, it is estimated will take several days, obviously can not go manual operation. Then there is a method: direct operation of the database, inserting data into a table.

In general, our application and database services are not on a single machine, so it is necessary to access the network request.

Server:

       Database service process: responsible for network communication, data transfer, database access pass commands to the DB.

       DB: corresponds to a specific database, usually forms as a database file on disk, internal storage database information.

Client:

       APP: application to access the database,

       Driver: APP China Unicom is responsible for communication between the database server, the equivalent of a bridge.

Python code database operation:

       pymysql python3.x bank is connected to a version for library mysql server; MySQLdb library is used in the python2.

       In windows platform, direct pip install PyMySQL

       First use third-party tools can be connected to a database, or some of the information requested to inform the development of connections to the database. For example: host name or IP, user name, password and port.

       So that you can connect to the database of:

              ① First import package import pymysql

              ① create a connection object db = pymysql.connect (host = '127.0.0.1', # hostname or IP address

                                                                        port = 3306, # generally the default port 3306

                                                                        user = 'xxxx', # username

                                                                        password = 'xxxx', # password

                                                                        db = 'xxxx') # database name

              ② using the cursor () to create a cursor object c = db.cursor ()

              ③ CRUD database content ---- sql statement is executed through the cursor

                      c.execute ( "select * from sq_course") # query

                      c.execute ( "insert into sq_course (name,` desc`, display_idx) ") values ​​( 'mathematics', 'math', 10) # increased sentence

                      c.execute ( "update sq_course set name = 'language' where name = 'math' ') # modification statements

                      c.execute ( "delete from sq_course where name = 'math' ') # delete statement 

              Note: All modifications to the database of the action, you must commit to submit to take effect

                         rollback () to roll back the current transaction and conmit opposite can be understood as meaning the withdrawal of

              ④ The data can be taken out to the query by the following method:

                      c.fetchall () # to query all data

                      c.fetchone () # a query data

                      c.fetchmany (3) # query multiple data parameters is how many

               ⑤ close the database connection db.close

Code examples:

         

Guess you like

Origin www.cnblogs.com/peipei-Study/p/12036345.html