I'm trying to run a sqlite3 query against a small DB to update a table. While the script is more complicated, for testing I set the following variables:
DN=123
UP=123
downlocalip=10.1.2.3
downremoteip=123
uplocalip=123
upremoteip=123
I then run the following command to update the table.
sqlite3 /var/www/server/newserverstats.db "UPDATE stats SET downspeed='''$DN''', upspeed='''$UP''', downlocalip='''$downlocalip''', downremoteip='''$downremoteip''', uplocalip='''$uplocalip''', upremoteip='''$upremoteip''' WHERE primkey=1"
This throws a syntax error:
Error: near ".2": syntax error
If I set the downlocalip as 10.1 only, it works fine, so it is not liking the additional decimals.
On the table itself I have the type set as text so I didn't think it would matter?
Pragma output of table:
0|primkey|integer|0||1
1|downspeed|integer|1||0
2|upspeed|integer|1||0
3|downlocalip|text|1||0
4|downremoteip|text|1||0
5|uplocalip|text|1||0
6|upremoteip|text|1||0
I have tried all sorts of quotation mark setups but can't see what I'm doing wrong.
Any ideas?
EDIT:
The full commands I have tried as per comments below are:
/usr/bin/ssh [email protected] 'sqlite3 /var/www/server/newserverstats.db "UPDATE stats SET downspeed=$DN, upspeed=$UP, downlocalip="$downlocalip", downremoteip="$downremoteip", uplocalip="$uplocalip", upremoteip="$upremoteip" WHERE primkey=1"'
or
/usr/bin/ssh [email protected] 'sqlite3 /var/www/server/newserverstats.db "UPDATE stats SET downspeed=$DN, upspeed=$UP, downlocalip='$downlocalip', downremoteip='$downremoteip', uplocalip='$uplocalip', upremoteip='$upremoteip' WHERE primkey=1;"'
both give me the following error:
Error: near ",": syntax error
New command, which is closer:
ssh [email protected] sqlite3 /var/www/server/newserverstats.db <<END_SQL
UPDATE stats
SET downspeed=$DN,
upspeed=$UP,
downlocalip="$downlocalip",
downremoteip="$downremoteip",
uplocalip="$uplocalip",
upremoteip="$upremoteip"
WHERE primkey=1
END_SQL
Collapsing back to a single line in a test script:
#!/bin/bash -x
DN=123
UP=123
downlocalip=10.1.2.3
downremoteip=123
uplocalip=123
upremoteip=123
sql="UPDATE stats SET downspeed=$DN, upspeed=$UP, downlocalip="$downlocalip", downremoteip="$downremoteip", uplocalip="$uplocalip", upremoteip="$upremoteip" WHERE primkey=1"
echo $sql
ssh [email protected] sqlite3 /var/www/server/newserverstats.db "$sql"
This gives me the response of:
UPDATE stats SET downspeed=123, upspeed=123, downlocalip=10.1.2.3, downremoteip=123, uplocalip=123, upremoteip=123 WHERE primkey=1
sqlite3: Error: too many options: "stats"
Use -help for a list of options.
sqlite3 ...command. they are conflicting with the single-quotes inside the double-quotes. And there's nothing about the command that needs single quotes -sshpasses all its remaining arguments (after user@host and option processing, if any) to the remote shell anyway.'\''(end-quote, escaped-quote, start-quote) but that would be extremely ugly and difficult to read.