开发者

is it possible to Insert a python tuple in a postgresql database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_运维百科

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 years ago.

Improve this question

is it possible to Insert a python tuple in a postgresql database


Yes, it is. Probably the easiest way is to serialise it using e.g. marshal, pickle or json and store it in a text field.

Another approach is to use Postgres' multitude of data types e.g. array type.

Finally, if the number of elements and their types of each tuple is fixed, then you may just create this many columns and map each element to table column.


Yes it is, PostgreSQL supports array as column type.

CREATE TABLE tuples_table (
   tuple_of_strings text[],
   tuple_of_ints    integer[]
);

Then inserting is done like this:

INSERT INTO tuples_table VALUES (
('{"a","b","c"}', '{1,2}'), 
('{"e",'f... etc"}', '{3,4,5}')
);


Really we need more information. What data is inside the tuple? Is it just integers? Just strings? Is it megabytes of images?

If you had a Python tuple like (4,6,2,"Hello",7) you could insert the string '(4,6,2,"Hello",7)' into a Postgres database, but that's probably not the answer you're looking for.

You really need to figure out what data you're really trying to store before you can figure out how/where to store it.


EDIT: So the short answer is "no", you cannot store an arbitrary Python tuple in a postgres database, but there's probably some way to take whatever is inside the tuple and store it somewhere useful.


This question does not make any sense. You can insert using SQL whatever is supported by your database model. If you need a fancy mapper: look at an ORM like SQLAlchemy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜