Login

grep and delete sqlite tables

Author:
kifkif
Posted:
December 7, 2009
Language:
SQL
Version:
Not specified
Tags:
db sqlite drop
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. oneliner to delete SQLite tables by kifkif 5 years, 7 months ago
  2. SQLite database vacuum script by pbx 8 years ago
  3. fast table flush without raw SQL by dsblank 5 years, 5 months ago
  4. Prefetch id for Postgresql backend by bretth 3 years, 1 month ago
  5. duplicate, please delete by RommeDeSerieux 5 years, 8 months ago

Comments

Please login first before commenting.