Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python insert datetime into Mysql DB

  • 17-08-2011 4:01pm
    #1
    Registered Users, Registered Users 2 Posts: 3,745 ✭✭✭


    Hi I'm having some difficulty inserting a row in a table using python MySQLdb:

    I've verified that my connection code and credentials work by printing the mysql version.

    I am passing some arguments to this simple script to have them inserted in a DB.
    #!/usr/bin/python -tt
    
    import MySQLdb as mdb
    import sys
    
    value_id = sys.argv[1]
    value = sys.argv[2]
    date = sys.argv[3]
    time = sys.argv[4] 
    dt = "'" + date + " " + time + "'"
    
    
    try:
      conn = mdb.connect('localhost', 'test', 'test', 'test');
    
      cursor = conn.cursor()
      print date + " " + time
      cursor.execute("INSERT INTO test (ID, VALUE_ID, VALUE, DATE) VALUES (NULL, value_id, value, dt) ")
      cursor.close()
      conn.close()
    
    except mdb.Error, e:
      print "Error %d: %s" % (e.args[0],e.args[1])
      sys.exit(1)
    
    

    I get the following when I run this:

    $ ./insert_stat.py 101 2169.41 2011-08-17 15:16:00
    Error 1054: Unknown column 'dt' in 'field list'

    I've tried a multitude of different quotes around this and it still will not work.

    At my mysql prompt the following works (ID the first column is an auto incrementing INT) :
    INSERT INTO test VALUES (NULL, 101, '1786.85', '2011-08-17 12:10:00');
    

    Any ideas?


Comments

  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Code is probably fine - it's telling you that it can't find a column called 'dt' in the table. Check the table and see if the date column is actually called dt.


  • Registered Users, Registered Users 2 Posts: 3,745 ✭✭✭laugh


    dlofnep wrote: »
    Code is probably fine - it's telling you that it can't find a column called 'dt' in the table. Check the table and see if the date column is actually called dt.

    Hey thank you for the reply, the column is called DATE as specified by the INSERT statement (ID, VALUE_ID, VALUE, DATE), dt is a variable containing a datetime that I want to insert. When I do a print of dt just before the insert it prints out exactly the format of date I used at the mysql prompt.
    +---------+----------+------+-----+---------+----------------+
    | Field   | Type     | Null | Key | Default | Extra          |
    +---------+----------+------+-----+---------+----------------+
    | ID      | int(11)  | NO   | PRI | NULL    | auto_increment | 
    | VALUE_ID | int(11)  | NO   |     |         |                | 
    | VALUE   | float    | NO   |     |         |                | 
    | DATE    | datetime | NO   |     |         |                | 
    +---------+----------+------+-----+---------+----------------+
    


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    You are passing in a string "dt" rather than the actual value of dt.

    Never used the MySQLdb, but it appears from the documentation that execute has an args argument. Or you could use Python's format strings/concatenation.

    
    cursor.execute("INSERT INTO test (ID, VALUE_ID, VALUE, DATE) VALUES (NULL, value_id, value, %s)", dt) # Might require a tuple, which would be (dt, )
    
    #Or on the string itself
    "INSERT INTO test (ID, VALUE_ID, VALUE, DATE) VALUES (NULL, value_id, value, '%s')"%dt
    


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Oops sorry :) I read it wrong.


Advertisement