Lauri's notes

.
#!/bin/sh

# Defining my databse first table
STRUCTURE="CREATE TABLE movies (name TEXT);"

touch movies.db
echo "CREATE TABLE movies (name TEXT);" | sqlite3 movies.db
sqlite3 movies.db "INSERT INTO movies (name) VALUES ('test')"
sqlite3 movies.db "INSERT INTO movies (name) VALUES ('test2')"
sqlite3 movies.db "INSERT INTO movies (name) VALUES ('test3')"

sqlite3 movies.db "SELECT name FROM movies" | while read NAME; do echo "Muuvi $NAME is cool"; done

sqlite3 status.db "BEGIN TRANSACTION"
sqlite3 status.db "COMMIT"


# Getting my data
LIST=$(sqlite3 dbname.db "SELECT * FROM data WHERE 1");

# For each row
for ROW in $LIST; do
    ID=${ROW%%|*} && ROW=${ROW#*|}
    NAME=${ROW%%|*} && ROW=${ROW#*|}
    VALUE=${ROW%%|*} && ROW=${ROW#*|}
    # Printing my data
    echo -e "\e[4m$ID\e[m) "$NAME" -> "$VALUE;
done

while read ID; do
    echo "id = $ID"
done < <( $DB "SELECT id FROM table;" )

Tags: sqlite, shell

Have an update or suggestion for this note? You can edit it and send me a pull request.

Since 2009 to 2015