首页 > 其他分享 >Chapter 11

Chapter 11

时间:2022-12-14 19:47:17浏览次数:54  
标签:Chapter 11 execute name db cursor close conn

Sqlite3

import sqlite3
conn = sqlite3.connect('mrsoft.db') 
cursor = conn.cursor()
cursor.execute('create table user (id int(10) primary key, name varchar(20))')
cursor.close()
conn.close()

User

import sqlite3
conn = sqlite3.connect('mrsoft.db')
cursor = conn.cursor()
cursor.execute('insert into user (id, name) values ("1","MRsoft")')
cursor.execute('insert into user (id, name) values ("2","Andy")')
cursor.execute('insert into user (id, name) values ("3","明日科技小助手")')
cursor.close()
conn.commit()
conn.close()

Fetchone

import sqlite3
conn = sqlite3.connect('mrsoft.db')
cursor = conn.cursor()
cursor.execute('select * from user')
result1 = cursor.fetchone()
print(result1)
cursor.close()
conn.close()

Update

import sqlite3
conn = sqlite3.connect('mrsoft.db')
cursor = conn.cursor()
cursor.execute('update user set name = ? where id = ?', ('MR',1))
cursor.execute('select * from user')
result = cursor.fetchall()
print(result)
cursor.close()
conn.commit()
conn.close()

Delete

import sqlite3
conn = sqlite3.connect('mrsoft.db')
cursor = conn.cursor()
cursor.execute('delete from user where id = ?', (1,))
cursor.execute('select * from user')
result = cursor.fetchall()
print(result)
cursor.close()
conn.commit()
conn.close()

Books

import pymysql
db = pymysql.connect(host = "localhost", user = "root", password = "13467958k", database = "localhost_3306")
cursor = db.cursor()
cursor.execute("SELECT VERSION")
data = cursor.fetchone()
print("Database version : %s" % data)
db.close()

Books 2

import pymysql
db = pymysql.connect(host = "localhost", user = "root", password = "13467958k", database = "localhost_3306")
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS books")
sql = """
CREATE TABLE books (
    id int(8) NOT NULL AUTO_INCREMENT,
    name varchar(50) NOT NULL,
    category varchar(50) NOT NULL,
    PRICE decimal(10,2) DEFAULT NULL,
    publish_time date DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = MyISAM AUTO_INCERMENT = 1 DEFAULT CHARSET = utf8;
"""
cursor.execute(sql)
db.close()

 

Project

#实战1
import pymysql
db = pymysql.connect("localhost", "root", "root", "mrsoft",charset="utf8")
cursor = db.cursor()
cursor.execute("select name,price from books order by id ")
result = cursor.fetchall()
for book in result :
    print("图书:《{name}》, 价格:¥{price}元".format(name=book[0], price=book[1]))
db.close()
#实战2
import pymysql
db = pymysql.connect("localhost", "root", "root", "mrsoft",charset="utf8")
cursor = db.cursor()
cursor.execute("select name,price,publish_time from books where price < 70 and publish_time >= '2017-01-01' ")
result = cursor.fetchall()
for book in result :
    print("图书:《{name}》, 价格:¥{price}元,出版日期:{publish_time}".format(name=book[0], price=book[1],publish_time=book[2]))
# 关闭数据库连接
db.close()
#实战3
import pymysql
db = pymysql.connect("localhost", "root", "root", "mrsoft",charset="utf8")
cursor = db.cursor()
try:
    cursor.execute("delete from books where category = 'PHP' ")
    db.commit()
except:
    db.rollback()
cursor.execute("select name,price from books")
result = cursor.fetchall()
for book in result :
    print("图书:《{name}》, 价格:¥{price}元".format(name=book[0], price=book[1]))
db.close()

 

标签:Chapter,11,execute,name,db,cursor,close,conn
From: https://www.cnblogs.com/Kyaria-code-test/p/16983346.html

相关文章

  • Chapter 10
    Antmanor_messageprint("\n","="*10,"蚂蚁庄园的动态","="*10)file=open('message_txt','w')print("\n即将显示……\n")Antmanor_message_print("\n","="*10,"蚂蚁......
  • 【雕爷学编程】Arduino动手做(110)---JDY-31 蓝牙模块
    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的......
  • STM32F411CEU6
    ​​https://gitee.com/WeAct-TC/MiniSTM32F4x1​​STM32F411CE: ​​/SDK/STM32F411CEU6/MicroPython/WeAct_F411CE​​​​https://www.weact-tc.cn/2020/01/01/micropyt......
  • 利联科技——0基础学会了后自己都能开​​传奇游戏45.113.200​​
    ​  作为经典的怀旧游戏,传奇游戏赢得了许多人的青睐,在这个科技的时代,玩服已经满足不了了,逐渐越多数人会选择自己开服,那么开服需要准备什么呢。 按照开服流程,咱们一步一......
  • 深入理解BootStrap -- 导航页(navbar)11
    1、导航条​​navbar​​)和上一节介绍的导航(​​nav​​),就相差一个字,多了一个“条”字。其实在Bootstrap框架中他们还是明显的区别。在导航条(​​navbar​​)中有一个背景......
  • CS:APP--Chapter03 : machine-level representation of program - part 1 basic(2)
    CS:APP--Chapter03:machine-levelrepresentationofprogram-part1basic(2)标签(空格分隔):CS:APP目录CS:APP--Chapter03:machine-levelrepresentationofpro......
  • Docker学习11-Docker常规方式安装软件
    本文咱们将通过按照Tomcat、按照MySQL、安装Redis这三个实战安装,来熟悉在docker中怎么安装软件,咱们使用端口映射,及数据卷的使用场景安装的总体步骤:1:搜索镜像;2:拉取镜像;3......
  • OpenMLDB 社区月报 | 2022年11月
    十一月,OpenMLDB进行了高频的小版本迭代,共发布四个版本,平均每周一次。v0.6.5、v0.6.6、v0.6.7和v0.6.8陆续与广大开发者见面,在增强功能的同时,也提高了运维效率,对易用性......
  • BL110网关远程监测充电桩断网故障解决方案
    在碳中和等因素的影响下,燃油车逐步退出市场,新能源汽车逐步替代燃油车市场,获得了爆发性增长的市场空间。近年来,中国新能源汽车市场保持高速增长,新能源汽车当前的保有量、增......
  • 【2022-12-11】连岳摘抄
    23:59水在浑浊时,道理也很清楚,只要一直坚持向低处流,就没有走不出的困境。                              ......