欢迎投稿

今日深度:

Python:如何使用SQLite数据库,pythonsqlite数据库

Python:如何使用SQLite数据库,pythonsqlite数据库


在数据库支持方面,Python提供了很简便的接口,可以很容易地连接到MySQL、SQLite等各种各样的数据库。

SQLite是一种轻量化的文件型数据库,默认是直接使用文件的形式在本地计算机就可以直接拥有操作数据库的优势。也就说,虽然SQLite把数据库以文件的形式表现,但具有数据库的操作功能,通过SQL指令,进行选择、插入、更新和删除的操作。

1、SQLite数据库的创建

可以利用可视化的工具,如Firefox 的SQLite  Manager附加组件,进行创建。

2、SQLite数据库的使用

在Python中使用SQLite数据库,需要以下几个关键步骤:

  • 导入sqlite3模块包:import sqlite3
  • 通过sqlite3的connect()函数连接已经存在的数据库,获得操作数据库的句柄:conn = sqlite3.connect(数据库名称)
  • 利用句柄的执行命令执行SQL指令:conn.execute(sql指令串)
  • 利用句柄的提交指令把操作结果提交给数据库:conn.commit()
  • 最后要关闭数据库的连接:conn.close()

3、SQLite数据库的操作实例

----------用门市销售管理的入库、库存整理、销售、查看库存分别进行数据库的插入(insert)、更新(update)、删除(delete)和选择(selete)操作。

import sqlite3

#显示项目菜单
def display_menu():
    print("门市销售")
    print("---------------------------")
    print("1.入库")
    print("2.整理")
    print("3.销售")
    print("4.显示所有库存")
    print("0.退出")
    print("---------------------------")


#入库
def append_data():
    while True:
        goodsNumber = int(input("请输入商品编号(-1停止输入):"))
        if goodsNumber == -1: break
        name = input("请输入商品名称:")
        sqlStr = "select * from goods where goods_no={};".format(goodsNumber)
        cursor = conn.execute(sqlStr)
        if len(cursor.fetchall()) > 0:
            print("你输入的商品编号已经有数据了")
        else:
            sqlStr = \
            "insert into goods values({},'{}');"\
                .format(goodsNumber,name)
            conn.execute(sqlStr)
            conn.commit()

#整理
def update_data():
    goodsNumber = input("请输入要整理的商品编号:")
    sqlStr = \
    "select * from goods where goods_no={};".format(goodsNumber)
    cursor = conn.execute(sqlStr)
    rows = cursor.fetchall()
    if len(rows) > 0:
        print("当前的商品名称:",rows[0][1])
        name = input("请输入商品名称:")
        sqlStr = \
        "update goods set name ='{}' where goods_no={};".format(name, goodsNumber)
        conn.execute(sqlStr)
        conn.commit()
    else:
        print("找不到要整理的商品编号!")

#销售
def sales_data():
    goodsNumber = input("请输入要销售的产品编号:")
    sqlStr = "select * from goods where goods_no={};".format(goodsNumber)
    cursor = conn.execute(sqlStr)
    rows = cursor.fetchall()
    if len(rows) > 0:
        print("你当前要销售的产品编号{}的{}".format(rows[0][0], rows[0][1]))
        answer = input("确定要销售吗?(y/n)")
        if answer == 'y' or answer == 'Y':
            sqlStr = "delete from goods where goods_no={};".format(goodsNumber)
            conn.execute(sqlStr)
            conn.commit()
            print("已销售指定的商品...")
        else:
            print("找不到要销售的商品!")

#显示库存
def display_data():
    cursor = conn.execute('select * from goods;')
    for row in cursor:
        print("商品编号:{}:  {}".format(row[0], row[1]))

conn = sqlite3.connect('d:\PythonTest\salesManager_db.sqlite')

while True:
    display_menu()
    choice = int(input("请输入你的选择:"))
    if choice == 0:
        conn.close()
        break
    if choice == 1:
        append_data()
    elif choice == 2:
        update_data()
    elif choice == 3:
        sales_data()
    elif choice == 4:
        display_data()
    else:break
    x = input("请按Enter键返回主菜单")


www.htsjk.Com true http://www.htsjk.com/SQLite/31511.html NewsArticle Python:如何使用SQLite数据库,pythonsqlite数据库 在数据库支持方面,Python提供了很简便的接口,可以很容易地连接到MySQL、SQLite等各种各样的数据库。 SQLite是一种轻量化的文件型数据库,...
相关文章
    暂无相关文章
评论暂时关闭