I have a raw SQL query with more than 80 lines and which results I can't reproduce using querysets. I want now to make this query compatible with both SQLite and PostgreSQL, so it can be run on development and production environments.
Is there any recommended or built-in way to proceed?
Example
So, for example, there's this PostgreSQL syntax error that I'm getting:
operator does not exist: boolean = integer
I adjusted the syntax and used some conditions to get the right SQL string for each case, but that just feels wrong to me:
db_engine = ''
engine = settings.DATABASES['default']['ENGINE']
if re.match('.*sqlite3$', engine):
db_engine = 'sqlite3'
elif re.match('.*psycopg2$', engine):
db_engine = 'postgresql'
elif re.match('.*mysql$', engine):
db_engine = 'mysql'
cursor = connection.cursor()
cursor.execute("""
... some big query here ...
WHERE 1=1
AND some_boolean_field = %(value)s
""" % {
'value': 'TRUE' if db_engine == 'postgresql' else '1'
})