slqite3练习
连接
import sqlite3 con = sqlite3.connect(":memory:") c = con.cursor() # Create table
c.execute('''CREATE TABLE COMPANY
(ID integer, NAME text, AGE integer, ADDRESS text, SALARY real)''') # Larger example that inserts many records at a time
purchases = [(1,'Paul',32,'California',20000.0),
(2,'Allen',25,'Texas',15000.0),
(3,'Teddy',23,'Norway',20000.0),
(4,'Mark',25,'Rich-Mond',65000.0),
(5,'David',27,'Texas',85000.0),
(6,'Kim',22,'South-Hall',45000.0),
(7,'James',24,'Houston',10000.0)]
c.executemany('INSERT INTO COMPANY VALUES (?,?,?,?,?)', purchases) # Create table
c.execute('''CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);''') # Larger example that inserts many records at a time
purchases = [(1, 'IT Billing', 1 ),
(2, 'Engineering', 2 ),
(3, 'Finance', 7 )]
c.executemany('INSERT INTO DEPARTMENT VALUES (?,?,?)', purchases) # Save (commit) the changes
con.commit() # 显示所有记录
c.execute("SELECT * FROM COMPANY;")
for row in c:
print(row) print()
# 显示所有记录
c.execute("SELECT * FROM DEPARTMENT;")
for row in c:
print(row) print("\n连接(JOIN)")
c.execute("SELECT * FROM COMPANY,DEPARTMENT;")
for row in c:
print(row) print("\n交叉连接(CROSS JOIN)")
#c.execute("SELECT EMP_ID, NAME, DEPT FROM COMPANY CROSS JOIN DEPARTMENT;")
c.execute("SELECT * FROM COMPANY CROSS JOIN DEPARTMENT;")
for row in c:
print(row) print("\n内连接(INNER JOIN)")
c.execute("SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;")
for row in c:
print(row) print("\n左外连接(LEFT OUTER JOIN)")
c.execute("SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;")
for row in c:
print(row)
子查询
import sqlite3 '''子查询''' con = sqlite3.connect(":memory:") c = con.cursor() # Create table
c.execute('''CREATE TABLE COMPANY
(ID integer, NAME text, AGE integer, ADDRESS text, SALARY real)''') # Larger example that inserts many records at a time
purchases = [(1,'Paul',32,'California',20000.0),
(2,'Allen',25,'Texas',15000.0),
(3,'Teddy',23,'Norway',20000.0),
(4,'Mark',25,'Rich-Mond',65000.0),
(5,'David',27,'Texas',85000.0),
(6,'Kim',22,'South-Hall',45000.0),
(7,'James',24,'Houston',10000.0)]
c.executemany('INSERT INTO COMPANY VALUES (?,?,?,?,?)', purchases) # Save (commit) the changes
con.commit() # ====================================================================================
# SELECT 语句中的子查询使用
# ====================================================================================
print('='*30)
print('SELECT 语句中的子查询使用')
print('='*30) c.execute("SELECT * "
"FROM COMPANY "
"WHERE ID IN (SELECT ID "
"FROM COMPANY "
"WHERE SALARY > 45000);") for row in c:
print(row) # ====================================================================================
# INSERT 语句中的子查询使用
# ====================================================================================
print('='*30)
print('INSERT 语句中的子查询使用')
print('='*30) # Create table
c.execute('''CREATE TABLE COMPANY_BKP
(ID integer, NAME text, AGE integer, ADDRESS text, SALARY real)''') c.execute("INSERT INTO COMPANY_BKP "
"SELECT * FROM COMPANY "
"WHERE ID IN (SELECT ID "
"FROM COMPANY);") c.execute("SELECT * FROM COMPANY_BKP")
for row in c:
print(row) # ====================================================================================
# UPDATE 语句中的子查询使用
# ====================================================================================
print('='*30)
print('UPDATE 语句中的子查询使用')
print('='*30) # 子查询
c.execute("UPDATE COMPANY_BKP "
"SET SALARY = SALARY * 0.50 "
"WHERE AGE IN (SELECT AGE FROM COMPANY_BKP "
"WHERE AGE >= 27 );") c.execute("SELECT * FROM COMPANY_BKP")
for row in c:
print(row) # ====================================================================================
# DELETE 语句中的子查询使用
# ====================================================================================
print('='*30)
print('DELETE 语句中的子查询使用')
print('='*30) # 子查询
c.execute("DELETE FROM COMPANY_BKP "
"WHERE AGE IN (SELECT AGE FROM COMPANY_BKP "
"WHERE AGE > 27 );") c.execute("SELECT * FROM COMPANY_BKP")
for row in c:
print(row) c.execute("SELECT tbl_name FROM :memory: WHERE type = 'table';")
slqite3练习的更多相关文章
- Python框架 Flask 项目实战教程
本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走.下载源码,运行pip install -r requirements.txt 建立环境python db_ ...
- python flask实现小项目方法
本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走. 下载源码,运行 pip install -r requirements.txt 建立环境 python ...
- swift3.0:CoreData的使用
一.介绍 CoreData不像slqite3那样编写代码繁琐,同时避免了使用了SQL语句的麻烦,也可以回避使用C语言的语法,降低了iOS开发的技术门槛. CoreData可降低开发成本,提高代码质量. ...
- Python Flask高级编程之从0到1开发《鱼书》精品项目 ☝☝☝
Python Flask高级编程之从0到1开发<鱼书>精品项目 ☝☝☝ 一 .安装环境我们使用 flask web框架,并用 sqlalchemy来做数据库映射,并使用 migrate做数 ...
- Python Flask高级编程之从0到1开发《鱼书》精品项目 ✍✍✍
Python Flask高级编程之从0到1开发<鱼书>精品项目 一 .安装环境我们使用 flask web框架,并用 sqlalchemy来做数据库映射,并使用 migrate做数据迁移 ...
随机推荐
- shell基础学习
1. #! /bin/bash #设置只读变量,只读变量不可修改myUrl="http://www.baidu.com"readonly myUrl #删除变量,unset不能删除 ...
- IntelliJ IDEA 下载和激活
IntelliJ IDEA 下载地址: https://www.jetbrains.com/idea/download/#section=windows 激活码获取地址:http://idea.lan ...
- Java并发案例01---多线程之死锁
多线程之死锁案例一 package example; /** * 模拟死锁 * @author maple * */ public class DeadLock { public int flag = ...
- .Net Core应用程序发布时不同方式的差别
.Net Core的文档更新的真是快..每次看的时候都觉得之前是不是梦游看的...每次发布应用程序的时候都要翻看下文档..至少rid是死活记不住.还是留个RID的索引吧..还有发布的索引 ,这样就好复 ...
- spring boot 输入参数统一校验
1 引入spring boot validate maven 依赖 <!-- 验证 --> <dependency> <groupId>org.hiberna ...
- 【HTML-进阶-如何实现父级块级元素宽度自适应子元素宽度】
背景 块级元素宽度默认值为100%,而不是auto;因此其宽度不会根据子元素内容动态适应. 如何实现父级元素宽度动态适应其子元素. 方法一 display:inline; 给块级元素设置inline- ...
- 在 Oracle Linux 6.5 上安装 Oracle 11g 单实例数据库
Checking the Hardware Requirements 系统必须满足下面最小的硬件要求 Memory Requirements Minimum: 1 GB of RAMRecommend ...
- QueryRunner cannot be resolved to a type:关于包不能正常导入的问题
在操作一个功能模块的时候,出现一个问题: 我原则是按着项目指导一步一步走的,但却出现, QueryRunner cannot be resolved to a type,这个问题应该属于Xxx can ...
- Kafka 部署指南-好久没有更新博客了
最近到了一家新公司,很多全新技术栈要理解.每天都在看各类 English Offcial Document,我的宗旨是我既然看懂了,就写下来分享,这是第一篇. 基本需求: 1.已有 zookeeper ...
- python制作验证码
from PIL import Image, ImageFont, ImageDraw, ImageFilterfrom random import choice, randint # 随即配置颜色d ...