zoukankan      html  css  js  c++  java
  • Python处理Sqlite3数据库

    sqlite3比较小众
    本章主要通过Python Code表述如何增、查、改、删 sqlite3 DB

    一.直接上代码

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2018-09-29 12:04:51
    # @Author  : call_me
    # @Version : V1.0
    
    import sqlite3
    connect = sqlite3.connect('student.db')
    cursor = connect.cursor()
    try:
        sql = '''create table student(
            [id]        integer PRIMARY KEY,
            [name]        varchar(50),
            [sex]        varchar(2),
            [age]        int
            );
            '''
        cursor.execute(sql)
    except Exception as e:
        print("表已创建")
    
    # 插入数据
    sql = '''insert into student (id,name,sex,age) VALUES (?,?,?,?)'''
    data = (1,"张1","","9")
    cursor.execute(sql,data)
    data = (2,"张2","","10")
    cursor.execute(sql,data)
    data = (3,"张3","","11")
    cursor.execute(sql,data)
    data = (4,"张4","","12")
    cursor.execute(sql,data)
    
    # 查数据
    for select in cursor.execute("select * from student"):
        print("名称:%s ,	 年龄:%s " %(select[1],select[3]))
    
    # 删除数据
    cursor.execute("delete from student")
    
    # 删除表
    cursor.execute("drop table student")
    
    connect.commit()
    connect.close()
    Code
    作者:GI-JOE
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    手机号不能为空
    选项卡套选项卡
    可以在一个html的文件当中读取另一个html文件的内容
    价格计算
    v形 加强版
    V形
    生成100个Div
    伪元素::after和::before
    数组中的toString,toLocalString,valueOf方法有什么区别
    JavaScript toLocaleString() 方法
  • 原文地址:https://www.cnblogs.com/BenLam/p/9723078.html
Copyright © 2011-2022 走看看