Login

grep and delete sqlite tables

Author:
kifkif
Posted:
December 7, 2009
Language:
SQL
Version:
Not specified
Score:
0 (after 0 ratings)

This is a slight improvment of a previous snippet of mine: http://www.djangosnippets.org/snippets/1776/

It is an interactive shell script that greps and deletes sqlite tables

USAGE: ./pdrop.sh myquery mydbfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /bin/sh

QUERY=$1
TABLE=$2

echo -e "Here is your statement:\n"

CMD="BEGIN;\n"
CMD=$CMD`echo '.tables' | sqlite3 $TABLE | tr ' ' '\n' | grep $QUERY | sed 's/^/DROP TABLE /g' | sed 's/$/;/g'`
CMD=$CMD"\nCOMMIT;"

echo -e "$CMD\n"

echo -n "Do you want to commit this statement? (Y/N) "
read -e ANSWER

if test "$ANSWER" = "Y"; then
    echo -e "$CMD" | sqlite3 $TABLE
    echo "Done!"
else
    echo "Nothing changed, have a good day."
fi

exit

More like this

  1. create_template_postgis-ubuntu_lucid by clawlor 13 years, 10 months ago
  2. PostgreSQL fulltext with language translations by diverman 13 years, 11 months ago
  3. Drop all tables in MySQL database by mpasternacki 14 years, 5 months ago
  4. Substitute hyphens with spaces to enale URLs to reference to multi-word tags by miernik 14 years, 8 months ago
  5. oneliner to delete SQLite tables by kifkif 14 years, 9 months ago

Comments

Please login first before commenting.