5

i want to create a new db of sqlite using A SHELL SCRIPT on an ubuntu10.10 OS....Any ideas??

I tried 'create.sh' file with following code...

#!/bin/bash
sqlite ex3.db
create table t1(f1 integer primary key,f2 text)

than run a ./create.sh from termminal but it leads me to sqlite> prompt...I dont see created DB ex3 anywhere..

Please help ...

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
Roopa
  • 159
  • 4
  • 11

5 Answers5

5

One more way to accomplish this can be using SQL_ENTRY_TAG_N

#!/bin/bash
sqlite3 mydatabase <<SQL_ENTRY_TAG_1
SELECT * FROM sqlite_master;
SQL_ENTRY_TAG_1

see snippet for details

4
#!/bin/bash

sqlite3 ex3.db "create table t1(f1 integer primary key,f2 text)"

should work I think, unfortunately, not able to check right now.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Vlad Khomich
  • 5,820
  • 1
  • 27
  • 39
  • 1
    You don't need the backticks inside a shell script unless you want to capture the command's output in a variable. OTOH, you **can** give SQLite small chunks of SQL via the command line like that and it will still work with the backticks. You'd probably want to use the standard input for more complicated chunks of SQL though. – mu is too short Apr 21 '11 at 08:21
3

You want to feed your SQL DDL commands to SQLite through the standard input:

#!/bin/bash
echo 'create table t1(f1 integer primary key,f2 text);' | sqlite ex3.db
mu is too short
  • 426,620
  • 70
  • 833
  • 800
1

Can you use the param -line on sqlite3, for example:

$sqlite3 -line teste.db 'create table table_name (field_name integer)'
$sqlite3 -line teste.db 'insert into table_name (123)'
$sqlite3 -line teste.db 'select * from table_name'
fdavid
  • 111
  • 1
0

Example of CREATE and INSERT of create.sh:

#!/bin/bash
DB_NAME=ex3.sqlite
DB_TABLE=t1

sqlite3 $DB_NAME << EOF

DROP TABLE IF EXISTS $DB_TABLE;

CREATE TABLE $DB_TABLE (
    "f1" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,
    "f2" TEXT NOT NULL DEFAULT "f2-text"
);

INSERT INTO $DB_TABLE (f1, f2) VALUES (1, "text 1");
INSERT INTO $DB_TABLE (f1, f2) VALUES (2, "text 2");

EOF