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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    P1005 矩阵取数
    [BZOJ2662][BeiJing wc2012]冻结
    [BZOJ1191]超级英雄Hero
    [bzoj1008] 越狱
    [bzoj1001]狼抓兔子 最小割
    网络流24题——负载平衡问题
    分形小山
    可并堆——左偏树、斜堆
    NOIP最优贸易
    洛谷P2073送花
  • 原文地址:https://www.cnblogs.com/BenLam/p/9723078.html
Copyright © 2011-2022 走看看