我正在 Python 为我的雇主创建库存系统,我应该将库存信息存储在 SQLite 数据库中吗?
I am creating an inventory system in Python for my employer, should I store inventory information in an SQLite database?
我已经开始在 Python 为我的雇主创建库存系统。我的前端 GUI 已基本完成,但对后端有疑问。
我应该如何存储信息?
该程序将位于一个可供多人同时访问的保管箱文件夹中。我应该使用 SQLite 数据库,并在执行 "add stock" 等函数时,打开连接,执行更改,然后关闭连接吗?这会允许多个用户同时打开库存吗?有没有更好的方法来处理这个问题?
库存物品不会很多。使用 python 对象和对象方法来存储和操作信息会更好吗?
您可以使用 Python 轻松操作 sqlite 数据库。
https://docs.python.org/3/library/sqlite3.html
import sqlite3 as sql
class SQLManiuplator:
def __init__(self, fp=None):
path = fp
if not fp:
path = "example.db"
self.path = path
def add_item_to_inventorytable(self, item):
with sql.connect(self.path) as conn: # close automatically
c = conn.cursor()
# Use '?' substitution to sanitize inputs per sqlite3 docs
c.execute('''
INSERT INTO inventory VALUES (?)''', item)
c.commit()
# More CRUD functions
您可以使用此模式构建漂亮的 CRUD api 并将操作映射到您的 GUI。从设计的角度来看,然后由您来决定您愿意向用户公开哪些操作。例如,我认为您不想让 CREATE TABLE
对用户可用,所以我没有包含它。 (但这意味着您必须手动创建表并定义其中存在的列。)
您已经说过这将是一个非常轻量级的库存系统,但不用说,在企业规模上,这个解决方案是一个失败者。
我已经开始在 Python 为我的雇主创建库存系统。我的前端 GUI 已基本完成,但对后端有疑问。
我应该如何存储信息?
该程序将位于一个可供多人同时访问的保管箱文件夹中。我应该使用 SQLite 数据库,并在执行 "add stock" 等函数时,打开连接,执行更改,然后关闭连接吗?这会允许多个用户同时打开库存吗?有没有更好的方法来处理这个问题?
库存物品不会很多。使用 python 对象和对象方法来存储和操作信息会更好吗?
您可以使用 Python 轻松操作 sqlite 数据库。
https://docs.python.org/3/library/sqlite3.html
import sqlite3 as sql
class SQLManiuplator:
def __init__(self, fp=None):
path = fp
if not fp:
path = "example.db"
self.path = path
def add_item_to_inventorytable(self, item):
with sql.connect(self.path) as conn: # close automatically
c = conn.cursor()
# Use '?' substitution to sanitize inputs per sqlite3 docs
c.execute('''
INSERT INTO inventory VALUES (?)''', item)
c.commit()
# More CRUD functions
您可以使用此模式构建漂亮的 CRUD api 并将操作映射到您的 GUI。从设计的角度来看,然后由您来决定您愿意向用户公开哪些操作。例如,我认为您不想让 CREATE TABLE
对用户可用,所以我没有包含它。 (但这意味着您必须手动创建表并定义其中存在的列。)
您已经说过这将是一个非常轻量级的库存系统,但不用说,在企业规模上,这个解决方案是一个失败者。