Inserting Data
CRUD is a database principle that is crucial for interacting with databases.
Create: The ability to create tables
Read: The ability to get data from tables
Update: The ability to change values within tables
Delete: The most dangerous ability, the ability delete data, tables, rows, and columns
We have focused a lot on Reading data from tables; this chapter will do basic insertion of data.
SQL Query Format
When using the
INSERT
query, you must specify a table that exists in your SQL databaseThe values that you want to insert should line up with the columns that it should go into
It is imperative to understand that we are adding a row of data when INSERTING
Example
Examine the following employee
table:
1
John Doe
Software Engineer
2
Jane Smith
Product Manager
3
Alice Johnson
UX Designer
Our goal is to insert the following employee:
ID: 4
Name: Lil Kid
Position: Junior Developer
The query would look like:
In Python, it would look like:
The execute()
method is going to be using two argument inputs.
The query as a long string
A tuple that contains the value to be inserted
The reason for using (?, ?, ?)
in our query
(?, ?, ?)
in our queryThe question marks will act as a placeholder and they will use the values we provide within our 2nd argument in order.
Last updated