Skip to content

PietroFara/sqlite_lib.bash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

sqlite_lib.bash

Bash library to use SQLite in bash scripts

Dependencies:

  • sqlite3

Tested with sqlite 3.13.0 in ArchLinux

Usage:

Import the library into your script

source sqlite_lib.bash

Call sqlite_create_fetch_array with two parameters: the first is the sqlite db file, the second is the sqlite query (must be in quotes).

sqlite_create_fetch_array db.sqlite "SELECT id, Name, Surname FROM Customers"

The function sets two variables:

  1. num_rows contains the number of rows of the query result
  2. rows (bidimensional array) contains the data. The first index is the row result index, the second is the Column name of the result data
for (( i = 0; i < num_rows; i++ )); do
  echo ${rows[$i,"id"]} ${rows[$i,"Name"]} ${rows[$i,"Surname"]} 
done

Full example:

#!/bin/bash
source sqlite_lib.bash
sqlite_create_fetch_array db.sqlite "SELECT id, Name, Surname FROM Customers"
for (( i = 0; i < num_rows; i++ )); do
  echo ${rows[$i,"id"]} ${rows[$i,"Name"]} ${rows[$i,"Surname"]} 
done

About

Bash library to use SQLite in bash scripts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages