Read schema from a file into SQLite database

  • Follow


Dear list,

I am having a silly problem, and need your expertise. I just want to
initiate a SQLite database using a schema file, but I just get an
empty database whatever I do. Loading from the "sqlite3" command line
client is no problem though..

Here is what I am doing:

proc init_db {dbDir {dbFile db.sqlite3} {force 1} } {

	set dbFile [file join [file normalize $dbDir] $dbFile ]
	if { $force == 1 && [file exists $dbFile]} {
		file delete $dbFile
	}
	sqlite3 db $dbFile
	set schemaFile [file normalize [file join  .. setup 1_schema.sql] ]

	if {! [file exists $schemaFile] } {
		return -code error "Unable to open schema file $schemaFile"
	}
	set inf [open $schemaFile r]
	set sql [read $inf]
	close $inf

	db eval {$sql}

	db close
	return [file normalize $dbFile ]
}

What am I doing wrong?

/Fredrik
0
Reply Fredrik 11/18/2010 9:23:00 PM

On 11/18/2010 3:23 PM, Fredrik Karlsson wrote:
> Dear list,
>
> I am having a silly problem, and need your expertise. I just want to
> initiate a SQLite database using a schema file, but I just get an
> empty database whatever I do. Loading from the "sqlite3" command line
> client is no problem though..
>
> Here is what I am doing:
>
> proc init_db {dbDir {dbFile db.sqlite3} {force 1} } {
>
> 	set dbFile [file join [file normalize $dbDir] $dbFile ]
> 	if { $force == 1&&  [file exists $dbFile]} {
> 		file delete $dbFile
> 	}
> 	sqlite3 db $dbFile
> 	set schemaFile [file normalize [file join  .. setup 1_schema.sql] ]
>
> 	if {! [file exists $schemaFile] } {
> 		return -code error "Unable to open schema file $schemaFile"
> 	}
> 	set inf [open $schemaFile r]
> 	set sql [read $inf]
> 	close $inf
>
> 	db eval {$sql}
>
> 	db close
> 	return [file normalize $dbFile ]
> }
>
> What am I doing wrong?
>
> /Fredrik

I didn't analyze your code closely, but this is almost certainly wrong:

db eval {$sql}

I'd guess that should be:

db eval $sql

That's assuming that your "sql" variable really contains the SQL you expect.

Jeff
0
Reply Jeff 11/18/2010 9:40:28 PM


Hi,

Yes, the string is a valid SQLite SQL string, so I'm now thinking that
this may be an issue with differences in the command line client and
the Tcl interface. Anyway, db eval $sql does not work.

Will post on the SQLite list to make sure.

Thanks!

/Fredrik

On 18 Nov, 22:40, Jeff Godfrey <jeff_godf...@pobox.com> wrote:
> On 11/18/2010 3:23 PM, Fredrik Karlsson wrote:
>
>
>
> > Dear list,
>
> > I am having a silly problem, and need your expertise. I just want to
> > initiate a SQLite database using a schema file, but I just get an
> > empty database whatever I do. Loading from the "sqlite3" command line
> > client is no problem though..
>
> > Here is what I am doing:
>
> > proc init_db {dbDir {dbFile db.sqlite3} {force 1} } {
>
> > =A0 =A0set dbFile [file join [file normalize $dbDir] $dbFile ]
> > =A0 =A0if { $force =3D=3D 1&& =A0[file exists $dbFile]} {
> > =A0 =A0 =A0 =A0 =A0 =A0file delete $dbFile
> > =A0 =A0}
> > =A0 =A0sqlite3 db $dbFile
> > =A0 =A0set schemaFile [file normalize [file join =A0.. setup 1_schema.s=
ql] ]
>
> > =A0 =A0if {! [file exists $schemaFile] } {
> > =A0 =A0 =A0 =A0 =A0 =A0return -code error "Unable to open schema file $=
schemaFile"
> > =A0 =A0}
> > =A0 =A0set inf [open $schemaFile r]
> > =A0 =A0set sql [read $inf]
> > =A0 =A0close $inf
>
> > =A0 =A0db eval {$sql}
>
> > =A0 =A0db close
> > =A0 =A0return [file normalize $dbFile ]
> > }
>
> > What am I doing wrong?
>
> > /Fredrik
>
> I didn't analyze your code closely, but this is almost certainly wrong:
>
> db eval {$sql}
>
> I'd guess that should be:
>
> db eval $sql
>
> That's assuming that your "sql" variable really contains the SQL you expe=
ct.
>
> Jeff

0
Reply Fredrik 11/20/2010 1:06:04 PM

2 Replies
466 Views

(page loaded in 0.044 seconds)

Similiar Articles:













7/24/2012 4:54:46 AM


Reply: