0

bid, best_bid_quantity are floats, target_date is set as timestamp in my db.

get timestamp

target_date_time_ms = (k.lastRestRequestTimestamp) 

base_datetime = datetime.datetime( 1970, 1, 1 )

delta = datetime.timedelta( 0, 0, 0, target_date_time_ms )

target_date = base_datetime + delta

timestamp = target_date

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                         user="root",         # your username
                         passwd="lolilol",  # your password
                         db="test")        # name of the data base

cur = db.cursor()

try:

   cur.execute("""INSERT INTO test1 (heure, prix, quantite) VALUES ({},{},{})""".format(target_date, bid, best_bid_quantity))
   db.commit()

except:
    print('did not insert')
    db.rollback()

db.close()

error --> it goes to except when I add the target_date

4
  • Try getting your exception to print out more info, like: except Exception as e: print(e). Then tell us what the exact error is. Commented Jul 4, 2018 at 12:56
  • (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13:25:34.218000,0.071,24.434)' at line 1") Commented Jul 4, 2018 at 13:26
  • What is the value of target_date_time_ms? Commented Jul 4, 2018 at 13:32
  • the value is 1530711319886 in ms Commented Jul 4, 2018 at 13:35

1 Answer 1

1

Try using time.strftime():

time.strftime('%Y-%m-%d %H:%M:%S', target_date.timetuple())

The .timetuple() method is required to convert the datetime.datetime object of target_date into a time_struct struct, which can then be used in the call to time.strftime.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.