mardi 7 mars 2017

Verifying SQL Query before Connectining to PostgresSQL

I've been looking for a while and haven't found anything..so apologizes if I just missed something. But I would like to find a way to verify my query before connecting to the database via unit tests. My thought is that this can be done in one of two ways...1) via a library whose file reader specifically reads in .sql files and checks for correct syntax, or just 2) some sort of method what can verify a query string for me.

My code looks similar to this:

import psycopg2


def query_db(self):

    connection = psycopg2.connect()
    connection.cursor().execute("SELECT A FROM B;")

So for 1) I'm wondering if there's some sort of file reader where I can save my query in a .sql file, and then when it reads in the file, if there's a problem, then it will thrown some sort of error. Then in my tests, an exception will be thrown when testing query_db if there is a syntax error with my query.

For 2), if there's a method that can just verify the query string..then again I could just run the query through this method before executing the query, and throw an error if there's a problem, so it'll be caught in the unit tests.

Any suggestions? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire