ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
But is it not possible to achieve this using a shell script..?
Cos my application has to be used by a Tester and he wants every time to enter the value in database...
Well, there is no shell API for inserting directly into the database, so you need to use some tool or other. sqlplus is such a tool which works with oracle. I don't know about HSQL - I would assume there is some analogue to sqlplus.
When I say "create an .sql file", you can do that in a script. You didn't say what data needs to be entered, so here's a pretty abstract example:
Code:
#!/bin/bash
read -p "enter value for field1> " field1
read -p "enter value for field2> " field2
read -p "enter value for field3> " field3
cat <<EOD > temp.sql
INSERT INTO mytable
VALUES ('$field1', '$field2', '$field3');
commit
quit
EOD
sqlplus -s user/pass@database @ temp.sql
rm -f temp.sql
There are many scripting engines for Java, you can use one, open a connection to a database and do what you have to do.
Google for JDBC and Groovy, Jython, BSH, Rhino, JRuby, etc.
Well, there is no shell API for inserting directly into the database, so you need to use some tool or other. sqlplus is such a tool which works with oracle. I don't know about HSQL - I would assume there is some analogue to sqlplus.
When I say "create an .sql file", you can do that in a script. You didn't say what data needs to be entered, so here's a pretty abstract example:
Code:
#!/bin/bash
read -p "enter value for field1> " field1
read -p "enter value for field2> " field2
read -p "enter value for field3> " field3
cat <<EOD > temp.sql
INSERT INTO mytable
VALUES ('$field1', '$field2', '$field3');
commit
quit
EOD
sqlplus -s user/pass@database @ temp.sql
rm -f temp.sql
---------------------
Hi Matthew,
I am new to shell scripting and PL SQL. I tried executing your script. I am encountering an error as ': not a valid identifier
/usr/home/kiran> ./temp.sql
enter value for c1 test1
': not a valid identifier
enter value for c2 test2
': not a valid identifier
enter value for c3 test3
': not a valid identifier
: command not found
---------------------------------------
Well, there is no shell API for inserting directly into the database, so you need to use some tool or other. sqlplus is such a tool which works with oracle. I don't know about HSQL - I would assume there is some analogue to sqlplus.
When I say "create an .sql file", you can do that in a script. You didn't say what data needs to be entered, so here's a pretty abstract example:
Code:
#!/bin/bash
read -p "enter value for field1> " field1
read -p "enter value for field2> " field2
read -p "enter value for field3> " field3
cat <<EOD > temp.sql
INSERT INTO mytable
VALUES ('$field1', '$field2', '$field3');
commit
quit
EOD
sqlplus -s user/pass@database @ temp.sql
rm -f temp.sql
But again, that can be done without creating an sql script outside of the bash script to be called.
Code:
#!/bin/bash
read -p "enter value for field1> " field1
read -p "enter value for field2> " field2
read -p "enter value for field3> " field3
sqlplus -s user/pass@database <<-EOF
INSERT INTO emp VALUES ('$c1', '$c2', '$c3');
commit
EOF
replace the sql script creation and deletion with the above and you are done. The - in front of EOF ignores white space if you indent your sql commands.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.