1

I am trying to insert Arduino data into a database through Python, however, it will not do it. Basically I am assigning data that I read in from the serial port assigned to my Arduino and storing the first value of it in the variable arduinoData. in my insert statement I am trying to use a string literal to put the arduinoData into the table. Here is the code:

import mysql.connector
from mysql.connector import errorcode
from time import sleep
import serial

# Obtain connection string information from the portal
config = {
  'host':'oursystem.mysql.database.azure.com',
  'user':'project',
  'password':'',
  'database':'projectdb'
}

# Construct connection string
try:
   conn = mysql.connector.connect(**config)
   print("Connection established")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with the user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cursor = conn.cursor()
  ser = serial.Serial('/dev/ttyACM0', 9600) # Establish the connection on a specific port
  arduinoData=ser.read().strip()
  print arduinoData
  # Drop previous table of same name if one exists
  cursor.execute("DROP TABLE IF EXISTS ArduinoData;")
  print("Finished dropping table (if existed).")

  # Create table
  cursor.execute("CREATE TABLE ArduinoData (value VARCHAR(20));")
  print("Finished creating table.")

  # Insert some data into table
  cursor.execute("INSERT INTO ArduinoData (value) VALUES (%s);",(arduinoData))
  print("Inserted",cursor.rowcount,"row(s) of data.")


  # Cleanup
  conn.commit()
  cursor.close()
  conn.close()
  print("Done.")

If I put the %s in single quotes like '%s' it just prints that instead of my arduinoData. Can anyone see what is wrong here, thanks.

4 Answers 4

2

I just lost two hours on this : If you're trying to observe what's happening to your database with phpmyadmin, please note that all your insert commands won't be visible until you commit them

connection.commit()
Sign up to request clarification or add additional context in comments.

Comments

0

Simply pass a tuple (arduinoData,) which means a comma within parentheses for a single value or list [arduinoData] and not a single value (arduinoData) in your parameterization:

cursor.execute("INSERT INTO ArduinoData (`value`) VALUES (%s);",(arduinoData,))

However if arduinoData is a list of multiple values, then use executemany, still passing a list. Also, escape value which is a MySQL reserved word:

cursor.executemany("INSERT INTO ArduinoData (`value`) VALUES (%s);",[arduinoData])

4 Comments

thank you for your answers, i used the tuple parsing and it identifies that a number is being retrieved from my arduino but will not store it. im pretty sure it is taking the number as a string. mysql.connector.errors.ProgrammingError: 1064 (42000): 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 ''value') VALUES ('7')' at line 1 that is the error it gives me, i changed the literal to %d but that still doesen't change anything.
Please show what is the length and content of arduino. I hope you are using backticks and not single quotes (as I am seeing in your error output) to escape value.
the length of the arduino is actually a constant stream of numbers from an ultrasonic sensor, I am only taking the first number that the sensor gives and inputting that to the database (for testing). im not quite sure what you mean to 'escape' value?
In databases like MySQL and languages like Python there are certain keywords that if used will cause idioyncractic behaviors such as your use of value. Ideally use a different name. But if you do not, you should escape the word by enclosing the identifier with backticks to tell the engine that my value is not your system's value. Your error looks like you used single quotes not backticks.
0

I may have interpreted this wrong but, shouldn't there be something like certain_value = '%s' otherwise it doesn't know what it is looking for.

7 Comments

I am unsure as to that, I was working on the insert statement on this link -> learn.microsoft.com/en-us/azure/mysql/connect-python as i am aware, the parameters that you specify after the %s like my arduinoData are used in place of the %s. that was what i thought anyway, any other link i look at uses that anyway
found your mistake, (hopefully) there is a ';' missing after the (%s)"
ah thanks, i didn't notice that unfortunately, it didn't fix it, it is still telling me there is an error after the %s somewhere :/
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 '%s)' at line 1 that is the error
dev.mysql.com/doc/connector-python/en/… have a look here this might help you
|
0

I have just figured out what was wrong, using Parfait's suggestion of parsing a tuple, i just changed my insert statement from cursor.execute("INSERT INTO ArduinoData (value) VALUES (%s);",(arduinoData))to cursor.execute("INSERT INTO ArduinoData (value) VALUES (%s);",(arduinoData,))thanks to everyone who answered you were all a great help! :D

1 Comment

Not quite sure what your answer is here as you originally had (value). Please clarify clearly for future readers. But glad it all worked out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.