Rodrigo
Rodrigo

Reputation: 12693

Bash: How insert text with special character in sqlite3?

Let's imagine I want to save the text of various bash script in my database with sqlite3.

If my script do this

VARIABLE=$(cat "bashScript.sh")
sqlite3 mydb.db "CREATE TABLE data (myBash BLOB)" 
sqlite3 mydb.db "INSERT INTO data VALUES (${VARIABLE})"

Because the bash script will have all the special character, this will not work. How can I do this?

Upvotes: 1

Views: 1406

Answers (1)

glenn jackman
glenn jackman

Reputation: 247042

You need to quote the value in the Insert statement, and you'll want to protect quotes in the value itself: untested:

sql_value=$(sed 's/'\''/&&/g' <<< "$VARIABLE")
sqlite3 mydb.db "insert into data values ('$sql_value')"

Upvotes: 2

Related Questions