将 Postgresql 添加到我的 Heroku 应用程序 - 语法崩溃查询的问题?
Adding Postgresql to my Heroku app - issues with syntax crashing queries?
我正在自学 Python、Flask,并将 Postgresql 添加到组合中。我最近在 Heroku 上部署了我的第一个应用程序,它根本不使用数据库,但现在我想添加功能。然而,我被困在这个过程中感觉非常简单的一步。请记住,我对 postgres 非常非常陌生。我在本地主机数据库上通过 MySQL 使所有这些功能完美运行,但无法在 Postgres 中远程运行:(
我的 check_connection() 有效,我的 create_table() 有效,但我的 pg_add_data() 失败,如您在底部看到的那样。我正在 PyCharm CE 中进行所有编码。
import psycopg2
# connect to remote Heroku Postgres DB
pgdb = psycopg2.connect(
host='ec2-54-211-160-34.compute-1.amazonaws.com',
user='xxxxxxxx',
password='xxxxxxxxxxxxxxxxxxxxxxxx',
port='5432',
database='df5p5d20v6pbf9'
)
pgcursor = pgdb.cursor()
# verify I've got my credentials in correctly
def check_connection():
pgcursor.execute("select version()")
data = pgcursor.fetchone()
print("Connection established to: ", data)
def create_table():
sql = '''CREATE TABLE estimation_data
(User text PRIMARY KEY,
Motor_kV integer,
Batt_Volt decimal,
Pinion integer,
Spur integer,
Final_Gearing decimal,
Wheel_Rad decimal);'''
pgcursor.execute(sql)
pgdb.commit()
def pg_add_data(sql,val):
pgcursor.executemany(sql, val)
pgdb.commit()
pgsql = '''INSERT INTO estimation_data
(User, Motor_kV, Batt_Volt, Pinion, Spur, Final_Ratio, Wheel_Rad)
VALUES (%s, %s, %s, %s, %s, %s, %s);'''
pgval = [
('204.210.165.122', 2400, 16.8, 16, 54, 3.92, 2.5),
('204.210.165.123', 3500, 12.6, 16, 54, 3.92, 2.5),
('204.210.165.124', 4200, 8.4, 12, 54, 3.92, 2.5)]
pg_add_data(pgsql,pgval)
当我执行底部的 pg_add_data 行时,我得到:
Traceback (most recent call last):
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 119, in <module>
pg_add_data(pgsql,pgval)
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 96, in pg_add_data
pgcursor.executemany(sql, val)
psycopg2.errors.SyntaxError: syntax error at or near "User"
LINE 2: (User, Motor_kV, Batt_Volt, Pinion, Spur, Final_Ratio, Wh...
^
Process finished with exit code 1
我试图破译 User.... 处或附近有什么语法错误的答案。我被难住了。我一直在尝试按照教程 https://www.tutorialspoint.com/python_data_access/python_postgresql_insert_data.htm with the help of https://www.postgresqltutorial.com/ 进行操作。我也安装了 pgAdmin4,但在我学习的这个阶段我对那个程序感到困惑(到目前为止我更喜欢 MySQL Workbench)。
非常感谢所有帮助和提示!
编辑 1:根据 charchit 的建议,尝试在列名周围使用“”,结果如下:
Traceback (most recent call last):
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 119, in <module>
pg_add_data(pgsql,pgval)
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 96, in pg_add_data
pgcursor.executemany(sql, val)
psycopg2.errors.UndefinedColumn: column "User" of relation "estimation_data" does not exist
LINE 2: (`User`, `Motor_kV`, `Batt_Volt`, `Pinion`, `Spur`, `Fina...
^
Process finished with exit code 1
User
是 postgresql 中的保留字,通过用 ""
括起来进行转义参见 。与转义字符为反引号的 mysql 相反。
您还尝试插入名为 Final_Ratio
的列,但定义了一个列 Final_Gearing
。为了测试,我还删除并重新创建了 table 每个 运行。固定码:
import psycopg2
# connect to remote Heroku Postgres DB
pgdb = psycopg2.connect(
host='localhost',
user='test',
password='test',
port='5432',
database='test_Whosebug'
)
pgcursor = pgdb.cursor()
# verify I've got my credentials in correctly
def check_connection():
pgcursor.execute("select version()")
data = pgcursor.fetchone()
print("Connection established to: ", data)
def create_table():
sql = '''CREATE TABLE estimation_data
("User" text PRIMARY KEY,
Motor_kV integer,
Batt_Volt decimal,
Pinion integer,
Spur integer,
Final_Gearing decimal,
Wheel_Rad decimal);'''
pgcursor.execute(sql)
pgdb.commit()
def drop_table():
sql = "DROP TABLE IF EXISTS estimation_data;"
pgcursor.execute(sql)
pgdb.commit()
def pg_add_data(sql,val):
pgcursor.executemany(sql, val)
pgdb.commit()
check_connection()
drop_table()
create_table()
pgsql = '''INSERT INTO estimation_data
("User", Motor_kV, Batt_Volt, Pinion, Spur, Final_Gearing, Wheel_Rad)
VALUES (%s, %s, %s, %s, %s, %s, %s);'''
pgval = [
('204.210.165.122', 2400, 16.8, 16, 54, 3.92, 2.5),
('204.210.165.123', 3500, 12.6, 16, 54, 3.92, 2.5),
('204.210.165.124', 4200, 8.4, 12, 54, 3.92, 2.5)]
pg_add_data(pgsql, pgval)
虽然学习生产站点的基础知识很好,但我强烈建议使用像 SQLalchemy 这样的高级库。
我正在自学 Python、Flask,并将 Postgresql 添加到组合中。我最近在 Heroku 上部署了我的第一个应用程序,它根本不使用数据库,但现在我想添加功能。然而,我被困在这个过程中感觉非常简单的一步。请记住,我对 postgres 非常非常陌生。我在本地主机数据库上通过 MySQL 使所有这些功能完美运行,但无法在 Postgres 中远程运行:(
我的 check_connection() 有效,我的 create_table() 有效,但我的 pg_add_data() 失败,如您在底部看到的那样。我正在 PyCharm CE 中进行所有编码。
import psycopg2
# connect to remote Heroku Postgres DB
pgdb = psycopg2.connect(
host='ec2-54-211-160-34.compute-1.amazonaws.com',
user='xxxxxxxx',
password='xxxxxxxxxxxxxxxxxxxxxxxx',
port='5432',
database='df5p5d20v6pbf9'
)
pgcursor = pgdb.cursor()
# verify I've got my credentials in correctly
def check_connection():
pgcursor.execute("select version()")
data = pgcursor.fetchone()
print("Connection established to: ", data)
def create_table():
sql = '''CREATE TABLE estimation_data
(User text PRIMARY KEY,
Motor_kV integer,
Batt_Volt decimal,
Pinion integer,
Spur integer,
Final_Gearing decimal,
Wheel_Rad decimal);'''
pgcursor.execute(sql)
pgdb.commit()
def pg_add_data(sql,val):
pgcursor.executemany(sql, val)
pgdb.commit()
pgsql = '''INSERT INTO estimation_data
(User, Motor_kV, Batt_Volt, Pinion, Spur, Final_Ratio, Wheel_Rad)
VALUES (%s, %s, %s, %s, %s, %s, %s);'''
pgval = [
('204.210.165.122', 2400, 16.8, 16, 54, 3.92, 2.5),
('204.210.165.123', 3500, 12.6, 16, 54, 3.92, 2.5),
('204.210.165.124', 4200, 8.4, 12, 54, 3.92, 2.5)]
pg_add_data(pgsql,pgval)
当我执行底部的 pg_add_data 行时,我得到:
Traceback (most recent call last):
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 119, in <module>
pg_add_data(pgsql,pgval)
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 96, in pg_add_data
pgcursor.executemany(sql, val)
psycopg2.errors.SyntaxError: syntax error at or near "User"
LINE 2: (User, Motor_kV, Batt_Volt, Pinion, Spur, Final_Ratio, Wh...
^
Process finished with exit code 1
我试图破译 User.... 处或附近有什么语法错误的答案。我被难住了。我一直在尝试按照教程 https://www.tutorialspoint.com/python_data_access/python_postgresql_insert_data.htm with the help of https://www.postgresqltutorial.com/ 进行操作。我也安装了 pgAdmin4,但在我学习的这个阶段我对那个程序感到困惑(到目前为止我更喜欢 MySQL Workbench)。
非常感谢所有帮助和提示!
编辑 1:根据 charchit 的建议,尝试在列名周围使用“”,结果如下:
Traceback (most recent call last):
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 119, in <module>
pg_add_data(pgsql,pgval)
File "C:\Users\prodsupervisor\PycharmProjects\RC_Speed_Estimator\db_handling.py", line 96, in pg_add_data
pgcursor.executemany(sql, val)
psycopg2.errors.UndefinedColumn: column "User" of relation "estimation_data" does not exist
LINE 2: (`User`, `Motor_kV`, `Batt_Volt`, `Pinion`, `Spur`, `Fina...
^
Process finished with exit code 1
User
是 postgresql 中的保留字,通过用 ""
括起来进行转义参见 。与转义字符为反引号的 mysql 相反。
您还尝试插入名为 Final_Ratio
的列,但定义了一个列 Final_Gearing
。为了测试,我还删除并重新创建了 table 每个 运行。固定码:
import psycopg2
# connect to remote Heroku Postgres DB
pgdb = psycopg2.connect(
host='localhost',
user='test',
password='test',
port='5432',
database='test_Whosebug'
)
pgcursor = pgdb.cursor()
# verify I've got my credentials in correctly
def check_connection():
pgcursor.execute("select version()")
data = pgcursor.fetchone()
print("Connection established to: ", data)
def create_table():
sql = '''CREATE TABLE estimation_data
("User" text PRIMARY KEY,
Motor_kV integer,
Batt_Volt decimal,
Pinion integer,
Spur integer,
Final_Gearing decimal,
Wheel_Rad decimal);'''
pgcursor.execute(sql)
pgdb.commit()
def drop_table():
sql = "DROP TABLE IF EXISTS estimation_data;"
pgcursor.execute(sql)
pgdb.commit()
def pg_add_data(sql,val):
pgcursor.executemany(sql, val)
pgdb.commit()
check_connection()
drop_table()
create_table()
pgsql = '''INSERT INTO estimation_data
("User", Motor_kV, Batt_Volt, Pinion, Spur, Final_Gearing, Wheel_Rad)
VALUES (%s, %s, %s, %s, %s, %s, %s);'''
pgval = [
('204.210.165.122', 2400, 16.8, 16, 54, 3.92, 2.5),
('204.210.165.123', 3500, 12.6, 16, 54, 3.92, 2.5),
('204.210.165.124', 4200, 8.4, 12, 54, 3.92, 2.5)]
pg_add_data(pgsql, pgval)
虽然学习生产站点的基础知识很好,但我强烈建议使用像 SQLalchemy 这样的高级库。