Python Readability for SQL query I have this piece of code in Python 2.7:
As you can see is very hard to read the SQL query, what is the best way to re-write it?

    try:
        if title and url:
            sql_query = "INSERT INTO news (title, author, description, " \
                        "content, url, url_to_image, source_id, source, " \
                        "campaign, published_at, score, magnitude, sentiment, " \
                        "inserted_at ) VALUES ('%s','%s','%s','%s'," \
                        "'%s','%s','%s','%s','%s','%s', %s, %s, '%s', '%s')" \
                        % ( title.replace("'", "''"),
                            author.replace("'", "''"),
                            description.replace("'", "''"),
                            content[:settings.content_size].replace("'", "''"),
                            url,
                            url_to_image,
                            source_id,
                            source,
                            campaign,
                            published_at,
                            score,
                            magnitude,
                            sentiment,
                            DB_NOW)
            db = Db.Db()
            db.initialize(dsn=settings.SQLALCHEMY_DSN)
            return db.insert_content(sql_query, 'news_id')
    except psycopg2.ProgrammingError as exception:
        log.exception(exception)`enter code here`
You can use multiline sting """ and string.format

try:
    if title and url:
        sql_query = """
        INSERT INTO news (
        title, author, description, content, url, url_to_image, source_id,
        source, campaign, published_at, score, magnitude, sentiment, inserted_at
         ) VALUES (
         '{title}','{author}','{description}','{content}', '{url}',
         '{url_to_image}','{source_id}','{source}', '{campaign}',
         '{published_at}', {score}, {magnitude}, '{sentiment}', '{DB_NOW}'
         )
        """.format(
            title=title.replace("'", "''"),
            author=author.replace("'", "''"),
            description=description.replace("'", "''"),
            content=content[: settings.content_size].replace("'", "''"),
            url=url,
            url_to_image=url_to_image,
            source_id=source_id,
            source=source,
            campaign=campaign,
            published_at=published_at,
            score=score,
            magnitude=magnitude,
            sentiment=sentiment,
            DB_NOW=DB_NOW,
        )

        db = Db.Db()
        db.initialize(dsn=settings.SQLALCHEMY_DSN)
        return db.insert_content(sql_query, "news_id")
except psycopg2.ProgrammingError as exception:
    log.exception(exception)`enter code here`


If you'd use python 3.6+, you could even simpler use f-strings

string interpolation

As noted by @Ludisposed, string interpolation is not the way to go link
Since it is unclear what the Db.Db is, I assume it is something you wrote yourself. You will have to adapt the insert_content code to accept the arguments, and then do something like this:

try:
    if title and url:
        sql_query = """
        INSERT INTO news (
        title, author, description, content, url, url_to_image, source_id,
        source, campaign, published_at, score, magnitude, sentiment, inserted_at
         ) VALUES (
         %(title)s,%(author)s,%(description)s,%(content)s, %(url)s,
         %(url_to_image)s,%(source_id)s,%(source)s, %(campaign)s,
         %(published_at)s, %(score)s, %(magnitude)s, %(sentiment)s, %(DB_NOW)s
         )
        """
        arguments = dict(
            title=title,
            author=author,
            description=description,
            content=content[: settings.content_size],
            url=url,
            url_to_image=url_to_image,
            source_id=source_id,
            source=source,
            campaign=campaign,
            published_at=published_at,
            score=score,
            magnitude=magnitude,
            sentiment=sentiment,
            DB_NOW=DB_NOW,
        )

        db = Db.Db()
        db.initialize(dsn=settings.SQLALCHEMY_DSN)
        return db.insert_content(sql_query, arguments, "news_id")
except psycopg2.ProgrammingError as exception:
    log.exception(exception)