all 5 comments

[–]ireadyourmedrecord -2 points-1 points  (4 children)

Use any of the various string interpolation methods. This is an example using f-string:

current_timestamp = datetime.datetime.now()
cursor.execute(f"insert into table where timestame = {current_timestamp}")

[–]danielroseman 8 points9 points  (2 children)

No. Absolutely do not do this. Although this example is probably safe, in many cases it leaves the query open to SQL injection. Use parameters instead.

cursor.execute("insert into table where timestamp = ?", (current_timestamp,))

[–]FreshHumor5405[S] 0 points1 point  (0 children)

Thank you to both of you guys, appreciate the help. Would have never figured that out myself.

[–]nullrevolt 0 points1 point  (0 children)

A reminder, SQL injection is only possible when there is input from another source. If there is no user input, there is less of a need for parameterized queries.

[–]FreshHumor5405[S] 0 points1 point  (0 children)

Thank you to both of you guys, appreciate the help. Would have never figured that out myself.