简体   繁体   English

psycopg2 将值作为列

[英]psycopg2 takes a value as a column

I'm trying to create a table using psycopg2 and then insert outer data from some jsons.我正在尝试使用 psycopg2 创建一个表,然后从一些 json 中插入外部数据。 For this reason I defined 2 function: create_words_table() and insert_data():为此,我定义了 2 function:create_words_table() 和 insert_data():

con = psycopg2.connect(
    database="dbname",
    user="username",
    password="password",
    host="127.0.0.1",
    port="5432"
)

cur = con.cursor()


def create_words_table():

    cur.execute(""" CREATE TABLE WORDS 
        (word_ CHAR(50),
        POS_ CHAR(20),
        case_ CHAR(20),
        animacy_ CHAR(20),
        tense_ CHAR(20),
        person_ CHAR(20),
        number_ CHAR(20),
        gender_ CHAR(20),
        mood_ CHAR(20),
        tonality_ CHAR(20),
        POS_score_ REAL NOT NULL,
        NEU_score_ REAL NOT NULL,
        NEG_score_ REAL NOT NULL );""")

def insert_data(word):

    # handle_dict() is a function for getting outer data.

    if handle_dict(word) is not None:
        (word_from_dict, pos_from_dict, case_from_dict, gender_from_dict,
         mood_from_dict, number_from_dict, person_from_dict, tense_from_dict,
         tonality_from_dict, pos_score_from_dict, neu_score_from_dict,
         neg_score_from_dict) = handle_dict(word)

        cur.execute('''INSERT INTO WORDS (word_, POS_, case_, gender_, mood_, 
        number_, person_, tense_, tonality_, pos_score_, neu_score_, neg_score_)       
        VALUES (word_from_dict, pos_from_dict, case_from_dict, gender_from_dict, 
        mood_from_dict, number_from_dict, person_from_dict, tense_from_dict, 
        tonality_from_dict, pos_score_from_dict, neu_score_from_dict, 
        neg_score_from_dict)''')

        con.commit()
        con.close()

So, attempting to execute them:因此,尝试执行它们:

if __name__ == '__main__':
    create_words_table()
    logger.info("table created")
    insert_data()
    logger.info("Records inserted successfully")
    

I got further error:我得到了进一步的错误:

Traceback (most recent call last):
  File "path\to\file", line 178, in <module>
    main()
  File "path\to\file", line 172, in main
    insert_data()
  File "path\to\file", line 148, in insert_data
    cur.execute('''
psycopg2.errors.UndefinedColumn: ОШИБКА:  столбец "word_from_dict" не существует 
                               # ERROR: column "word_from_dict" does not exist ###
LINE 4:         VALUES (word_from_dict, pos_from_dict, case_from_dic...

Looking at documentation, I don't see any mistake applying simple INSERT query and can't understand why psycopg2 takes a tuple after word VALUE as a table's fields.查看文档,我没有看到应用简单 INSERT 查询的任何错误,并且无法理解为什么 psycopg2 将单词 VALUE 之后的元组作为表的字段。 If anybody could clear me this, I'll be very grateful.如果有人能帮我解决这个问题,我将不胜感激。

the problem is in your query, you need to change your query to this and pass the tuple as param:问题出在您的查询中,您需要将查询更改为此并将元组作为参数传递:

cur.execute('''INSERT INTO WORDS (word_, POS_, case_, gender_, mood_, 
        number_, person_, tense_, tonality_, pos_score_, neu_score_, neg_score_) 
   VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''',handle_dict(word))

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

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