开发者

Copy_from command to copy values of dictionary in a table

I want to insert values that ar开发者_JS百科e stored in a dictionary into a table. This dictionary contains 1000000 values.

The sample of my dictionary is :

 mydictationary: {"a":1; "b":2; "c":3; "d":4}

I am looking forward to use copy_from command (psycopg2) to copy these values into a table. Something like:

cur.copy_from (mydictationary, myTable)

But is it possible to use copy_from command inorder to copy values from dictionary??? I want to use copy_from command as this might be faster than using sql insert statement.


Normal way to do it:

cur.executemany('INSERT INTO myTable (key, value) VALUES (%s, %s)', mydictionary.items())

If you insist on using StringIO

import StringIO
dictFile = StringIO.StringIO()
for item in dictionary.iteritems():
    dictFile.write("%s\t%s\n"%item)
cur.copy_from(dictFile, 'myTable')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜