Insert data into SQLite database using Python


This article introduces Python's built-in sqlite3 module to create an SQLite database connection, create a table, and insert data into that table.


Insert data into an SQLite database using Python using the sqlite3 module

We have to follow the next steps to insert data into the SQLite database table.

  • Import the sqlite3 module.
import sqlite3

It's a built-in module; we don't have to install it separately. We just need to import it and use it.

  • Create a SQLite database connection.
connect= sqlite3.connect('test.db')

.connect()method creates a connection to the specified SQLite database; in our case, it is test.db. You can rename the database name, remember the following syntax.

sqlite3.connect('database_name.db')

Gets a cursor object to execute a SQL query.

cursor = connect.cursor()

connect.cursor()method creates a cursor object, we can use it to execute SQL queries to operate the specified database, whether it is creating tables, inserting data, updating data, etc.

  • Create a STUDENT table.
std_table ="""CREATE TABLE STUDENT( FIRSTNAME VARCHAR(255), LASTNAME VARCHAR(255));"""
cursor.execute(std_table)

Here, we first design our CREATE TABLE query and save it in std_table. Next, we pass the std_table to the cursor.execute() method that executes the specified query.

  • Insert data into the STUDENT table.
cursor.execute('''INSERT INTO STUDENT VALUES ('Mehvish', 'Ashiq')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Raza', 'Tahir')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Hina', 'Mukhtar')''')

Here, we use the cursor.execute() method to run the INSERT query.
%> 请注意, we don't have to store the query in a separate variable when creating the STUDENT table. Nevertheless, we can still pass an SQL query to the cursor.execute() method, just like we did for the INSERT statement above.

  • Display the inserted data.
print("The 'STUDENT' Table Data:")
table_data=cursor.execute('''SELECT * FROM STUDENT''')
for row in table_data:
  print(row)

Again, we use cursor.execute()the method to run the SELECT query and save all the table data in table_data, which we will use to loop and print each row.

output:

The 'STUDENT' Table Data:
('Mehvish', 'Ashiq')
('Raza', 'Tahir')
('Hina', 'Mukhtar')
  • Commit and close the connection.
connect.commit()
connect.close()

.commit()will commit the latest changes in our currently selected database, and .close() will close the connection. The full source code is below.

full source code

import sqlite3

connect= sqlite3.connect('test.db')
cursor = connect.cursor()

std_table ="""CREATE TABLE STUDENT( FIRSTNAME VARCHAR(255), LASTNAME VARCHAR(255));"""
cursor.execute(std_table)

cursor.execute('''INSERT INTO STUDENT VALUES ('Mehvish', 'Ashiq')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Raza', 'Tahir')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Hina', 'Mukhtar')''')

print("The 'STUDENT' Table Data:")
table_data=cursor.execute('''SELECT * FROM STUDENT''')
for row in table_data:
    print(row)

connect.commit()
connect.close()

output:

The 'STUDENT' Table Data:
('Mehvish', 'Ashiq')
('Raza', 'Tahir')
('Hina', 'Mukhtar')

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/131589777