Python 数据访问教程

Python 数据访问主页

Python MySQL

Python MySQL 简介 Python MySQL 数据库连接 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入数据 Python MySQL 选择数据 Python MySQL Where 子句 Python MySQL 排序 Python MySQL 更新表 Python MySQL 删除数据 Python MySQL 删除表 Python MySQL Limit 子句 Python MySQL 连接 Python MySQL 游标对象

Python PostgreSQL

Python PostgreSQL 简介 Python PostgreSQL 数据库连接 Python PostgreSQL 创建数据库 Python PostgreSQL 创建表 Python PostgreSQL 插入数据 Python PostgreSQL 选择数据 Python PostgreSQL Where 子句 Python PostgreSQL 排序 Python PostgreSQL 更新表 Python PostgreSQL 删除数据 Python PostgreSQL 删除表 Python PostgreSQL Limit 子句 Python PostgreSQL 连接 Python PostgreSQL 游标对象

Python SQLite

Python SQLite 简介 Python SQLite 建立连接 Python SQLite 创建表 Python SQLite 插入数据 Python SQLite 选择数据 Python SQLite Where 子句 Python SQLite 排序 Python SQLite 更新表 Python SQLite 删除数据 Python SQLite 删除表 Python SQLite Limit 子句 Python SQLite 连接 Python SQLite 游标对象

Python MongoDB

Python MongoDB 简介 Python MongoDB 创建数据库 Python MongoDB 创建集合 Python MongoDB 插入文档 Python MongoDB 查找 Python MongoDB 查询 Python MongoDB 排序 Python MongoDB 删除文档 Python MongoDB 删除集合 Python MongoDB 更新 Python MongoDB Limit 子句

Python 数据访问资源

Python 数据访问 快速指南 Python 数据访问 有用资源 Python 数据访问 讨论


Python SQLite - 插入数据

您可以使用 INSERT INTO 语句向 SQLite 的现有表中添加新行。在此,您需要指定表的名称、列名和值(与列名的顺序相同)。

语法

以下是 INSERT 语句的推荐语法 −

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);

其中,column1、column2、column3、.. 是表的列名称,value1、value2、value3、... 是您需要插入到表中的值。

示例

假设我们使用 CREATE TABLE 语句创建了一个名为 CRICKETERS 的表,如下所示 −

sqlite> CREATE TABLE CRICKETERS (
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Age int,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
);
sqlite>

以下 PostgreSQL 语句在上面创建的表中插入一行。

sqlite> insert into CRICKETERS 
   (First_Name, Last_Name, Age, Place_Of_Birth, Country) values
   ('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite>

使用 INSERT INTO 语句插入记录时,如果跳过任何列名,则插入该记录时会在跳过的列处留下空格。

sqlite> insert into CRICKETERS 
   (First_Name, Last_Name, Country) values 
   ('Jonathan', 'Trott', 'SouthAfrica');
sqlite>

如果传递的值的顺序与表中相应的列名相同,您也可以在不指定列名的情况下将记录插入表中。

sqlite> insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
sqlite> insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
sqlite>

将记录插入表后,您可以使用 SELECT 语句验证其内容,如下所示 −

sqlite> select * from cricketers;
Shikhar  | Dhawan     | 33 | Delhi  | India
Jonathan | Trott      |    |        | SouthAfrica
Kumara   | Sangakkara | 41 | Matale | Srilanka
Virat    | Kohli      | 30 | Delhi  | India
Rohit    | Sharma     | 32 | Nagpur | India
sqlite>

使用 python 插入数据

向 SQLite 数据库中的现有表添加记录 −

  • 导入 sqlite3 包。

  • 使用 connect() 方法通过将数据库名称作为参数传递给它来创建连接对象。

  • cursor() 方法返回一个游标对象,您可以使用该对象与 SQLite3 进行通信。通过在(上面创建的)Connection 对象上调用 cursor() 对象来创建游标对象。

  • 然后,通过将 INSERT 语句作为参数传递给游标对象,调用游标对象上的 execute() 方法。

示例

以下 python 示例将记录插入到名为 EMPLOYEE 的表中 −

import sqlite3

#连接到 sqlite
conn = sqlite3.connect('example.db')

#使用 cursor() 方法创建游标对象
cursor = conn.cursor()

# 准备 SQL 查询以将记录插入数据库。
cursor.execute('''INSERT INTO EMPLOYEE(
   FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ('Ramya', 'Rama Priya', 27, 'F', 9000)''')

cursor.execute('''INSERT INTO EMPLOYEE(
   FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ('Vinay', 'Battacharya', 20, 'M', 6000)''')

cursor.execute('''INSERT INTO EMPLOYEE(
   FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ('Sharukh', 'Sheik', 25, 'M', 8300)''')

cursor.execute('''INSERT INTO EMPLOYEE(
   FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ('Sarmista', 'Sharma', 26, 'F', 10000)''')

cursor.execute('''INSERT INTO EMPLOYEE(
   FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ('Tripthi', 'Mishra', 24, 'F', 6000)''')

# 在数据库中提交你的更改
conn.commit()
print("Records inserted........")

# 关闭连接
conn.close()

输出

Records inserted........