不是可执行对象:’SELECT * FROM LoanParcel’

huangapple go评论70阅读模式
英文:

Not an executable object: 'SELECT * FROM LoanParcel'

问题

以下是翻译好的部分:

"我想通过将数据库创建为数据帧来使用它,并且我已经使用了SQLAlchemy来导入create_engine,但我遇到了'不可执行对象:'SELECT * FROM LoanParcel''的问题,其中'LoanParcel'是我要创建为数据帧的数据库名称,我应该如何修复它?

views.py

from sqlalchemy import create_engine

engine = create_engine("mysql+pymysql://mariadb:mariadb@localhost:9051/mariadb")

def user_detail(req, id):
    conn = engine.connect()
    QLoanParcel = "SELECT * FROM LoanParcel"
    dfParcel = pd.read_sql(QLoanParcel, conn)
    conn.close()
    df = dfParcel.drop(["id", "date_add", "start_date"], axis = 1)
    return render(req,'pages/user_detail.html')

希望这能帮助你解决问题。

英文:

I want to use the database by creating it as a dataframe, and I've used sqlalchemy for importing create_engine, but I'm stuck with the not an executable object: 'SELECT * FROM LoanParcel', where LoanParcel is the name of the database I want to create as a dataframe, how should I fix it?

views.py

from sqlalchemy import create_engine

engine = create_engine("mysql+pymysql://mariadb:mariadb@localhost:9051/mariadb")

def user_detail(req, id):
    conn = engine.connect()
    QLoanParcel = "SELECT * FROM LoanParcel"
    dfParcel = pd.read_sql(QLoanParcel, conn)
    conn.close()
    df = dfParcel.drop(["id", "date_add", "start_date"], axis = 1)
    return render(req,'pages/user_detail.html')

答案1

得分: 8

SQLAlchemy 2引入了重大变更您需要将SQL查询包装在[`text`][1]中以创建可执行对象

此外在您的示例中,`LoanParcel`是一个表而不是数据库

英文:

SQLAlchemy 2 introduced breaking changes, you need to wrap your SQL query in a text to create an executable object.

Also, LoanParcel is a table not a database in your example.

from sqlalchemy import create_engine, text

engine = create_engine("mysql+pymysql://mariadb:mariadb@localhost:9051/mariadb")

def user_detail(req, id):
    conn = engine.connect()
    QLoanParcel = text("SELECT * FROM LoanParcel")
    dfParcel = pd.read_sql(QLoanParcel, conn)
    conn.close()
    df = dfParcel.drop(["id", "date_add", "start_date"], axis = 1)
    return render(req,'pages/user_detail.html')

huangapple
  • 本文由 发表于 2023年2月16日 03:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定