python操作sqllite實現增刪改查

#!/usr/bin/python
import sqlite3

#創建表
def create_table(conn):
    c = conn.cursor()
    sql = 'create table company(' \
          'id int primary key not null,' \
          'name text not null,' \
          'age int not null,' \
          'address char(50),' \
          'salary real' \
          ');'
    c.execute(sql)
    print("table create successful!")
    conn.commit()
    conn.close()

#增加
def add_data(conn):
    c = conn.cursor()
    sql = "insert into company(id,name,age,address,salary) " \
          "values(?,?,?,?,?)"
    params = [5,'nick002',25,'南陽',400]
    c.execute(sql, params)
    conn.commit()

#修改
def modify_data(conn):
    c = conn.cursor()
    sql = "update company set name=? where id=?"
    params = ['nick', 5]
    c.execute(sql, params)
    conn.commit()

#刪除
def del_data(conn):
    c = conn.cursor()
    sql = "delete from company where id=?"
    params = [5]
    c.execute(sql, params)
    conn.commit()

#查詢
def query_data(conn):
    c = conn.cursor()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章