LoginSignup
Sakuraga
@Sakuraga

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

[PostgreSQL]辞書型のようにデータを格納したい

やりたいこと

{名前1:, 名前2:}

の形でデータを格納したい。

詳しく言うと

RPGゲームを作成中なのですが、アイテムの要素を追加しようと思ったときにアイテム名所持数の辞書型でデータを管理しようとしています。
詳しく例を挙げると

items_dict = {
    "回復薬":5,
    "痺れ薬":2
}

こんな感じです。どんなデータ型を使えばいいでしょうか、また、もっと効率のよい(?)方法等あれば教えていただきたいです。

0

2Answer

Comments

  1. @Sakuraga

    Questioner
    定期的に再起動入るので、データ失われますん…( ˘ω˘ )
  2. @Sakuraga

    Questioner
    すみませんテーブルですね。自分てっきりclass作成かと思って定期的にデータ失われると言ってしまいました。やっぱりテーブルを別に作成するほうがいいのでしょうか…。

再起動でデータが失われるのであれば、pythonからCSVを読み込んでpostgresqlに入れるのはいかがでしょうか。csvならexcelみたいなので編集ができます。

import psycopg2 #import the Postgres library

#connect to the database
conn = psycopg2.connect(host='localhost',
                       dbname='mytestdb',
                       user='postgres',
                       password='')  
#create a cursor object 
#cursor object is used to interact with the database
cur = conn.cursor()

#create table with same headers as csv file
cur.execute('''create table test(name char(50), age char(50), height char(50));''')

#open the csv file using python standard file I/O
#copy file into the table just created 
f = open('file.csv','r')
cursor.copy_from(f, 'test', sep=',')
f.close()
0Like

Your answer might help someone💌