0

I can't seem to find out what I'm doing wrong. It would be great to receive some feedback!

    import cgi, cgitb, MySQLdb
    cgitb.enable()
    db = MySQLdb.connect(db="watchdog",user="root",passwd="XXXXXXX")
    c = db.cursor()
    print ("Content-type:text/html\n")
    print ("<html><head><title>WatchdogServer</title></head><body>")
    data = cgi.FieldStorage()
    hostname = data.getvalue("hostname")
    osName = data.getvalue("osName")
    ramUsage = data.getvalue("ramUsage")
    cpuUsage = data.getvalue("cpuUsage")
    diskUsage = data.getvalue("diskUsage")
    c.execute("""CREATE TABLE IF NOT EXISTS `{}` (`ID` int(11) NOT NULL,`hostname` text NOT NULL,`osName` text NOT NULL,`ramUsage` text NOT NULL,`cpuUsage` text NOT NULL,`diskUsage` text NOT NULL,`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=latin1; """.format(hostname))
    c.execute("""INSERT INTO `{}` (`hostname`, `osName`, `ramUsage`, `cpuUsage`, `diskUsage`) VALUES (`{}`,`{}`,`{}`,`{}`,`{}`);""".format(hostname,hostname,osName,ramUsage,cpuUsage,diskUsage))
    print ("</body></html>")

For some reason it is looking for a column with the hostname variable. But even if I change all the column names and try again I still receive the same error everytime. :(

_mysql_exceptions.OperationalError: (1054, "Unknown column 'BOB-PC' in 'field list'")

Hope anyone of you can assist me!

Cheers! Bob

monk8855
  • 1
  • 1
  • Possible duplicate of [Unknown column in 'field list' error on MySQL Update query](https://stackoverflow.com/questions/1346209/unknown-column-in-field-list-error-on-mysql-update-query) – roganjosh Dec 29 '17 at 17:16
  • In other words, don't enclose your `VALUES` in backticks. Also, you should not be using string formatting to build statements, you should look into prepared statements to avoid SQL injection and potentially speed up the inserts. – roganjosh Dec 29 '17 at 17:17
  • Thanks for your response! I'll try it right now and get back to you asap! EDIT: It worked! I can't thank you enough. I've been sitting with this for the last 2 hours. Wish I could buy you a beer or something haha! – monk8855 Dec 29 '17 at 17:22
  • You are welcome. Note that I've used MySQL once so the answer was not obvious to me when looking at your code, instead I just googled "_mysql_exceptions.OperationalError: (1054, "Unknown column in 'field list'")" (removing the column name) and got the solution straight away. Always Google the error message. I believe that you're able to accept my duplicate suggestion if you feel that it provided enough explanation to solve this. That then tells others that your question is no longer active and helps point other Googlers to the answer. – roganjosh Dec 29 '17 at 17:30

0 Answers0