Проект

Общее

Профиль

Install

Use git to get repository:

git clone https://bitbucket.org/21h/sqlite3socket.git

Get updates:

git pull

Usage

All requests sending only in plain text SQL format. JSON answer in a plain array. First item may be "result" or "error", second item is another array with query result or error text.

bash

Using unix socket file instead binding to interface (works faster):

echo "select value from options where option='transmitter' limit 1;"|nc -U /tmp/sqlite3.sock

Success with 1 row:

vlad@hostname:/opt/sqlite3socket$ echo "select value from options where option='transmitter';"|nc 127.0.0.1 5566
["result", [["enabled"]]]

Success with 0 rows:

vlad@hostname:/opt/sqlite3socket$ echo "select value from options where option='transmitter_blabla';"|nc 127.0.0.1 5566
["result", []]

Success query with no results:

vlad@hostname:/opt/sqlite3socket$ echo "insert into options values (112,'gg','ddfsdf',null);"|nc 127.0.0.1 5566
["result", []]

Oops! Error!

vlad@hostname:/opt/sqlite3socket$ echo "select value from options where option_blabla;"|nc 127.0.0.1 5566
["error", "no such column: option_blabla"]

Parse JSON in bash

echo "select value from options where option='transmitter' limit 1;"|nc 127.0.0.1 5566|python -c 'import json,sys;obj=json.load(sys.stdin);print obj[1][0][0]'
enabled

PHP

Client code:

<?php

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

$result = socket_connect($socket, '127.0.0.1', 5566);

if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
}

$sql = 'select value from options where option="transmitter";';
$result_json = '';

socket_write($socket, $sql, strlen($sql));
while ($read = socket_read($socket, 2048)) {
    $result_json .= $read;
}
socket_close($socket);
var_dump(json_decode($result_json));

Result:

array(2) {
  [0]=>
  string(6) "result" 
  [1]=>
  array(1) {
    [0]=>
    array(1) {
      [0]=>
      string(7) "enabled" 
    }
  }
}