sqlite3 学习笔记

sqlite3 学习笔记#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @descrip : operate SqLite intrface # @Time : 2020/…

sqlite3 学习笔记[数据库教程]

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @descrip : operate SqLite intrface
# @Time    : 2020/04/22 21:57
# @Author  : mingfei.tang
import sqlite3

class SqLite3_Opt():
    def __init__(self):
        print(sqlite3.apilevel)
        self.conn = sqlite3.connect(first.db)
    
    def create_table(self):
        self.cursor = self.conn.cursor()
        #create table -1
        self.cursor.execute(‘‘‘create table user_tb(
                               _id integer primary key autoincrement,
                               name text,
                               password text,
                               gender text)‘‘‘)

        #create table -2
        self.cursor.execute(‘‘‘create table order_tb(
                               _id integer primary key autoincrement,
                               item_name text,
                               item_price text,
                               item_number text,
                               user_id inteter,
                               foreign key(user_id) references user_tb(_id))‘‘‘)

        self.cursor.close()
        
    #insert-1  many
    def insert_many_value(self):
        self.cursor = self.conn.cursor()
        self.cursor.executemany(insert into user_tb values(null, ?, ?, ?),
                                ((孙悟空-1, 423456,male),
                                (孙悟空-2, 423456,male),
                                (孙悟空-3, 423456,male),
                                (孙悟空-4, 423456,male)))
        self.conn.commit()
        self.cursor.close()

    #insert-2 single
    def insert_single_value(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute(insert into user_tb values(null, ?, ?, ?),(孙悟空-0, 423456,male))
        self.conn.commit()
        self.cursor.close()

    #delete single
    def delete_single_data(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute("DELETE FROM user_tb WHERE _id=?", (1,))
        self.conn.commit()
        self.cursor.close()
    
    #update single
    def update_data(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute("UPDATE user_tb SET name=? WHERE _id=?", ("猪八戒", 1))
        self.conn.commit()
        self.cursor.close()

    #fatch all data
    def fetch_all(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute("select * from user_tb")
        print(self.cursor.fetchall())
        self.cursor.close()

    #fatch one data
    def fetch_one(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute("select * from user_tb")
        print(self.cursor.fetchone())
        self.cursor.close()

    #fatch many data
    def fetch_many(self):
        self.cursor = self.conn.cursor()
        self.cursor.execute("select * from user_tb")
        print(self.cursor.fetchmany(10))
        self.cursor.close()

    def closedb(self):
        self.conn.close()

class Unit_test():
    def __init__(self):
        pass

    def infor_test(self):
        test = SqLite3_Opt()
        #test.create_table()
        #test.insert_single_value()
        #test.update_data()
        #test.delete_single_data()
        #test.fetch_all()
        #test.fetch_one()
        test.fetch_many()
        test.closedb()


if __name__ == "__main__":
    utest=Unit_test()
    utest.infor_test()

代码100分

 

sqlite3 学习笔记

原文:https://www.cnblogs.com/mftang2018/p/12764336.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8828.html

(0)
上一篇 2023-02-21
下一篇 2023-02-21

相关推荐

  • mac怎么启动redis_redis的配置文件在哪

    mac怎么启动redis_redis的配置文件在哪近来学习scrap分布式,需要用到redis,但以前没接触过,所以记录一下自己的安装过程。 准备:Mac,redis-5.0.4.tar.gz 1.压缩包到官网下(建议下载稳定版)网址:redis.i

    2023-01-25
    98
  • can’t create table errno:150_MySQL server has gone away

    can’t create table errno:150_MySQL server has gone away一、基础信息 1. Centos7.4 2.MySQL 5.7.21 3.基于gtid的复制 二、异常描述 误把从节点当成主节点插入一条数据,同一条数据在主、从节点插入都进行了一次插入操作,导致主键冲

    2022-12-25
    91
  • Oracle通过一个字段的值将一条记录拆分为多条记录「建议收藏」

    Oracle通过一个字段的值将一条记录拆分为多条记录「建议收藏」前言 之前遇到了一次这样的需求,当时没有记录,这一次又赶上了,简单的记录一下。 本文个人拙见,若有出入,请指出——来自菜的颤抖 该方式的效率不高,如何优化,请看记Oracle中regexp_subs…

    2023-01-28
    98
  • cbbe3bbb安装_B&O软件

    cbbe3bbb安装_B&O软件在目前我接触过的轻量级BI工具中,我最喜欢的是MS Power BI。 如果只是个人使用,我觉得这简直是一个完美的工具了。但是,凡事就怕但是,在企业级应用中,当前版本的Power BI有几个不太方便的地方。对我而言,最不能忍的一点就是数据需要完全导入到PBI文件之后才能使用(官…

    2023-03-02
    95
  • Python: 字符串替换方法全解析

    Python: 字符串替换方法全解析字符串是Python编程语言中最常用的数据类型之一,可以通过多种方法对字符串进行替换处理。Python字符串替换方法主要包括replace()、translate()、re.sub()三种方法,这三种方法在Python中都非常方便实用。

    2024-03-17
    28
  • mysql模糊查询区分大小写_oracle实例名区分大小写吗

    mysql模糊查询区分大小写_oracle实例名区分大小写吗Sql模糊查询,Like默认是不区分大小写的 使用Like时,怎么支持大小写呢? upper、lower,只能模糊所有的内容,不能区分内容中的大小写。 sqlite数据库对text字段默认是大小写敏感

    2022-12-25
    96
  • MySQL 约束「终于解决」

    MySQL 约束「终于解决」* 概念: 对表中的数据进行限定,保证数据的正确性、有效性和完整性。 * 分类: 1. 主键约束:primary key 2. 非空约束:not null 3. 唯一约束:unique 4. 外键约束

    2023-03-23
    109
  • 如何使用 SQL WHERE 过滤返回的数据「终于解决」

    如何使用 SQL WHERE 过滤返回的数据「终于解决」本文介绍如何使用 SQL WHERE 子句指定搜索条件,过滤返回的数据。还介绍如何检验相等、不相等、大于、小于、值的范围以及 NULL 值等。 一、使用 WHERE 子句 数据库表一般包含大量的数据,

    2023-05-13
    96

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注