我想從字典數據插入SQLite表,我使用slqalchemy做的是,在字典中的鍵和列名是一樣的,我想插入值到表中相同的列名稱。所以這是我的代碼:插入字典到SQLlite3
#This is the class where I create a table from with sqlalchemy, and I want to
#insert my data into.
#I didn't write the __init__ for simplicity
class Sizecurve(Base):
__tablename__ = 'sizecurve'
XS = Column(String(5))
S = Column(String(5))
M = Column(String(5))
L = Column(String(5))
XL = Column(String(5))
XXL = Column(String(5))
o = Mapping() #This creates an object which is actually a dictionary
for eachitem in myitems:
# Here I populate the dictionary with keys from another list
# This gives me a dictionary looking like this: o={'S':None, 'M':None, 'L':None}
o[eachitem] = None
for eachsize in mysizes:
# Here I assign values to each key of the dictionary, if a value exists if not just None
# product_row is a class and size and stock are its attributes
if(product_row.size in o):
o[product_row.size] = product_row.stock
# I put the final object into a list
simplelist.append(o)
現在我想把從保存在simplelist中成在sizecurve表右欄的字典每個值。但我卡住了,我不知道該怎麼做?因此,例如,我有一個這樣的對象:
o= {'S':4, 'M':2, 'L':1}
而且我希望看到該行的列S值4,M列的值2等
你可以將其轉換成JSON,然後對其進行編碼,並從該字符串,然後將字符串保存到數據庫中,也可以有足夠的列各參數保存在其專欄 –