搞懂Python SQLite的基本操作
>>> import site; site.getsitepackages()
['/home/user/usr/python/python2.7/lib/python2.7/site-packages', '/home/user/usr/python/python2.7/lib/site-python']
>>> import sqlite3
编号 | API & 描述 |
1 | sqlite3.connect(database [,timeout ,other optional arguments]) 该API打开与SQLite数据库文件的连接。你可以使用“:memory:”打开与驻留在RAM中而不是磁盘上的数据库的数据库连接。如果数据库成功打开,它将返回一个连接对象。 |
2 | connection.cursor([cursorClass]) 此例程创建一个游标,该游标将在整个Python数据库编程中使用。此方法接受单个可选参数cursorClass。如果提供,则必须是扩展sqlite3.Cursor的自定义光标类。 |
3 | cursor.execute(sql [, optional parameters]) 该例程执行一条SQL语句。可以对SQL语句进行参数化(即,使用占位符代替SQL文字)。sqlite3模块支持两种占位符:问号和命名占位符(命名样式)。 例如: − cursor.execute("insert into people values (?, ?)", (who, age)) |
4 | connection.execute(sql [, optional parameters]) 此例程是游标对象提供的上述execute方法的快捷方式,它通过调用cursor方法创建中间游标对象,然后使用给定的参数调用游标的execute方法。 |
5 | cursor.executemany(sql, seq_of_parameters) 此例程针对序列sql中找到的所有参数序列或映射执行SQL命令。 |
6 | connection.executemany(sql[, parameters]) 此例程是一种快捷方式,它通过调用cursor方法创建中间光标对象,然后使用给定的参数调用cursor.s executemany方法。 |
7 | cursor.executescript(sql_script) 此例程以脚本形式一次执行多个SQL语句。它首先发出COMMIT语句,然后执行作为参数获取的SQL脚本。所有SQL语句都应以半冒号(;)分隔。 |
8 | connection.executescript(sql_script) 该例程是一种快捷方式,它通过调用cursor方法来创建中间光标对象,然后使用给定的参数调用光标的executescript方法。 |
9 | connection.total_changes() 此例程返回自打开数据库连接以来已被修改,插入或删除的数据库行的总数。 |
10 | connection.commit() 此方法提交当前事务。如果不调用此方法,则从其他数据库连接中看不到自上次调用commit()以来所做的任何操作。 |
11 | connection.rollback() 自上次调用commit()以来,此方法将回滚对数据库的所有更改。 |
12 | connection.close() 此方法关闭数据库连接。请注意,这不会自动调用commit()。如果只关闭数据库连接而没有先调用commit(),则更改将丢失! |
13 | cursor.fetchone() 此方法获取查询结果集的下一行,返回单个序列,如果没有更多数据可用,则返回None。 |
14 | cursor.fetchmany([size = cursor.arraysize]) 该例程获取查询结果的下一组行,并返回一个列表。当没有更多行可用时,将返回一个空列表。该方法尝试获取由size参数指示的尽可能多的行。 |
15 | cursor.fetchall() 该例程获取查询结果的所有(剩余)行,并返回一个列表。如果没有可用的行,则返回一个空列表。 |
使用import语句导入sqlite3模块。
connect()方法将你需要连接的数据库的名称作为参数接受,并返回一个Connection对象。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
conn = sqlite3.connect('example.db')
print("Connection established ..........")
CREATE TABLE database_name.table_name(
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
.....
columnN datatype
);
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE ODIStats (
First_Name VARCHAR(255),
Matches INT,
Runs INT,
AVG FLOAT,
Centuries INT,
HalfCenturies INT
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> . tables
CRICKETERS ODIStats
sqlite>
使用connect()方法与数据库建立连接。
现在,使用Cursor类的execute()方法执行CREATE TABLE语句。
通过在上面创建的连接对象上调用cursor()方法来创建游标对象。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接到sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 删除已存在的表
cursor.execute("DROP TABLE if EXISTS EMPLOYEE")
# 按要求创建表
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) not NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(sql)
print("Table created successfully........")
# 提交
conn.commit()
# 关闭连接
conn.close()
Table created successfully........
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS (First_Name, Last_Name, Age, Place_Of_Birth, Country)
values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS (First_Name, Last_Name, Country)
values ('Jonathan', 'Trott', 'SouthAfrica');
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
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>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 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>
导入sqlite3包。
通过将数据库名称作为参数传递给它,使用connect()方法创建一个连接对象。
cursor()方法返回使用你可以与sqlite3的通信的光标对象。通过调用(上面创建的)Connection对象上的cursor()对象来创建光标对象。
然后,通过将INSERT语句作为参数传递给游标对象,从而调用execute()方法。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# Preparing SQL queries to INSERT a record into the database.
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........
SELECT column1, column2, columnN from table_name;
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS; Shikhar |Dhawan |India Jonathan |Trott |SouthAfrica Kumara |Sangakkara |Srilanka Virat |Kohli |India Rohit |Sharma |India sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> .header on sqlite> .mode column sqlite> SELECT FIRST_NAME, LAST_NAME, COUNTRY FROM CRICKETERS; First_Name Last_Name Country ---------- ---------- ---------- Shikhar Dhawan India Jonathan Trott SouthAfric Kumara Sangakkara rilanka Virat Kohli India Rohit Sharma India
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> .header on sqlite> .mode column sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_Birth Country ---------- ---------- ------- -------------- ---------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfric Kumara Sangakkara 41 Matale Srilanka Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> .width 10, 10, 4, 10, 13 sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- -------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India sqlite>
fetchall()方法检索查询结果集中的所有行,并将它们作为元组列表返回。(如果我们在检索到几行之后执行此操作,它将返回剩余的行)。
fetchone()方法获取查询结果中的下一行,并将其作为元组返回。
fetchmany()方法类似于fetchone(),但是它检索查询结果集中的下一组行,而不是单行。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 检索数据
cursor.execute('''SELECT * from EMPLOYEE''')
# 正在从表中获取第一行
result = cursor.fetchone();
print(result)
result = cursor.fetchall();
print(result)
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
('Ramya', 'Rama priya', 27, 'F', 9000.0)
[
('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)
]
SELECT column1, column2, columnN
from table_name
WHERE [search_condition]
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS WHERE AGE > 35; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ----------- Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqlite>
使用connect()方法与数据库建立连接。
通过在上面创建的连接对象上调用cursor()方法来创建游标对象。
现在,使用Cursor类的execute()方法执行CREATE TABLE语句。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 删除已存在的表
cursor.execute("DROP TABLE if EXISTS EMPLOYEE")
sql = '''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) not NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(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)''')
# 使用where子句检索特定记录
cursor.execute("SELECT * from EMPLOYEE WHERE AGE <23")
print(cursor.fetchall())
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
[('Vinay', 'Battacharya', 20, 'M', 6000.0)]
SELECT column-list
from table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS ORDER BY AGE; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ----------- Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS ORDER BY AGE, FIRST_NAME; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS ORDER BY AGE DESC; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Kumara Sangakkara 41 Matale Srilanka Jonathan Trott 38 CapeTown SouthAfrica Shikhar Dhawan 33 Delhi India Rohit Sharma 32 Nagpur India Virat Kohli 30 Delhi India sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import psycopg2
# 建立连接
conn = psycopg2.connect(
database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)
conn.autocommit = True
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
#删除已存在的表
cursor.execute("DROP TABLE if EXISTS EMPLOYEE")
# 创建表
sql = '''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) not NULL,
LAST_NAME CHAR(20),
AGE INT, SEX CHAR(1),
INCOME INT,
CONTACT INT
)'''
cursor.execute(sql)
# 填充表
cursor.execute(
'''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)
VALUES ('Ramya', 'Rama priya', 27, 'F', 9000),
('Vinay', 'Battacharya', 20, 'M', 6000),
('Sharukh', 'Sheik', 25, 'M', 8300),
('Sarmista', 'Sharma', 26, 'F', 10000),
('Tripthi', 'Mishra', 24, 'F', 6000)''')
conn.commit()
#Retrieving specific records using the ORDER BY clause
cursor.execute("SELECT * from EMPLOYEE ORDER BY AGE")
print(cursor.fetchall())
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
[
('Vinay', 'Battacharya', 20, 'M', 6000, None),
('Tripthi', 'Mishra', 24, 'F', 6000, None),
('Sharukh', 'Sheik', 25, 'M', 8300, None),
('Sarmista', 'Sharma', 26, 'F', 10000, None),
('Ramya', 'Rama priya', 27, 'F', 9000, None)
]
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> UPDATE CRICKETERS SET AGE = 45 WHERE FIRST_NAME = 'Shikhar' ;
sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS WHERE FIRST_NAME = 'Shikhar'; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- -------- Shikhar Dhawan 45 Delhi India sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> UPDATE CRICKETERS SET AGE = AGE+1;
sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Kumara Sangakkara 42 Matale Srilanka Virat Kohli 31 Delhi India Rohit Sharma 33 Nagpur India sqlite>
导入sqlite3包。
通过将数据库名称作为参数传递给它,使用connect()方法创建一个连接对象。
cursor()方法返回使用你可以与sqlite3的通信的光标对象。通过调用(上面创建的)Connection对象上的cursor()对象来创建光标对象。
然后,通过向游标对象传递UPDATE语句作为参数来调用execute()方法。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
#删除已存在的表
cursor.execute("DROP TABLE if EXISTS EMPLOYEE")
# 按要求创建表
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) not NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(sql)
# 插入数据
cursor.execute('''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)
VALUES ('Ramya', 'Rama priya', 27, 'F', 9000),
('Vinay', 'Battacharya', 20, 'M', 6000),
('Sharukh', 'Sheik', 25, 'M', 8300),
('Sarmista', 'Sharma', 26, 'F', 10000),
('Tripthi', 'Mishra', 24, 'F', 6000)''')
conn.commit()
# 正在获取更新前的所有行
print("Contents of the Employee table: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
# 更新记录
sql = '''UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX = 'M' '''
cursor.execute(sql)
print("Table updated...... ")
# 正在获取更新后的所有行
print("Contents of the Employee table after the update operation: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
Contents of the Employee table:
[
('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)
]
Table updated......
Contents of the Employee table after the update operation:
[
('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 21, 'M', 6000.0),
('Sharukh', 'Sheik', 26, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)
]
DELETE from table_name [WHERE Clause]
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> DELETE from CRICKETERS WHERE LAST_NAME = 'Sangakkara';
sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- -------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Virat Kohli 31 Delhi India Rohit Sharma 33 Nagpur India sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> DELETE from CRICKETERS;
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> SELECT * from CRICKETERS;
sqlite>
导入sqlite3包。
通过将数据库名称作为参数传递给它,使用connect()方法创建一个连接对象。
cursor()方法返回使用你可以与sqlite3的通信的光标对象。通过调用(上面创建的)Connection对象上的cursor()对象来创建光标对象。
然后,通过将游标对象作为参数传递给DELETE语句,从而在游标对象上调用execute()方法。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 检索表的内容
print("Contents of the table: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
# 删除记录
cursor.execute('''DELETE from EMPLOYEE WHERE AGE > 25''')
# 删除后检索数据
print("Contents of the table after delete operation ")
cursor.execute("SELECT * from EMPLOYEE")
print(cursor.fetchall())
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
Contents of the table:
[
('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 21, 'M', 6000.0),
('Sharukh', 'Sheik', 26, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)
]
Contents of the table after delete operation
[
('Vinay', 'Battacharya', 21, 'M', 6000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)
]
DROP TABLE table_name;
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int,
Place_Of_Birth VARCHAR(255), Country VARCHAR(255)
);
sqlite> CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) not NULL, LAST_NAME CHAR(20), AGE INT,
SEX CHAR(1), INCOME FLOAT
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> .tables
CRICKETERS EMPLOYEE
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> DROP table employee;
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> .tables
CRICKETERS
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> DROP table employee;
Error: no such table: employee
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> DROP table if EXISTS employee;
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 删除已存在的表
cursor.execute("DROP TABLE emp")
print("Table dropped... ")
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
Table dropped...
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
SELECT column1, column2, columnN
from table_name
LIMIT [no of rows]
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
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>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS LIMIT 3; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT * FROM CRICKETERS LIMIT 3 OFFSET 2; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- -------- Kumara Sangakkara 41 Matale Srilanka Virat Kohli 30 Delhi India Rohit Sharma 32 Nagpur India sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 检索单行
sql = '''SELECT * from EMPLOYEE LIMIT 3'''
# 执行查询
cursor.execute(sql)
# 获取数据
result = cursor.fetchall();
print(result)
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
[
('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0)
]
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age int,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
sqlite> CREATE TABLE ODIStats (
First_Name VARCHAR(255),
Matches INT,
Runs INT,
AVG FLOAT,
Centuries INT,
HalfCenturies INT
);
sqlite>
# Filename : example.py # Copyright : 2020 By Lidihuo # Author by : www.lidihuo.com # Date : 2020-08-15 sqlite> SELECT Cricketers.First_Name, Cricketers.Last_Name, Cricketers.Country, OdiStats.matches, OdiStats.runs, OdiStats.centuries, OdiStats.halfcenturies from Cricketers INNER JOIN OdiStats ON Cricketers.First_Name = OdiStats.First_Name; First_Name Last_Name Country Matches Runs Centuries HalfCenturies ---------- ---------- ------- ------- ---- --------- -------------- Shikhar Dhawan Indi 133 5518 17 27 Jonathan Trott Sout 68 2819 4 22 Kumara Sangakkara Sril 404 14234 25 93 Virat Kohli Indi 239 11520 43 54 Rohit Sharma Indi 218 8686 24 42 sqlite>
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
# 检索数据
sql = '''SELECT * from EMP INNER JOIN CONTACT ON EMP.CONTACT = CONTACT.ID'''
# 执行查询
cursor.execute(sql)
# 正在从表中获取第一行
result = cursor.fetchall();
print(result)
# 在数据库中提交更改
conn.commit()
# 关闭连接
conn.close()
[
('Ramya', 'Rama priya', 27, 'F', 9000.0, 101, 101, 'Krishna@mymail.com', 'Hyderabad'),
('Vinay', 'Battacharya', 20, 'M', 6000.0, 102, 102,'Raja@mymail.com', 'Vishakhapatnam'),
('Sharukh', 'Sheik', 25, 'M', 8300.0, 103, 103, 'Krishna@mymail.com', 'Pune'),
('Sarmista', 'Sharma', 26, 'F', 10000.0, 104, 104, 'Raja@mymail.com', 'Mumbai')
]
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-15
import sqlite3
# 连接sqlite
conn = sqlite3.connect('example.db')
# 使用cursor()方法创建游标对象
cursor = conn.cursor()
方法 | 描述 |
execute() | 该例程执行一条SQL语句。可以对SQL语句进行参数化(即,使用占位符代替SQL文字)。psycopg2模块使用%s符号支持占位符 例如:cursor.execute("insert into people values (%s, %s)", (who, age)) |
executemany() | 此例程针对序列sql中找到的所有参数序列或映射执行SQL命令。 |
fetchone() | 此方法获取查询结果集的下一行,返回单个序列,如果没有更多数据可用,则返回None。 |
fetchmany() | 该例程获取查询结果的下一组行,并返回一个列表。当没有更多行可用时,将返回一个空列表。该方法尝试获取由size参数指示的尽可能多的行。 |
fetchall() | 该例程获取查询结果的所有(剩余)行,并返回一个列表。如果没有可用的行,则返回一个空列表。 |
方法 | 描述 |
arraySize |
这是一个读/写属性,你可以设置fetchmany()方法返回的行数。
|
description |
这是一个只读属性,它返回包含结果集中列说明的列表。
|
lastrowid |
这是一个只读属性,如果表中有任何自动递增的列,它将返回在上一次INSERT或UPDATE操作中为该列生成的值。
|
rowcount |
如果执行SELECT和UPDATE操作,则返回返回/更新的行数。
|
connection |
此只读属性提供Cursor对象使用的SQLite数据库连接。
|