|
|
|
||||||||||||||
|
Unix FAQ Menu |
Question;Hi Andrew, Answer;This question is mostly about SQL rather than shell programming.... You need to identify which program will perform SQL queries for you. This will vary according to which database you are using. Below is a simple C-shell script which illustrates how to do this; #!/bin/csh -f # set outfile = "/tmp/sql.out" set team = "Germany" # /usr/local/bin/psql worldcup << ENDSQL >! $outfile select teamname,goals1,goals2 from teams,matches where teamname='$team'; ENDSQLThis example extracts a list of matches played by Germany in last years World Cup and puts them in a file named /tmp/sql.out, overwriting anything that was already in the file (">!"). The database in use is called "worldcup", and 'psql' is the SQL interpreter for the Postgres RDBMS. The keyword ENDSQL is my arbitary choice and simply marks the text that should be passed to 'psql' as stdin. The SQL itself could go in a different file in which case the initial command would be; psql worldcup < sqlcommandfile >! $outfile 'worldcup' is, in this instance the database name, which you will need to replace with your own database name. You may also need to expand this to a full 'connect string' ( SQL jargon ) which includes the name of system where the database resides, and possibly a username/password. Both these questions you may need to ask of your database administrator. If you need to massage the output format, then I would suggest using 'awk' on the $outfile, this being a common approach.. Style notes: C-shell has never been well thought of as a scripting language by a number
of people. Whilst it certainly has some drawbacks relating to quoting
and security, it is still useful for 'quick-and-dirty' solutions.. References: http://www.sqlcourse.com/ And dbforums offers discussion groups covering all the major databases. FeedbackI hope you found this FAQ to be of some use. It would be most helpful if you
could rate it below. All fields are optional...
|
||||||||||||||
|
|||||||||||||||