简体   繁体   English

如何将数组插入到我的金字塔应用程序的数据库表的列中?

[英]How can I insert arrays into columns on a database table for my pyramid app?

I am trying to create an sql (sqlite) database where users upload an stl file and the data (all the points, triangles, ect.) is stored in a database so that it is permanently available.我正在尝试创建一个 sql (sqlite) 数据库,用户可以在其中上传 stl 文件,并且数据(所有点、三角形等)都存储在数据库中,以便永久可用。 Is it possible to do this with a database with only two columns (three with the key): name (title for the url), and data (the array data)?是否可以使用只有两列(三列带有键)的数据库来执行此操作:名称(url 的标题)和数据(数组数据)?

The data array is in the format: [[[x1,y1,z1],....],[[v1,v2,v3],...]] .数据数组的格式为: [[[x1,y1,z1],....],[[v1,v2,v3],...]] All points are given first and then the triangles are defined through ordering of the points given.首先给出所有点,然后通过给定点的排序来定义三角形。 Can this array be stored in the database, and if so, what data type would it be (integer, string, ect.)?这个数组可以存储在数据库中吗,如果可以,它是什么数据类型(整数、字符串等)?

Upon reading into this issue more, It seems that pickling would be a good way to go: http://docs.python.org/2/library/pickle.html在更多地阅读这个问题后,酸洗似乎是一个好方法: http : //docs.python.org/2/library/pickle.html

I am having trouble figuring out how to implement this.我无法弄清楚如何实现这一点。 Should I just add pickle(data)?我应该只添加泡菜(数据)吗?

Edit: upon further review, it seems like pickling introduces some security holes that do not exist if using JSON.编辑:经过进一步审查,酸洗似乎引入了一些使用 JSON 时不存在的安全漏洞。 Is it possible to simply call jsondatastring=JSON.stringify(data) and then save that to the database?是否可以简单地调用jsondatastring=JSON.stringify(data)然后将其保存到数据库中? If so, what would be the appropriate column type?如果是这样,合适的列类型是什么?

If your intention is only to store the array in DB and work with it in your webapp code, SQLAlchemy's PickleType is a good choice.如果您只想将数组存储在数据库中并在您的 web 应用程序代码中使用它,SQLAlchemy 的PickleType是一个不错的选择。 Pickling and unpickling will be done transparently for you: Pickling 和 unpickling 将为您透明地完成:

from sqlalchemy.types import PickleType

class Foo(Base):
    __tablename__ = 'foo'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    array = Column(PickleType)

foo = Foo(name=u'bar', array=[1, 2, 3])
session.add(foo)
session.commit()

foo = session.query(Foo).filter_by(name=u'bar').one()
print foo.array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM