Test Development Series Python Development Mock Interface (II)

Source: Internet
Author: User

We have prepared the pre-development environment, we need to do a little bit of preparation, your account information is there, of course, there is a database, we are going to pay, deduct money, must be operating from the database, to update the data in the Account table, so we have to prepare the database, set up the Account table , initialize several account information in order to complete the mock interface.

Let's learn how Python operates a database, how to execute SQL, and a database using Sqlite,sqlite is a lightweight relational database that is open-source across platforms without the need for configuration and installation to be used directly, SQL statements and MySQL, server, Databases such as Oracle are the same.

First create a database, and then create the Account table, insert a few initialization data, here to use navicat this tool, or other SQLite visualizer can also, Navicat installation package connected to Http://pan.baidu.com/s/1eRArvAM , open navicat After the connection select SQLite, and then choose a new SQLite3 database, select a path, enter the database name to create a database, and then select the newly created database, new query, execute SQL can, see below.

Create a database

Execute SQL

The initialization of the database is ready, the following is to build a table and insert some initial data, or write interface when there is no data test, payment is definitely the operation of the Account table, so to create the Account table, there must be an account ID, user ID and the balance of the user three fields, the table structure is also designed, and then access a little data , to facilitate the use of the later self-test, SQL is as follows:

12345678910111213 --SQL for building tablesCREATE TABLE "Accounts" ( "account_id" INTEGER PRIMARY KEY autoincrement not NULL, "user_id" INTEGER, "Money" INTEGER );--SQL to insert dataINSERT into ' accounts ' ("user_id", "Money") VALUES (1, 1111.98); INSERT into ' accounts ' ("user_id", "Money") VALUES (2, 981); INSERT into ' accounts ' ("user_id", "Money") VALUES (8, 8888); INSERT into ' accounts ' ("user_id", "Money") VALUES (9, 889211.1); INSERT into ' accounts ' ("user_id", "Money") VALUES (19, 1.2);

Table structure is also created, the data also has, the following will use Python to manipulate the database, very simple, is to execute SQL, code as follows, each line is annotated

123456789101112131415161718192021 #s. PYimport sqlite3 #操作sqlite使用sqlite3模块, the above is the import moduledb = sqlite3. Connect(' my.db ') #连接数据库, specify the path to the database, and if the path to the database is under the same path as the Python file, write the name directly.#如果不在同一个路径的话, write the absolute path to the database file, such as the C:\Users\Sabri\Desktop\my.dbcourse = db. Cursor() #创建游标, it's the equivalent of creating a database of staff to work, and to change things from a database .course. Execute(' select * from Accounts; ' )#游标执行sql语句, the equivalent of having the worker you just created go execute the SQL statementres = course. Fetchall() #获取sql执行结果, save in Res, RES is the result of SQL executionPrint(res) #打印结果course. Close() #关闭游标db. Commit() #提交数据库事物, if it is an update, INSERT, DELETE statement that requires a commit to take effect, select does not needdb. Close() #做人有始有终, there is a turn off, close the database connection

This article we learn the simple use of sqlite and python How to operate SQLite, a little progress every day, go to try it, next we formally began to write interface

Test Development Series Python Development Mock Interface (II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.