title: How to create and update a SQL database with real-time data received from a python socket?
body: <h2>Data Source</h2>
  <p>I am receiving data from a driving simulator in real-time. The following is a socket program that receives the data from the simulator and then prints it:</p>
  <pre><code>import socket
  import struct
  
  UDP_IP = &quot;127.0.0.1&quot;
  UDP_PORT = 4001
  
  sock = socket.socket(socket.AF_INET, # Internet
                       socket.SOCK_DGRAM) # UDP
  sock.bind((UDP_IP, UDP_PORT))
  
  while True:
      data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
      fields = struct.unpack_from('=ddd', data)
      print(fields[0],fields[1],fields[2])
  </code></pre>
  <h3>Output</h3>
  <p>The printed data appear as follows in a shell:</p>
  <p><a href="https://i.stack.imgur.com/yr1OZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yr1OZ.jpg" alt="enter image description here" /></a></p>
  <h2>What I want to do</h2>
  <p>I want to store these data in a database. Later, I want to use the database in an application. My question is: how do I create a database that can store and save these real-time data? I have no prior knowledge of creating databases. Please guide me about the relevant resources.</p>
  <h2>What I have tried:</h2>
  <p>I first created a database in the same location where I have the socket script (described above):</p>
  <h3>Database creation:</h3>
  <pre><code> import sqlite3
  
  conn = sqlite3.connect('test_database')
  c = conn.cursor()
  
  c.execute('''
            CREATE TABLE IF NOT EXISTS position
            ([y] REAL PRIMARY KEY, [x] REAL, [z] REAL)
            ''')
           
           
  conn.commit()
  </code></pre>
  <h3>Attempt to update the database with the real-time data:</h3>
  <p>Then I modified my script file to store the data in a database as follows:</p>
  <pre><code>import socket
  import struct
  import sqlite3
  
  conn = sqlite3.connect('test_database')
  c = conn.cursor()
  
  UDP_IP = &quot;127.0.0.1&quot;
  UDP_PORT = 4001
  
  sock = socket.socket(socket.AF_INET, # Internet
                       socket.SOCK_DGRAM) # UDP
  sock.bind((UDP_IP, UDP_PORT))
  
  while True:
      data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
      fields = struct.unpack_from('=ddd', data)
      #print(&quot;received message:&quot;, data)
      print(fields[0],fields[1],fields[2])
      #sock.sendto(fields.encode(), (&quot;127.0.0.1&quot;,7070))
       
      c.execute('''
                INSERT INTO position (y, x, z)
  
                      VALUES
                      (fields[0],fields[1], fields[2])
                ''')
      conn.commit()
  </code></pre>
  <h3>Error:</h3>
  <p>But I get the following error:</p>
  <pre><code>2315.259850934807 35025.972653539255 -1.7375892216255708
  Traceback (most recent call last):
    File &quot;C:\NadsMiniSim_2.3\bin.x64\testIP-UDP3.py&quot;, line 22, in &lt;module&gt;
      c.execute('''
  sqlite3.OperationalError: near &quot;[0]&quot;: syntax error
  </code></pre>
  <p>I am not sure what went wrong. Please guide me.</p>