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()