Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Michael T. Richter

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Making SQLITE/SQLITE3 executable scripts.

Use "here document" statements to build complex script files with embedded SQL statements via the sqlite/sqlite3 utility.

   1  
   2  #! /usr/bin/env bash
   3  
   4  # execute some bash scripting commands here
   5  
   6  sqlite3 mydatabase <<SQL_ENTRY_TAG_1
   7  SELECT * 
   8    FROM mytable 
   9    WHERE somecondition='somevalue';
  10  SQL_ENTRY_TAG_1
  11  
  12  # execute other bash scripting commands here
  13  
  14  sqlite3 mydatabase <<SQL_ENTRY_TAG_2
  15  SELECT *
  16    FROM myothertable
  17    WHERE someothercondition='someothervalue';
  18  SQL_ENTRY_TAG_2


Note that being in a bash script means that you can expand $-variables inside the SQL code directly. This is, however, not advised unless you can be sure that only trusted, competent people will run your code. Otherwise you'll be facing SQL injection attacks.

Making MySQL executable scripts.

Use "here document" statements to build complex script files with embedded SQL statements via the mysql utility.

   1  
   2  #! /usr/bin/env bash
   3  
   4  # execute some bash scripting commands here
   5  
   6  mysql mydatabase <<SQL_ENTRY_TAG_1
   7  SELECT * 
   8    FROM mytable 
   9    WHERE somecondition='somevalue';
  10  SQL_ENTRY_TAG_1
  11  
  12  # execute other bash scripting commands here
  13  
  14  mysql mydatabase <<SQL_ENTRY_TAG_2
  15  SELECT *
  16    FROM myothertable
  17    WHERE someothercondition='someothervalue';
  18  SQL_ENTRY_TAG_2


Note that being in a bash script means that you can expand $-variables inside the SQL code directly. This is, however, not advised unless you can be sure that only trusted, competent people will run your code. Otherwise you'll be facing SQL injection attacks.

Get HTML/XML output of your MySQL queries.

The mysql command line utility can be used to build databases, manipulate them, etc. It can also be used as an ad-hoc query tool with HTML-snippet output. Consider this code:

   1  
   2  mysql -e "SELECT * FROM mytable WHERE somecondition='somevalue'"


The resulting output will be a mess of +, - and | characters used to frame boxes around the values. Now consider instead:

   1  
   2  mysql -H -e "SELECT * FROM mytable WHERE somecondition='somevalue'"


The output of this is <TABLE/><TR/><TD/> code that can be cut-and-pasted into your HTML editor of choice. Complex queries not easily put into a single -e string can be done thus:

   1  
   2  mysql -H < myqueries.sql


Note that no HTML is generated for any query that does not have a result set (like INSERT or UPDATE).

Change the -H to a -X to get XML output (without a DTD/XSL description, unfortunately).
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS