title: Read data from a serial device using Python and save it to PostgreSQL using a Raspberry Pi
body: <p>Im trying read data from a smartmeter that has FTDI chip. Wrote a simple Python serial program to pass the commands to meter and the meter replies back. the data from meter is then converted to float and stored in dictionary.</p>
  
  <p>now i want to store the dictionary to DB, <strong>here is the code to put data in the table.</strong></p>
  
  <pre><code>import psycopg2
  
  conn = psycopg2.connect(database="smartmeter", user="postgres", password="12345", host="localhost", port="5432")
  
  cur = conn.cursor()
  
  cur.execute('''CREATE TABLE metertable (ID      SERIAL  PRIMARY KEY     NOT NULL,meter  TEXT    NOT NULL,temperature    TEXT    NOT NULL,freq   TEXT    NOT NULL,penergy        TEXT    NOT NULL,qenergy    TEXT    NOT NULL,senergy        TEXT    NOT NULL,cospi  TEXT    NOT NULL,irms   TEXT    NOT NULL,ppower TEXT    NOT NULL,qpower TEXT    NOT NULL,spower TEXT    NOT NULL);''')
  while 1:
      data=dict[]
      data={
                  'time':timestamp,
                  'meter':m0_data,
                  'temperature':m1_data,
                  'freq':m2_data,
                  'penergy':m3_data,
                  'qenergy':m6_data,
                  'senergy':m7_data,
                  'cospi':m11_data,
                  'irms':m15_data,
                  'vrms':m16_data,
                  'ppower':realpower,
                  'qpower':reactivepower,
                  'spower':apparentpower
          }
          cur.executemany ("""INSERT INTO metertable(time,meter,temperature,freq,penergy,qenergy,senergy,cospi,irms,vrms,ppower,qpower,spower) VALUES (%(time)s, %(meter)s), %(temperature)s), %(freq)s), %(penergy)s), %(qenergy)s), %(senergy)s), %(cospi)s), %(irms)s), %(vrms)s), %(ppower)s) %(qpower)s) %(spower)s)""", data)
  </code></pre>
  
  <p><strong>I get an error like this</strong></p>
  
  <pre><code>Traceback (most recent call last):
    File "metercmd.py", line 97, in &lt;module&gt;
      cur.executemany("""INSERT INTO metertable(time,meter,temperature,freq,penergy,qenergy,senergy,cospi,irms,vrms,ppower,qpower,spower) VALUES (%(time)s, %(meter)s), %(temperature)s), %(freq)s), %(penergy)s), %(qenergy)s), %(senergy)s), %(cospi)s), %(irms)s), %(vrms)s), %(ppower)s) %(qpower)s) %(spower)s)""", data)
  TypeError: string indices must be integers, not str
  </code></pre>
  
  <p>Am I going on the right direction to enter data to the DB? Please suggest some best methods. </p>