SQLite

SQLite Insert Values

“The INSERT command is being used to add content to a required table. SQLite has several INSERT commands that could be applied to import only one row, multi rows, or some specific values into a column. Furthermore, we may utilize data from a SELECT query to add a record into a column.

  • Following the INSERT INTO terms, we have to provide the name of the required table within which we would like to add the data.
  • Following the table name, we have added a series of columns that will be alienated using commas. It is, therefore, the standard way to mention the names of columns after the required table name.
  • Finally, when the word VALUES is used, we will add a set of values, and these are also separated with the help of commas. We must declare entries for all rows in the set of values if we skip the names of the columns. The number of entries in the set of values and the number of attributes in the column set must be equal.

In this article, we’ll see how to incorporate rows into the required table by using the INSERT command of the SQLite query.

We downloaded the BD Browser for the SQLite compiler to execute SQLite queries. First, we must hit the “CTRL+N” shortcut key from the keyboard to build a new database. The database file is saved in almost any computer folder with the “SQLite databases files” format. We might even execute the query to build a new database. We will maintain a database of “Pilot” in this article. Then we must create a table, add different values to the table, and run various commands.”

Creation of Table

We will take the example of a table “Pilot,” and the CREATE query is being used to create a table. In this query, we will specify its different attributes. The attributes have different data types, such as the Id possess integer, name, city, and rank possess text data type, and the age and salary possess numeric data types.

To execute the query, we first select the required query, then press the “Shift + F5” shortcut keys. The output indicates that the “CREATE” query could be successfully implemented.

Insert Data in a Single Row

After generating the table, we will utilize the INSERT query to add the data to the table. Here we just add the data of only one Pilot.

INSERT INTO Pilot (Id, Name, City, Rank, Age, Salary)

VALUES (98445, 'Azaan', 'Sahiwal', 'Training Captain', '38', '90000')

We successfully inserted the data in a single row of the table “Pilot.”

Run SELECT Query

The SELECT statement is the most important command for querying an SQLite database. In the SELECT query, we will indicate what we want to get. Furthermore, we can use the “FROM” command to retrieve records while using the SELECT statement. The “FROM” command has been applied to define where data will be retrieved from clearly. The “FROM” command will indicate a single or many tables from which entries will be retrieved.

>> SELECT * FROM Pilot

Whenever we inserted the record of a single pilot, we applied a SELECT query to obtain that data.

Insert Data in the Multiple Rows

Using the INSERT query, we can insert the data of multiple rows. In this step, we applied the “INSERT” query to insert the data in the columns of table “Pilot” using only one INSERT command.

>> INSERT INTO Pilot (Id, Name, City, Rank, Age, Salary) VALUES (5035, 'Faiq', 'Karachi', 'First Officer', '32', '50000'),

(6975, 'Shazain', 'Islamabad', 'Captain', '42', '180000'),

(3546, 'Zain', 'Sargodha', 'Second Officer', '35', '70000'),

(8767, 'Daniyal', 'Lahore', 'Senior First Officer', '40', '120000');

We add values in different rows in the table “Pilot,” as shown in the figure.

Once again, we want to fetch the data from the table “Pilot,” so we have employed the SELECT query. The symbol * represents that SELECT will return the data of all the table columns.

>> SELECT * FROM Pilot

Hence we read the data of the table “Pilot” by running the “SELECT” command. This can retrieve all the columns and rows of the required table mentioned in the “FROM” clause.

The data shows the Ids, names, city names, ranks, ages, and salaries of different pilots.

Insert All Other Values Without Inserting the PRIMARY KEY Column

In this step, we will insert all other table values, except the column with the PRIMARY KEY of the table. We noticed from the above query that the column “Id” contains the PRIMARY KEY. It is not mandatory to insert the column’s value that possesses a PRIMARY KEY.

>> INSERT INTO Pilot (Name, City, Rank, Age, Salary) VALUES ('Faiq', 'Karachi', 'First Officer', '32', '50000')

Here the INSERT query adds the value of all columns of the table except “Id.,” And we implemented the above query without any error.

Insert the Values in the Table Without Specifying the Column List

We can also add all the values into the table “Pilot” without defining the list of columns in the INSERT query. We will be inserting the values by just writing INSERT INTO term, followed by a table name and then using the keyword VALUES.

>> INSERT INTO Pilot VALUES (503, 'Faiq', 'Karachi', 'First Officer', '32', '50000'),

(697, 'Shazain', 'Islamabad', 'Captain', '42', '180000'),

(354, 'Zain', 'Sargodha', 'Second Officer', '35', '70000'),

(876, 'Daniyal', 'Lahore', 'Senior First Officer', '40', '120000');

As no list of all the columns has been provided after the required table name in the INSERT query above. Therefore, only data for all required columns are given in this situation.

Insert the Values in the Table by Specifying Some Columns

In this instance, we only insert the data of only a few columns of the table “Pilot.” We have specified the names of those columns in the INSERT query, which we want to be inserted into the table.

>> INSERT INTO Pilot (Name, Rank, Age) VALUES ('Faiq', 'First Officer', '32')

We have provided only three columns and their values in this INSERT query.

Update the Data

We may update the table’s data using REPLACE or an UPDATE query. We have applied the REPLACE command to replace the older data with the new one in this INSERT query.

>> REPLACE INTO Pilot (Name, City, Rank, Age, Salary) VALUES ('Arslan', 'Rawalpindi', 'First Officer', '34', '60000')

The data of the columns we want to be replaced includes Name, City, Rank, Age, and Salary. The outcome shows the successful execution of the query.

Conclusion

This SQLite tutorial demonstrated the use of the “INSERT” query in SQLite. The “INSERT” command in SQLite queries allows users to insert additional data sets into a database table. Moreover, we have seen how to use INSERT to add a single set of data to a table, how to utilize the INSERT query to add data to some specified columns, how to update the table’s record, as well as how to add data to columns without providing column names in the query.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.