Python SQLite - 插入数据
在本文中,我们将讨论如何使用 sqlite3 模块从Python在 SQLite 数据库的表中插入数据。 SQL 的 SQL INSERT INTO语句用于在表中插入新行。有两种使用 INSERT INTO 语句插入行的方法:
- Only values :第一种方法是只指定要插入的数据的值,而没有列名。
INSERT INTO table_name VALUES (value1, value2, value3,…);
table_name: name of the table.
value1, value2,.. : value of first column, second column,… for the new record
- 列名和值都:在第二种方法中,我们将指定要填充的列及其对应的值,如下所示:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);
table_name: name of the table.
column1: name of first column, second column …
value1, value2, value3 : value of first column, second column,… for the new record
示例 1:下面的程序描述了如何仅使用值在 SQLite 表中插入数据。在程序中,我们首先创建一个名为 STUDENT 的表,然后使用 INSERT 查询的第一种语法向其中插入值。最后,我们显示表的内容并将其提交到数据库。
Python3
# Import module
import sqlite3
# Connecting to sqlite
conn = sqlite3.connect('geeks2.db')
# Creating a cursor object using the
# cursor() method
cursor = conn.cursor()
# Creating table
table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),
SECTION VARCHAR(255));"""
cursor.execute(table)
# Queries to INSERT records.
cursor.execute('''INSERT INTO STUDENT VALUES ('Raju', '7th', 'A')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Shyam', '8th', 'B')''')
cursor.execute('''INSERT INTO STUDENT VALUES ('Baburao', '9th', 'C')''')
# Display data inserted
print("Data Inserted in the table: ")
data=cursor.execute('''SELECT * FROM STUDENT''')
for row in data:
print(row)
# Commit your changes in the database
conn.commit()
# Closing the connection
conn.close()
Python3
# Import module
import sqlite3
# Connecting to sqlite
conn = sqlite3.connect('geek.db')
# Creating a cursor object using the
# cursor() method
cursor = conn.cursor()
# Creating table
table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),
SECTION VARCHAR(255));"""
cursor.execute(table)
# Queries to INSERT records.
cursor.execute(
'''INSERT INTO STUDENT (CLASS, SECTION, NAME) VALUES ('7th', 'A', 'Raju')''')
cursor.execute(
'''INSERT INTO STUDENT (SECTION, NAME, CLASS) VALUES ('B', 'Shyam', '8th')''')
cursor.execute(
'''INSERT INTO STUDENT (NAME, CLASS, SECTION ) VALUES ('Baburao', '9th', 'C')''')
# Display data inserted
print("Data Inserted in the table: ")
data=cursor.execute('''SELECT * FROM STUDENT''')
for row in data:
print(row)
# Commit your changes in
# the database
conn.commit()
# Closing the connection
conn.close()
输出:
SQLite3:
示例 2:下面的程序与第一个程序类似,但我们通过重新排序具有值的列的名称,将值插入表中,如第二个语法。
蟒蛇3
# Import module
import sqlite3
# Connecting to sqlite
conn = sqlite3.connect('geek.db')
# Creating a cursor object using the
# cursor() method
cursor = conn.cursor()
# Creating table
table ="""CREATE TABLE STUDENT(NAME VARCHAR(255), CLASS VARCHAR(255),
SECTION VARCHAR(255));"""
cursor.execute(table)
# Queries to INSERT records.
cursor.execute(
'''INSERT INTO STUDENT (CLASS, SECTION, NAME) VALUES ('7th', 'A', 'Raju')''')
cursor.execute(
'''INSERT INTO STUDENT (SECTION, NAME, CLASS) VALUES ('B', 'Shyam', '8th')''')
cursor.execute(
'''INSERT INTO STUDENT (NAME, CLASS, SECTION ) VALUES ('Baburao', '9th', 'C')''')
# Display data inserted
print("Data Inserted in the table: ")
data=cursor.execute('''SELECT * FROM STUDENT''')
for row in data:
print(row)
# Commit your changes in
# the database
conn.commit()
# Closing the connection
conn.close()
输出:
SQLite3: