對於下面的代碼,我有地圖和包含一些信息,我想通過蟒蛇添加到SQLAlchemy的一個「cookie」的一個例子。正如你可以看到一些像Farmloc,TreasureMap和Crafts等字段都有它們指向的多個條目。我搜索了Enum,除了Enum之外找不到其他東西,但那給了我一些麻煩。有沒有比我在這裏做的更好的方法。使用Python3.6.1和SQLAlchemy的1.1.11在SQLalchemy中,我可以有一個包含多個字符串的列嗎?
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()
你的計劃辦用sqlite來交易,還是你會長期使用其他數據庫? –
我對數據庫很陌生。現在我堅持下去,除非你有其他想法。 –
您可以排序在sqlite的陣列列,但它沒有原生支持。你可以[店JSON文本列(http://docs.sqlalchemy.org/en/latest/core/custom_types.html#marshal-json-strings)。 [PostgreSQL](http://www.postgresql.org)有真正的數組類型,如果你有一天會跳轉。 –