gpt4 book ai didi

python - 在 SQLalchemy 中,我可以有一个包含多个字符串的列吗?

转载 作者:行者123 更新时间:2023-12-05 00:50:27 32 4
gpt4 key购买 nike

对于以下代码,我有映射和一个“cookie”示例,其中包含我想通过 python 添加到 SQLalchemy 的一些信息。正如您所看到的,Farmloc、TreasureMap 和 Crafts 等一些字段都有指向的多个条目。我搜索并找不到除 Enum 以外的其他内容,但这给我带来了一些麻烦。有没有比我在这里所做的更好的方法。使用 Python3.6.1 和 SQLalchemy 1.1.11

import csv, sqlalchemy
from sqlalchemy import Column, String, Integer, ForeignKey, Numeric, Boolean
from sqlalchemy.ext.declarative import declarative_base
import sqlite3
from enum import Enum, auto
import enum
from sqlalchemy import create_engine

engine = create_engine('sqlite:///:memory:', echo=True) #TEST DB

Base = declarative_base()

class Crystal(Base):
__tablename__ = 'crystals'
dbID = Column(Integer, primary_key=True)
ItemName = Column(String, index=True, nullable=False) #Full name, (ie. Earth Shard, Wind Crystal, Fire Cluster)
ItemType = Column(String) #Fire, Earth, Wind, Ice, Water ONLY
ItemPow = Column(String) #Shard, Crystal, Cluster ONLY
Farmloc = Column(String) #Where player can farm
Retainer = Column(Boolean) #If retainer can farm
RetainerQ = Column(Integer) #Quantity retainer gets
Levee = Column(Boolean) #Any Levees rewards these.
TreasureMap = Column(String) #False=NO, otherwise Types listed
Desynthed = Column(String) #False=NO, otherwise Herb1, Herb2, Ore1, etc
Crafts = Column(String) #Crafts associated with (ie Earth w LWR)
Price = Column(Integer) #MB price (Should be used as ref for all craftables)

from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()

cc_cookie = Crystal(dbID = 1,
ItemName= 'Wind Cluster',
ItemType = 'Wind',
ItemPow = 'Cluster',
Farmloc = 'The Dravanian Hinterlands, Mor Dhona',
Retainer = False,
RetainerQ = 0,
Levee = False,
TreasureMap = 'Dragonskin Treasure Map,Gaganaskin Treasure Map,Gazelleskin Treasure Map,Leather Buried Treasure Map',
Desynthed = 'Clary Sage,Furymint,Highland Oregano,Windtea Leaves',
Crafts = 'CRP,GSM,LWR,WVR',
Price = 500)


Base.metadata.create_all(engine)

session.add(cc_cookie)
session.commit()

最佳答案

查看内置 class sqlalchemy.types.ARRAY

例如:
TreasureMap = Column(ARRAY(String))
旁注:按照惯例,所有列名都是小写的,并以分数分隔(例如 treasure_map over TreasureMap 。)

关于python - 在 SQLalchemy 中,我可以有一个包含多个字符串的列吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604488/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com