• python连接sqlite基本操作


    import sqlite3
    
    conn = sqlite3.connect('play.sqlite')
    cur = conn.cursor()
    
    # 如果存在就删除
    # cur.executescript('''
    # DROP TABLE IF EXISTS User;
    # CREATE TABLE User (
    #     id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    #     name   TEXT UNIQUE
    # );
    # ''')
    
    # 如果不存在就创建
    cur.executescript('''
    CREATE TABLE IF NOT EXISTS User (
        id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
        name   TEXT UNIQUE
    );
    ''')
    
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三1',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三2',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三3',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三3',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三3',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三3',))
    # cur.execute('INSERT OR IGNORE INTO User (name) VALUES (?)', ('张三4',))
    
    cur.execute('SELECT id, name FROM User LIMIT 3')
    list = cur.fetchall()
    print(list)
    
    conn.commit()
  • 相关阅读:
    MySQL多表查询
    多表关联
    MySQL数据类型 约束
    初识数据库
    socker server和 event
    os 模块 和 os模块下的path模块
    sys 模块
    time 模块
    目录规范

  • 原文地址:https://www.cnblogs.com/zhishaofei/p/16385831.html
Copyright © 2020-2023  润新知