Home > Back-end >  Trying to insert python datetime into SQL DATE
Trying to insert python datetime into SQL DATE

Time:02-11

Trying to insert python datetime into SQL DATE but an error keeps appearing saying OperationalError: near "02": syntax error

import  sqlite3
conn=sqlite3.connect("Date_time3.db")
print("Database Opened successfully")
conn.execute("""
CREATE TABLE ADMIN(
EXPIRE DATETIME PRIMARY KEY
)
""")

This is my database^

import datetime 
import pytz
import  sqlite3

today = datetime.datetime.now()
date_time = today.strftime("%m/%d/%Y, %H:%M:%S")

conn=sqlite3.connect("Date_time3.db")
print("Database Opened successfully")

conn.execute("INSERT INTO ADMIN(EXPIRE) VALUES "  date_time);



conn.commit()
print ("Records inserted successfully")
conn.close()
"""
###Output###
#Database Opened successfully
#Records inserted successfully
#"""
print ("Table ADMIN created successfully")
"""
####Output###
Database Opened successfully
Table ADMIN created successfully
"""

CodePudding user response:

Change this:

conn.execute("INSERT INTO ADMIN(EXPIRE) VALUES "  date_time);

to

conn.execute('INSERT INTO ADMIN(EXPIRE) VALUES (?)', [date_time]);

As you are running SQL commands, they work differently from Python. So you cannot use operators. You need to use placeholders like (?)

  • Related