代碼示例sql
#導入sqlite3庫 import sqlite3 #鏈接數據庫 conn = sqlite3.connect("customers.db") #建立表,主鍵id自增 conn.execute('''CREATE TABLE IF NOT EXISTS tb_user (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL);''') #向表中插入記錄 name = 'Jay' age = 28 sql = "insert into tb_user(name,age)values('%s',%d)" % (name, age) conn.execute(sql) conn.commit() # 查詢記錄 cursor = conn.execute("select id,name,age from tb_user") values = cursor.fetchall() print(values) #關閉數據庫鏈接 cusor.close()conn.close()