Sqlalchemy 查询在 Sublime Text IDE 中成功,但从命令行执行时失败

Sqlalchemy Query Succeeds in Sublime Text IDE but fails when executed from the command line

大家好。我正在使用 sqlalchemy 和 pyqt 为一个大学项目构建一个相册数据库应用程序(因为这两个库我都想获得一些经验),所有这些都是用 Python 3 编写的。 我选择的 IDE 是 Sublime 3,我的数据库服务器是 Mariadb 运行 Ubuntu 14.04 64 位变体(基本 OS)。

我遇到的问题很奇怪,我不知道从哪里开始(实际上我以为我快完成了,呃!)。

如果我从 Sublime (f7) 内部执行应用程序,那么程序会按照我期望的方式运行所有查询 returning 所需的值(因此我认为我快完成了)。 但是,如果我使脚本可执行并从命令行或另一个 IDE(在本例中为 Geany)中执行它,事情就会崩溃,但只有在尝试根据用户输入提供的过滤参数进行某些查询时.程序中其他地方的查询成功,例如Songs.query.all() 将 return 数据库中的所有歌曲。

下面的代码是应用程序 'Read' 部分发生错误的片段。 'table' 引用 Table 对象(歌曲、专辑或艺术家),过滤列和子句引用用户输入的字符串参数。

    elif function == 'Read':

        # Debug Print - Display Details
        print('Table is ' + str(table))
        print('Clause is ' + clause)
        # Carry out the Query - Should only return 1 match - Fails Here
        r = table.query.filter(
            getattr(table, column.lower()) == clause).one()

        if self.db == 'Songs':

            # Query Corresponding Album & Artist
            al = Albums.query.get(int(r.album_id))
            ar = Artists.query.get(int(r.artist_id))

            headers = 'Name;Artist;Album;Track Number;Genre'
            data = [r.name, ar.name, al.name,
                    r.track_num, r.genre]

我的模型如下:

    class Artists(Base):
        __tablename__ = 'artists'

        artist_id = Column(Integer, autoincrement=True, primary_key=True)
        name = Column(String(30))

        albums = relationship(
            'Albums', backref='artists', cascade='all, delete-orphan')
        songs = relationship(
             'Songs', backref='artists', cascade='all, delete-orphan')

    class Albums(Base):
        __tablename__ = 'albums'

        album_id = Column(Integer, autoincrement=True, primary_key=True)
        name = Column(String(30), nullable=False)
        artist_id = Column(Integer, ForeignKey('artists.artist_id'))
        created_on = Column(
            DateTime(timezone=True), default=datetime.now, onupdate=datetime.now)

        songs = relationship(
            "Songs", backref="albums", cascade="all, delete-orphan")

    class Songs(Base):
        __tablename__ = 'songs'

        song_id = Column(Integer, autoincrement=True, primary_key=True)
        name = Column(String(50))
        track_num = Column(Integer)
        genre = Column(String(30))
        created_on = Column(
            DateTime(timezone=True), default=datetime.now, onupdate=datetime.now)

        album_id = Column(Integer, ForeignKey('albums.album_id'))
        artist_id = Column(Integer, ForeignKey('artists.artist_id'))

尝试匹配数据库中存在的歌曲时出现的错误是:

sqlalchemy.exc.ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 3") [SQL: 'SELECT songs.song_id AS songs_song_id, songs.name AS songs_name, songs.track_num AS songs_track_num, songs.genre AS songs_genre, songs.created_on AS songs_created_on, songs.album_id AS songs_album_id, songs.artist_id AS songs_artist_id \nFROM songs \nWHERE songs.name = %s'] [parameters: ('WHAT WENT DOWN',)]

任何有关此问题的帮助都将非常有用,如果需要任何其他信息,我将非常乐意提供。提前致谢

错误是抱怨 %s 的语法作为绑定参数。如果我没记错的话,MySQL 语法使用 ? 作为位置参数,使用 :name 作为关键字绑定参数。 %spsycopg2 库使用的,因为它有自己的语句准备。此处未显示您的引擎配置。

至于 Sublime 和来自控制台的 运行 之间的区别,请查找 (a) 正在使用的 Python 解释器中的任何潜在差异以及 (b) 由于 PYTHONPATH 问题。