Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Ivan Shmakov
> Pete Helgren writes:

 > I may end up going this direction, at the moment I am not having much
 > luck with the conditional copy in Busybox.  Your suggestion:

 > cp -n newdatabase.db /data/newdatabase.db

 > Isn't supported in the version of Busybox that I am running.  Also
 > the script example I tried:

 > if  [ -f /data/newdatabase.db];
 > then
 > echo "Nothing to do, database exists"
 > else
 > cp newdatabase.db /data/newdatabase.db
 > fi

 > delivers the error  [:missing]

The primary token delimiter in POSIX Shell is space.  Hence, the
following line:

   if  [ -f /data/newdatabase.db];

Is understood as: “check if the file ‘/data/newdatabase.db]’
exists” (note the closing bracket), and it certainly lacks a
closing bracket for the ‘test’ (AKA ‘[’) command.

The solution would be as follows:

 - if  [ -f /data/newdatabase.db];
 + if  [ -f /data/newdatabase.db ];

OTOH, the when the ‘test’ form of the command is used, closing
bracket is not necessary, thus:

   if  test -f /data/newdatabase.db ;

Please also consider joining the news:comp.unix.shell newsgroup
(e. g., on Aioe, nntp://aioe.org/comp.unix.shell/), as there're
quite a few folks familiar with the arcane art of Unix Shell
programming.  (AFAIK, Thunderbird has the support for the
Internet News service.)

[…]

-- 
FSF associate member #7257  Coming soon: Software Freedom Day
http://mail.sf-day.org/lists/listinfo/ planning-ru (ru), sfd-discuss (en)

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Simon Slavin

On 31 Aug 2011, at 3:53am, Pete Helgren wrote:

> I may end up going this direction, at the moment I am not having much luck 
> with the conditional copy in Busybox.  Your suggestion:
> 
> cp -n newdatabase.db /data/newdatabase.db
> 
> Isn't supported in the version of Busybox that I am running. 

Oh, you're running BusyBox, not a standard shell.  okay, well here's a doc for 
BusyBox:



see whether cp will overwrite without any options set.  Try it out.  See what 
happens.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Jay A. Kreibich
On Tue, Aug 30, 2011 at 08:29:06PM -0600, Pete Helgren scratched on the wall:
> The only issue I had was finding an example of how I could do all of
> what you describe below in bash script.  For example, if I put this
> in a script:
> 
> sqlite3 newdatabase.db
> 
> and save that as createdb.sh and execute it then the script never
> completes because SQLite is at the sqlite> prompt, waiting for
> commands.  Hence that option is a non-starter.

  You need to give sqlite3 a command, or it will go into interactive
  mode.  That's how the shell is designed to work.
  
  You can do this, however:

$ sqlite3 newdatabase.db .exit

  The existence of the command will cause sqlite3 to execute the
  command and quit, without going into interactive mode.  As I 
  explained before, this specific example won't actually create a
  database file, however.

  I suppose you could do something this:

sqlite3 newdatabase.db "CREATE TABLE IF NOT EXISTS ..."
sqlite3 newdatabase.db "CREATE TABLE IF NOT EXISTS ..."
...

  But that seems a bit wasteful.  If you want to do all your
  initialization in one pass, I would do something like this:

sqlite3 newdatabase.db << EOF
CREATE TABLE IF NOT EXISTS t1 ( a, b, c );
CREATE TABLE IF NOT EXISTS t2 ( d, e, f );
EOF

  -j

-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Pete Helgren
I may end up going this direction, at the moment I am not having much 
luck with the conditional copy in Busybox.  Your suggestion:


cp -n newdatabase.db /data/newdatabase.db

Isn't supported in the version of Busybox that I am running.  Also the 
script example I tried:


if  [ -f /data/newdatabase.db];
then
echo "Nothing to do, database exists"
else
cp newdatabase.db /data/newdatabase.db
fi

delivers the error  [:missing]

So I'll have to work through the scripting.  Sure would be nice to have 
something like "sqlite3 newdatabase.db .exit" work so that it would just 
create the DB and exit


Pete Helgren
Value Added Software, Inc
www.asaap.com
www.opensource4i.com


On 8/30/2011 8:14 PM, Simon Slavin wrote:

Forgot to mention: copying an existing database file also lets you set up the 
file the way you want without having to issue separate commands.  For instance, 
you could create blank tables.  Or set a specific page size.  Or include some 
sort of DRM or security check in the 'blank' file.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Pete Helgren
The only issue I had was finding an example of how I could do all of 
what you describe below in bash script.  For example, if I put this in a 
script:


sqlite3 newdatabase.db

and save that as createdb.sh and execute it then the script never 
completes because SQLite is at the sqlite> prompt, waiting for 
commands.  Hence that option is a non-starter.


Pete Helgren
Value Added Software, Inc
www.asaap.com
www.opensource4i.com


On 8/30/2011 8:23 PM, Jay A. Kreibich wrote:

   Of course, I'm not sure what the big deal is.  By default, if you
   attempt to open an SQLite database file that does not exist, the
   system will just go ahead and create it.  This sounds like exactly
   the desired behavior.  There is no need to pre-create the file.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Jay A. Kreibich
On Tue, Aug 30, 2011 at 09:54:21PM -0400, Igor Tandetnik scratched on the wall:
> Pete Helgren  wrote:
> > I have a need to create a database if it doesn't already exist.  The
> > obvious solution is to just use:
> > 
> > sqlite3 newdatabase.db
> > 
> > Except that it not only creates the db but also opens that db for
> > commands.  I am running this from a script so I want to just want to run
> > the command from a script so that I know the database exists before
> > issuing other commands.
> 
> Try something like
> 
> echo ".exit" | sqlite3 newdatabase.db

  Except that won't work**, since creating the database file is a lazy
  operation.  There are several ways to force the creation of a
  zero-byte file (open/commit a transaction, for example), but that can
  be done with something as simple as "touch(1)".
 
  Creating the file and writing the full database header (making it a
  recognizable SQLite file) requires putting something into the
  sqlite3_master table (e.g. creating a user-defined table).  This
  could be done with any CREATE TABLE IF NOT EXISTS... statement.


  Of course, I'm not sure what the big deal is.  By default, if you
  attempt to open an SQLite database file that does not exist, the
  system will just go ahead and create it.  This sounds like exactly
  the desired behavior.  There is no need to pre-create the file.
  
  Assuming the start-up process continues with a series of CREATE
  TABLE IF NOT EXISTS... statements, a new database will have the file
  created and defined, while an existing database will create/ignore
  the tables depending on the existing structure.



  ** Who are you, and what did you do with Igor?

   -j

-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Simon Slavin
Forgot to mention: copying an existing database file also lets you set up the 
file the way you want without having to issue separate commands.  For instance, 
you could create blank tables.  Or set a specific page size.  Or include some 
sort of DRM or security check in the 'blank' file.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Simon Slavin

On 31 Aug 2011, at 2:53am, Pete Helgren wrote:

> Thanks.  I'll add a little more info
> 
> This script is used to set up the initial DB in a programmable device that 
> will then record data to the database and the database should never be 
> replaced.  So I just figured there would be a simple way to issue the sqlite 
> commands in script.

The mechanism in SQLite which creates a database is to open one that doesn't 
exist.  There's no command or C function which just makes a database without 
opening it.  You could, of course, hack that functionality out of the source 
code but I think that's a poor solution.

> Even found an example using a createdb command, although I could never see 
> where that was an SQLite command
> 
> So, you suggest I script it like so:
> 
> if [ -f /data/newdatabase.db];
> then
> echo "Nothing to do, database exists"
> else
> cp newdatabase.db /data/newdatabase.db
> fi
> 
> I am not much of a Linux guy so the scripting might be wrong.

That would do fine.  But as a single-command alternative you could use 'cp -n':

cp -n newdatabase.db /data/newdatabase.db

the '-n' means 'don't replace an existing file'.  I tested it on my Unix box.  
I believe it's implemented in Linux, but you should definitely test it because 
I don't have Linux here.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Igor Tandetnik
Pete Helgren  wrote:
> I have a need to create a database if it doesn't already exist.  The
> obvious solution is to just use:
> 
> sqlite3 newdatabase.db
> 
> Except that it not only creates the db but also opens that db for
> commands.  I am running this from a script so I want to just want to run
> the command from a script so that I know the database exists before
> issuing other commands.

Try something like

echo ".exit" | sqlite3 newdatabase.db

-- 
Igor Tandetnik

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Pete Helgren

Thanks.  I'll add a little more info

This script is used to set up the initial DB in a programmable device 
that will then record data to the database and the database should never 
be replaced.  So I just figured there would be a simple way to issue the 
sqlite commands in script.  Even found an example using a createdb 
command, although I could never see where that was an SQLite command


So, you suggest I script it like so:

if [ -f /data/newdatabase.db];
then
echo "Nothing to do, database exists"
else
cp newdatabase.db /data/newdatabase.db
fi

I am not much of a Linux guy so the scripting might be wrong.

Pete Helgren
Value Added Software, Inc
www.asaap.com
www.opensource4i.com


On 8/30/2011 7:38 PM, Simon Slavin wrote:

On 31 Aug 2011, at 2:36am, Pete Helgren wrote:


I have a need to create a database if it doesn't already exist.  The obvious 
solution is to just use:

sqlite3 newdatabase.db

Except that it not only creates the db but also opens that db for commands.

Make yourself an empty database file and keep it somewhere safe.  When you need 
a new one just copy this existing file, and rename and/or move it to the right 
folder.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Simon Slavin

On 31 Aug 2011, at 2:36am, Pete Helgren wrote:

> I have a need to create a database if it doesn't already exist.  The obvious 
> solution is to just use:
> 
> sqlite3 newdatabase.db
> 
> Except that it not only creates the db but also opens that db for commands.

Make yourself an empty database file and keep it somewhere safe.  When you need 
a new one just copy this existing file, and rename and/or move it to the right 
folder.

Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Creating a database with a script or SQLite command

2011-08-30 Thread Pete Helgren
I have a need to create a database if it doesn't already exist.  The 
obvious solution is to just use:


sqlite3 newdatabase.db

Except that it not only creates the db but also opens that db for 
commands.  I am running this from a script so I want to just want to run 
the command from a script so that I know the database exists before 
issuing other commands.


I searched around the Internet for what I thought would be an easy 
answer and didn't find one.  I am running SQLite 3.3.13 from BusyBox 1.1.3


Thanks

--
Pete Helgren
Value Added Software, Inc
www.asaap.com
www.opensource4i.com

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users