Inserting None value into Date column from python to PostgreSQL database using PygreSQL v5.0.6
I am strugeling to insert None values into the date collumn of a PostgreSQL database from python using PygreSQL v5.0.6.
Some code:
def _update_traits_db(self, code, date_start, date_end, unit):
sql = ("Insert into traits (code, date_start, date_end, unit) "
"VALUES ('%s', '%s', '%s', '%s') "
"ON CONFLICT (code) DO UPDATE "
"SET date_start = excluded.date_start, date_end = excluded.date_end, unit = excluded.unit "
% (code, date_start, date_end, unit))
try:
self._connect()
self._cur.execute(sql)
self._con.commit()
self._close()
except Exception as e:
self._close()
raise e
There are a couple issues i am facing, the biggest being the the possibility of None values for "date_end" and "unit", the first being a date causing SQL errors like:
ERROR: invalid input syntax for type date: "None"
LINE 1: ...d, unit) VALUES ('AWGHT', '2003-01-29T23:00:00Z', 'None', 'N...
^
If i replace the none value with a hardcoded NULL then it works but from reading around i figured it should be handled py PyGreSQL automatically converting None to NULL but i cant get that to work.
A second issue is with None values in the "unit" column, this is supposed to be a string but "None" is now stored in the database where it idealy would be a NULL value. I have tried removing the quotes from the around the '%s' for unit in the query vut that only causes SQL error on the None value.
I am fairly new to Python and PostgreSQL so there are many potential places i might have messed up so all suggestions are more than welcome.
python postgresql pygresql
|
show 8 more comments
I am strugeling to insert None values into the date collumn of a PostgreSQL database from python using PygreSQL v5.0.6.
Some code:
def _update_traits_db(self, code, date_start, date_end, unit):
sql = ("Insert into traits (code, date_start, date_end, unit) "
"VALUES ('%s', '%s', '%s', '%s') "
"ON CONFLICT (code) DO UPDATE "
"SET date_start = excluded.date_start, date_end = excluded.date_end, unit = excluded.unit "
% (code, date_start, date_end, unit))
try:
self._connect()
self._cur.execute(sql)
self._con.commit()
self._close()
except Exception as e:
self._close()
raise e
There are a couple issues i am facing, the biggest being the the possibility of None values for "date_end" and "unit", the first being a date causing SQL errors like:
ERROR: invalid input syntax for type date: "None"
LINE 1: ...d, unit) VALUES ('AWGHT', '2003-01-29T23:00:00Z', 'None', 'N...
^
If i replace the none value with a hardcoded NULL then it works but from reading around i figured it should be handled py PyGreSQL automatically converting None to NULL but i cant get that to work.
A second issue is with None values in the "unit" column, this is supposed to be a string but "None" is now stored in the database where it idealy would be a NULL value. I have tried removing the quotes from the around the '%s' for unit in the query vut that only causes SQL error on the None value.
I am fairly new to Python and PostgreSQL so there are many potential places i might have messed up so all suggestions are more than welcome.
python postgresql pygresql
You don't need the'
s around the placeholders... just use%s
by itself... eg:values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...
– Jon Clements♦
Nov 13 '18 at 14:19
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
So your date is a string then? If you make it adatetime
object instead - it'll work fine.
– Jon Clements♦
Nov 13 '18 at 14:40
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15
|
show 8 more comments
I am strugeling to insert None values into the date collumn of a PostgreSQL database from python using PygreSQL v5.0.6.
Some code:
def _update_traits_db(self, code, date_start, date_end, unit):
sql = ("Insert into traits (code, date_start, date_end, unit) "
"VALUES ('%s', '%s', '%s', '%s') "
"ON CONFLICT (code) DO UPDATE "
"SET date_start = excluded.date_start, date_end = excluded.date_end, unit = excluded.unit "
% (code, date_start, date_end, unit))
try:
self._connect()
self._cur.execute(sql)
self._con.commit()
self._close()
except Exception as e:
self._close()
raise e
There are a couple issues i am facing, the biggest being the the possibility of None values for "date_end" and "unit", the first being a date causing SQL errors like:
ERROR: invalid input syntax for type date: "None"
LINE 1: ...d, unit) VALUES ('AWGHT', '2003-01-29T23:00:00Z', 'None', 'N...
^
If i replace the none value with a hardcoded NULL then it works but from reading around i figured it should be handled py PyGreSQL automatically converting None to NULL but i cant get that to work.
A second issue is with None values in the "unit" column, this is supposed to be a string but "None" is now stored in the database where it idealy would be a NULL value. I have tried removing the quotes from the around the '%s' for unit in the query vut that only causes SQL error on the None value.
I am fairly new to Python and PostgreSQL so there are many potential places i might have messed up so all suggestions are more than welcome.
python postgresql pygresql
I am strugeling to insert None values into the date collumn of a PostgreSQL database from python using PygreSQL v5.0.6.
Some code:
def _update_traits_db(self, code, date_start, date_end, unit):
sql = ("Insert into traits (code, date_start, date_end, unit) "
"VALUES ('%s', '%s', '%s', '%s') "
"ON CONFLICT (code) DO UPDATE "
"SET date_start = excluded.date_start, date_end = excluded.date_end, unit = excluded.unit "
% (code, date_start, date_end, unit))
try:
self._connect()
self._cur.execute(sql)
self._con.commit()
self._close()
except Exception as e:
self._close()
raise e
There are a couple issues i am facing, the biggest being the the possibility of None values for "date_end" and "unit", the first being a date causing SQL errors like:
ERROR: invalid input syntax for type date: "None"
LINE 1: ...d, unit) VALUES ('AWGHT', '2003-01-29T23:00:00Z', 'None', 'N...
^
If i replace the none value with a hardcoded NULL then it works but from reading around i figured it should be handled py PyGreSQL automatically converting None to NULL but i cant get that to work.
A second issue is with None values in the "unit" column, this is supposed to be a string but "None" is now stored in the database where it idealy would be a NULL value. I have tried removing the quotes from the around the '%s' for unit in the query vut that only causes SQL error on the None value.
I am fairly new to Python and PostgreSQL so there are many potential places i might have messed up so all suggestions are more than welcome.
python postgresql pygresql
python postgresql pygresql
asked Nov 13 '18 at 14:18
KevinKevin
83
83
You don't need the'
s around the placeholders... just use%s
by itself... eg:values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...
– Jon Clements♦
Nov 13 '18 at 14:19
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
So your date is a string then? If you make it adatetime
object instead - it'll work fine.
– Jon Clements♦
Nov 13 '18 at 14:40
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15
|
show 8 more comments
You don't need the'
s around the placeholders... just use%s
by itself... eg:values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...
– Jon Clements♦
Nov 13 '18 at 14:19
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
So your date is a string then? If you make it adatetime
object instead - it'll work fine.
– Jon Clements♦
Nov 13 '18 at 14:40
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15
You don't need the
'
s around the placeholders... just use %s
by itself... eg: values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...– Jon Clements♦
Nov 13 '18 at 14:19
You don't need the
'
s around the placeholders... just use %s
by itself... eg: values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...– Jon Clements♦
Nov 13 '18 at 14:19
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
So your date is a string then? If you make it a
datetime
object instead - it'll work fine.– Jon Clements♦
Nov 13 '18 at 14:40
So your date is a string then? If you make it a
datetime
object instead - it'll work fine.– Jon Clements♦
Nov 13 '18 at 14:40
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15
|
show 8 more comments
1 Answer
1
active
oldest
votes
Simply use parameterization, the industry standard to separate SQL code from data values, and not string interpolation which looks similar since the placeholder %s
is used in both. With this approach, None
should resolve as NULL
. In fact, PygreSQL docs even warns users on the practice:
Warning
Remember to never insert parameters directly into your queries
using the % operator. Always pass the parameters separately.
Consider following adjustment using unquoted %s
placeholders (see docs) with values later binded in cursor.execute()
call:
def _update_traits_db(self, code, date_start, date_end, unit):
# PREPARED STATEMENT (NO DATA VALUES INTERPOLATED)
sql = """INSERT INTO traits (code, date_start, date_end, unit)
VALUES (%s, %s, %s, %s)
ON CONFLICT (code) DO UPDATE
SET date_start = excluded.date_start,
date_end = excluded.date_end,
unit = excluded.unit
"""
try:
self._connect()
# BIND PARAMETERS WITH TUPLE OF VALUES
self._cur.execute(sql, (code, date_start, date_end, unit))
self._con.commit()
except Exception as e:
raise e
finally:
self._close()
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, thiscursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!
– Parfait
Nov 13 '18 at 16:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283046%2finserting-none-value-into-date-column-from-python-to-postgresql-database-using-p%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply use parameterization, the industry standard to separate SQL code from data values, and not string interpolation which looks similar since the placeholder %s
is used in both. With this approach, None
should resolve as NULL
. In fact, PygreSQL docs even warns users on the practice:
Warning
Remember to never insert parameters directly into your queries
using the % operator. Always pass the parameters separately.
Consider following adjustment using unquoted %s
placeholders (see docs) with values later binded in cursor.execute()
call:
def _update_traits_db(self, code, date_start, date_end, unit):
# PREPARED STATEMENT (NO DATA VALUES INTERPOLATED)
sql = """INSERT INTO traits (code, date_start, date_end, unit)
VALUES (%s, %s, %s, %s)
ON CONFLICT (code) DO UPDATE
SET date_start = excluded.date_start,
date_end = excluded.date_end,
unit = excluded.unit
"""
try:
self._connect()
# BIND PARAMETERS WITH TUPLE OF VALUES
self._cur.execute(sql, (code, date_start, date_end, unit))
self._con.commit()
except Exception as e:
raise e
finally:
self._close()
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, thiscursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!
– Parfait
Nov 13 '18 at 16:23
add a comment |
Simply use parameterization, the industry standard to separate SQL code from data values, and not string interpolation which looks similar since the placeholder %s
is used in both. With this approach, None
should resolve as NULL
. In fact, PygreSQL docs even warns users on the practice:
Warning
Remember to never insert parameters directly into your queries
using the % operator. Always pass the parameters separately.
Consider following adjustment using unquoted %s
placeholders (see docs) with values later binded in cursor.execute()
call:
def _update_traits_db(self, code, date_start, date_end, unit):
# PREPARED STATEMENT (NO DATA VALUES INTERPOLATED)
sql = """INSERT INTO traits (code, date_start, date_end, unit)
VALUES (%s, %s, %s, %s)
ON CONFLICT (code) DO UPDATE
SET date_start = excluded.date_start,
date_end = excluded.date_end,
unit = excluded.unit
"""
try:
self._connect()
# BIND PARAMETERS WITH TUPLE OF VALUES
self._cur.execute(sql, (code, date_start, date_end, unit))
self._con.commit()
except Exception as e:
raise e
finally:
self._close()
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, thiscursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!
– Parfait
Nov 13 '18 at 16:23
add a comment |
Simply use parameterization, the industry standard to separate SQL code from data values, and not string interpolation which looks similar since the placeholder %s
is used in both. With this approach, None
should resolve as NULL
. In fact, PygreSQL docs even warns users on the practice:
Warning
Remember to never insert parameters directly into your queries
using the % operator. Always pass the parameters separately.
Consider following adjustment using unquoted %s
placeholders (see docs) with values later binded in cursor.execute()
call:
def _update_traits_db(self, code, date_start, date_end, unit):
# PREPARED STATEMENT (NO DATA VALUES INTERPOLATED)
sql = """INSERT INTO traits (code, date_start, date_end, unit)
VALUES (%s, %s, %s, %s)
ON CONFLICT (code) DO UPDATE
SET date_start = excluded.date_start,
date_end = excluded.date_end,
unit = excluded.unit
"""
try:
self._connect()
# BIND PARAMETERS WITH TUPLE OF VALUES
self._cur.execute(sql, (code, date_start, date_end, unit))
self._con.commit()
except Exception as e:
raise e
finally:
self._close()
Simply use parameterization, the industry standard to separate SQL code from data values, and not string interpolation which looks similar since the placeholder %s
is used in both. With this approach, None
should resolve as NULL
. In fact, PygreSQL docs even warns users on the practice:
Warning
Remember to never insert parameters directly into your queries
using the % operator. Always pass the parameters separately.
Consider following adjustment using unquoted %s
placeholders (see docs) with values later binded in cursor.execute()
call:
def _update_traits_db(self, code, date_start, date_end, unit):
# PREPARED STATEMENT (NO DATA VALUES INTERPOLATED)
sql = """INSERT INTO traits (code, date_start, date_end, unit)
VALUES (%s, %s, %s, %s)
ON CONFLICT (code) DO UPDATE
SET date_start = excluded.date_start,
date_end = excluded.date_end,
unit = excluded.unit
"""
try:
self._connect()
# BIND PARAMETERS WITH TUPLE OF VALUES
self._cur.execute(sql, (code, date_start, date_end, unit))
self._con.commit()
except Exception as e:
raise e
finally:
self._close()
answered Nov 13 '18 at 15:59
ParfaitParfait
50.5k84269
50.5k84269
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, thiscursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!
– Parfait
Nov 13 '18 at 16:23
add a comment |
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, thiscursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!
– Parfait
Nov 13 '18 at 16:23
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
That did it! Thanks a lot to both of you! Seems i had a misunderstanding on how the parameterization in python really worked, this makes a lot more sense. Glad to have that cleared up, thanks!
– Kevin
Nov 13 '18 at 16:16
Great to hear. Yes, this
cursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!– Parfait
Nov 13 '18 at 16:23
Great to hear. Yes, this
cursor.execute(query, params)
approach is consistent across most Python DB-APIs (cxOracle, ibmdb, pymssql, pyodbc, pymysql, psycopg2, sqlite3) per Python's PEP 249 SQL standard. Also, parameterization is an industry-wide best practice not just in Python but any app layer language (Java, C#, PHP, Perl, etc.) running SQL. Happy coding!– Parfait
Nov 13 '18 at 16:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283046%2finserting-none-value-into-date-column-from-python-to-postgresql-database-using-p%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don't need the
'
s around the placeholders... just use%s
by itself... eg:values (%s, %s, %s, %s)
- the engine will already correctly escape strings, None will be converted to null etc...– Jon Clements♦
Nov 13 '18 at 14:19
Tried this now, but am getting an error on the date due to the date format i believe, the date input is this " 2003-01-29T23:00:00Z" so i get ERROR: syntax error at or near "T23"
– Kevin
Nov 13 '18 at 14:23
So your date is a string then? If you make it a
datetime
object instead - it'll work fine.– Jon Clements♦
Nov 13 '18 at 14:40
Have converted the date strings to datetime objects and that works and can be inserted into the database, I cant find a way to convert a "None" variable to a datetime though and for some reason its still not converted into NULL when i try inserting it to the database. Same thing goes for the "Unit" value.
– Kevin
Nov 13 '18 at 15:13
After removing the quotes around the values like @JonClements sugested i also get an error on the code column. "ERROR: column "testCode" does not exist" so it does not seem to escape the strings on its own.
– Kevin
Nov 13 '18 at 15:15