Psycopg2 / PostgreSQL 中的硬编码和冗余代码?

问题描述

我目前正在学习数据工程课程,对数据建模部分提供的示例和练习感到有点傻眼:

一切都是硬编码的,“复制粘贴”统治着这个地方......

这是一个简单的例子:

try: 
    cur.execute("INSERT INTO music_store2 (transaction_id,customer_name,cashier_name,year,albums_purchased) \
                 VALUES (%s,%s,%s)",\
                 (1,"Amanda","Sam",2000,"Rubber Soul"))
except psycopg2.Error as e: 
    print("Error: Inserting Rows")
    print (e)

try: 
    cur.execute("INSERT INTO music_store2 (transaction_id,"Let it Be"))
except psycopg2.Error as e: 
    print("Error: Inserting Rows")
    print (e)
    
try: 
    cur.execute("INSERT INTO music_store2 (transaction_id,\
                 (2,"Toby","My Generation"))
except psycopg2.Error as e: 
    print("Error: Inserting Rows")
    print (e)
    
try: 
    cur.execute("INSERT INTO music_store2 (transaction_id,\
                 (3,"Max","Bob",2018,"Help!"))
except psycopg2.Error as e: 
    print("Error: Inserting Rows")
    print (e)
    
try: 
    cur.execute("INSERT INTO music_store2 (transaction_id,"Meet the Beatles"))
except psycopg2.Error as e: 
    print("Error: Inserting Rows")
    print (e)

我必须承认,乍一看,我不禁认为这真的很糟糕,很难维护代码,而使用变量和循环会更好。

然而,想起我有时会成为一个无知和傲慢的傻瓜,我退后一步,不要急于下结论。

难道这真的是标准做法吗?

所有这些硬编码和复制粘贴在这里有充分的理由吗?

非常感谢您的评论

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...