Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the SELECT statement of SQLite

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the SELECT sentence of SQLite". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the SELECT sentence of SQLite.

Grammar

The basic syntax of the SELECT statement for SQLite is as follows:

SELECT column1, column2, columnN FROM table_name

Here, column1, column2... Are the fields of the table, and their values are what you want to get. If you want to get all the available fields, you can use the following syntax:

SELECT * FROM table_name; instance

Suppose the COMPANY table has the following records:

ID NAME AGE ADDRESS SALARY--1 Paul 32 California 20000.02 Allen 25 Texas 15000.03 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.06 Kim 22 South-Hall 45000.07 James 24 Houston 10000.0

The following is an example that uses the SELECT statement to get and display all of these records. Here, the first three commands are used to set the correctly formatted output.

Sqlite > .header onsqlite > .mode columnsqlite > SELECT * FROM COMPANY

Finally, the following results will be obtained:

ID NAME AGE ADDRESS SALARY--1 Paul 32 California 20000.02 Allen 25 Texas 15000.03 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.06 Kim 22 South-Hall 45000.07 James 24 Houston 10000.0

If you want to get only the fields specified in the COMPANY table, use the following query:

Sqlite > SELECT ID, NAME, SALARY FROM COMPANY

The above query produces the following results:

ID NAME SALARY- 1 Paul 20000.02 Allen 15000.03 Teddy 20000.04 Mark 65000.05 David 85000.06 Kim 45000.07 James 10000.0 sets the width of the output column

Sometimes, the output is truncated because .mode column is caused by the default width of the column to be displayed. At this point, you can use .width num, num... . The command sets the width of the display column, as follows:

Sqlite > .width 10,20, 10sqlite > SELECT * FROM COMPANY

The .width command above sets the width of the first column to 10, the width of the second column to 20, and the width of the third column to 10. So the above SELECT statement will get the following result:

ID NAME AGE ADDRESS SALARY--1 Paul 32 California 20000.02 Allen 25 Texas 15000.03 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.06 Kim 22 South-Hall 45000.07 James 24 Houston 10000.0Schema Information

Because all dot commands are only available at the SQLite prompt, when you program with SQLite, you use the following SELECT statement with the sqlite_master table to list all tables created in the database:

Sqlite > SELECT tbl_name FROM sqlite_master WHERE type = 'table'

Assuming that a unique COMPANY table already exists in testDB.db, the following results are produced:

Tbl_name-COMPANY

You can list complete information about the COMPANY table, as follows:

Sqlite > SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name =' COMPANY'

Assuming that a unique COMPANY table already exists in testDB.db, the following results are produced:

CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (50), SALARY REAL) Thank you for your reading. This is the content of "how to use the SELECT statement of SQLite". After studying this article, I believe you have a deeper understanding of how to use the SELECT statement of SQLite. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report