What is a database? Is it difficult to operate the database with Python(19)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Welcome to learn Python with cat girl.

today's topic

Today we learn what is a database and how to operate a database with Python.

Database = data + library

Ku, a commonly used Chinese character (first-level character), read as kù, was first seen in the Warring States period, and it is both ideographic and pictophonetic.

The original meaning is the place where soldiers and other weapons are stored; later it generally refers to the place where various items are stored; it also refers to prisons.

A garage is a place to store your car.

Grain depots are places where grain is stored.

A reservoir is a place where water is stored.

A small vault is a place to store money.

What about the database?

Nature is where data is stored.

A database is "a warehouse that organizes, stores and manages data according to the data structure". It is a collection of large amounts of data that is stored in a computer for a long time, organized, shareable, and managed in a unified manner.

Smaller data may be placed in Notepad, word, ppt and other software, but what about large amounts of data?

Usually stored in the database.

The database is actually around us. If you think about it, you log in to a website or APP, enter the user name and password, and click Enter. If the information is correct, the login is successful, and if the information is incorrect, the login fails.

Where are millions, tens of millions, or even hundreds of millions of information stored?

This information is stored in the database, including user name, password, gender, address, product description, pictures and many other information.

How to instantly find the data you input among the massive data of millions, tens of millions, or hundreds of millions, and then judge whether it matches. Think about it, it’s technology!

Indeed, the database is a high technology.

common database

What are the common databases?

Let's study together.

Oracle should be the largest database company in the world, and generally very large-scale data will use oracle.

Microsoft's SQl Server, generally medium-scale data will use SQL Server.

Microsoft's commercial products have good compatibility with Microsoft SQL statements and high commercial maturity.

Microsoft Access, compact, easy to use, one of the office series.

MySQL software adopts a dual authorization policy, which is divided into community edition and commercial edition. Due to its small size, fast speed, low overall cost of ownership, especially the open source feature, MySQL is generally chosen as the website for the development of small, medium and large websites. database.

MySQL was acquired by Sun for US$1 billion in 2008, and Sun was acquired by Oracle on April 20, 2009.

SQLite, a lightweight database, is an ACID-compliant relational database management system contained in a relatively small C library.

Its design goal is embedded, and it has been used in many embedded products. It occupies very low resources, and may only need a few hundred K of memory.

It can support mainstream operating systems such as Windows/Linux/Unix, and can be combined with many programming languages, such as Tcl, C#, PHP, Java, etc., and has an ODBC interface. As far as the world famous database management system is concerned, its processing speed is faster than them.

MongoDB is a database based on distributed file storage. Written by C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications.

MongoDB is a product between relational databases and non-relational databases. It is the most functional among non-relational databases and most similar to relational databases.

Operate the database

The above briefly introduces so many databases, isn’t it a bit confusing?

For non-professionals, as long as they can use it simply.

Professionals have a lot to learn.

Let's use SQLite as an example today to learn the common operations of the database.

Other databases need to download and install software. We just understand briefly today, so we won’t download the installation package.

Python itself has built-in SQLite, no need to install, you can use it directly!

Database, data table, field, record, etc., we first have a general impression of these names.

For example, the Chinese scores, English scores, and math scores of Cat Girl's class.

First, there is a database, cat girl class grades.

There are data tables in the database. There are three tables here, Chinese scores, math scores, and English scores.

There are fields and records in the table, and the student number, name, and grade in the Chinese score are fields.

The student number is 01, the name is Maomei, and the score is 85, which means a record in the Chinese score sheet.

The student number does not allow duplicate values ​​and can be used as a primary key.

create database

  • 1 line: the sqlite3 module needs to be imported.

  • Line 4: You need to create a connection object with connnect first.

    If the database file lvye.db exists, connect to it.

    If it does not exist, create it and connect to it.

  • Line 6: create a cursor, the Chinese is a cursor, we all access the data table through the cursor, such as adding, deleting, modifying and checking.

  • Line 8: Execute sql statements. These statements create a table table, which contains id (primary key), name, and age.

    What is a primary key? It just cannot be repeated.

    For example, the ID number can be used as the primary key, but the name cannot.

    The id type is int, the name type is varchar, and the age type is int.

  • Line 12: Close the cursor.

  • Line 14: close the connection.

If you run it again, the following error will be reported:

This is because the student table already exists in the data, and two data tables with the same name cannot exist in the same database.

CRUD operations _

The database stores data tables, and the data tables store data.

For data table operations, the most frequent operations are adding, deleting, modifying, and checking.

CRUD refers to adding a record, deleting a record, modifying a record, and querying a record in a table.

increase

insert into student (id, name, age) values (1, '李雷', 21)

It means to add a piece of data to the data table student, the content of this data is (1, 'Li Lei', 21), and the format of the content is (id, name, age).

Be sure to call conn.commit() before closing the database connection, otherwise the data update will fail.

check

select * from student means to get all the data from the data table student.

Its return value is a list, each element of which is a tuple.

change

update student set name='cat sister' where id=3

It means to update a record, the id of this record is 3, and its name is changed to Maomei.

delete

delete from student where id=3

Indicates to delete a record from the data table, and the id of this record is 3.

How about it?

Not difficult, is it?

Have you learned it?

There are fixed formats for connecting and disconnecting to the database, and you just need to follow them.

There is a lot to learn about sql statements. To be proficient, you need to learn more and practice more. It is a programming language. Do you think it is very close to our human language?

SQL stands for Structured Query Language (Structured Query Language), which is a database query and programming language for accessing data and querying, updating and managing relational database systems; it is also the extension of database script files.

Well, let's learn this today!

If you encounter any problems, let's communicate and solve them together.

I'm Cat Girl, see you next time!

Guess you like

Origin blog.csdn.net/parasoft/article/details/129848267