title: serialize and deserialize SQL query
body: <p>I'm having an embedded device which keeps a list of inner tables. I would like it to synchronize this table's state with some outside database for debugging purposes. That is whenever I add an element to a certain struct array I wish the device to issue an "INSERT INTO ..." command.</p>
  
  <p>However I'm sending the data over an RS232 serial cable, which makes the overhead of sending explicit SQL unacceptable.</p>
  
  <p>Since there are only 3 types of SQL commands I use a lot, I can only serialize these few. Namely <code>INSERT INTO</code>,<code>DELETE FROM</code>, and <code>UPDATE</code>.</p>
  
  <p>The general idea I had in mind is to send data with a "compressed/serializable" SQL protocol. We won't send commands directly to SQL server but to a custom serialized-SQL server I'll write:</p>
  
  <ol>
  <li>We'll assign a number to each database-changing simple action (ie INSERT, DELETE, UPDATE). The only available serializable-SQL commands are <code>INSERT INTO x ()</code>, <code>DELETE FROM x WHERE id=y</code>. Where we can only change <code>x</code> and <code>y</code>.</li>
  <li>At first create all necessary tables on the server once. Keep a hash table on the server that maps each table to a number. This can be done in plain SQL as this is only done once.</li>
  <li>Then assign a number to each table, make sure the server knows about this number</li>
  <li>Finally whenever we wish to execute an SQL command, we'll send command number, followed by table number, followed by data length followed by data. The server would figure out the layout of the actual data by table's description.</li>
  </ol>
  
  <p>For example</p>
  
  <pre><code>INSERT INTO temperature(temperature,location)
       VALUES ((108,"chille"),(120,"usa"))
  </code></pre>
  
  <p>Would be translated to</p>
  
  <pre><code>[INSERT INTO id][2 data to send]
      [byte of 108][6 bytes string "chille"]
      [byte of 120][3 bytes "usa"]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>DELETE FROM people (id,"bob") WHERE id=1 or id=2
  </code></pre>
  
  <p>Would be translated to</p>
  
  <pre><code>[DELETE id][2 data to send][byte of 1][byte 2]
  </code></pre>
  
  <p>Since id is defined as a single byte integer.</p>
  
  <p>Is there any known protocol/implementation in this spirit?</p>
  
  <p>Does anyone have a better idea?</p>