Re: [sqlite] Creating a database from inside a program

2007-01-02 Thread Dennis Cote

Rob Richardson wrote:
 


So, I thought I could just issue the following command from inside my
program:

 


Sqlite3 newdatabase.db .read schemafile.txt

 


But, when I issue this command from the DOS prompt,  it gives me an
error message saying that there is no command named "read".

Rob,

You can pass your schema command file into sqlite in 3 ways. The first 
is using the -init option.


   sqlite3 -init schemafile.txt newdatabase.db

The second is using input redirection.

   sqlite3 newdatabase.db < schemafile.txt

The third is supplying a read meta command on the command line. Note, 
you must quote the command so it gets passed to sqlite as a single string.


   sqlite3 newdatabase.db ".read schemafile.txt"

HTH
Dennis Cote

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] Creating a database from inside a program

2006-12-29 Thread Ken

I have a nice solution that really works well, at least from C... 
1. Create a template Database. (using whatever method you like, either 
embedded in your code or via sqlite3).
 2. Copy the database to a new file, using plane old cp, copy, or if you 
like an in code copy using open, read/write, close... Then open the newly 
created copy.
 
 I've found that if you need mulitples of a single database structure this is a 
very fast way to do this, a lot faster than creating the DB and then creating 
the individual tables/indices.
 
 If you want really, really fast, you could load your templated db into memory 
and then just write this to disk whenever you need a new DB.
 
 
 
Kees Nuyt <[EMAIL PROTECTED]> wrote: On Fri, 29 Dec 2006 12:33:46 -0500, you 
wrote:

> Sqlite3 newdatabase.db .read schemafile.txt
>
> But, when I issue this command from the DOS prompt, 
> it gives me an error message saying that there 
> is no command named "read".  

Try input redirection:

Sqlite3 newdatabase.db 

If your schemafile.txt contains valid SQLite statements, it
should work.

HTH
-- 
  (  Kees Nuyt
  )
c[_]

-
To unsubscribe, send email to [EMAIL PROTECTED]
-




Re: [sqlite] Creating a database from inside a program

2006-12-29 Thread Kees Nuyt
On Fri, 29 Dec 2006 12:33:46 -0500, you wrote:

> Sqlite3 newdatabase.db .read schemafile.txt
>
> But, when I issue this command from the DOS prompt, 
> it gives me an error message saying that there 
> is no command named "read".  

Try input redirection:

Sqlite3 newdatabase.db 

Re: [sqlite] Creating a database from inside a program

2006-12-29 Thread Clay Dowling
Open the database as per normal with sqlite3_open().  Then issue the SQL
commands necessary to create your schema.  I have a nice little utility I
wrote which will take an SQLite schema dump and convert it to an array of
C strings that you can issue in sequence (and thanks to Microsoft for the
technique).

I've included the utility which converts an SQL export to C code.  I'll
let you work out the details for the rest.

Clay Dowling

Rob Richardson said:
> Greetings!
>
>
>
> I need to be able to create a database with a known schema from inside a
> program.  I used sqlite3.exe's .schema command to export the SQL needed
> to create the schema for my new database.  I can create the database by
> the following steps:
>
>
>
> C:\: sqlite3 newdatabase.db
>
> Sqlite3> .read schemafile.txt
>
>
>
> At that point, my database is ready to go.
>
>
>
> But, sqlite3 is supposed to be able to accept commands on its command
> line.  For example, the schema file was generated using this command:
>
>
>
> C:\: sqlite3 existingdatabase.db .schema > schemafile.txt
>
>
>
> So, I thought I could just issue the following command from inside my
> program:
>
>
>
> Sqlite3 newdatabase.db .read schemafile.txt
>
>
>
> But, when I issue this command from the DOS prompt,  it gives me an
> error message saying that there is no command named "read".  (Note the
> lack of a leading period.)  If this won't work from a DOS prompt, I'm
> sure it won't work from my program.  So, what is the recommended way to
> create a new database and its schema from inside a program?  In case it
> matters, I'll be using Visual C# 2005 and the SQLite.net.dll file from
> SourceForge.
>
>
>
> Thank you very much!
>
>
>
> Rob Richardson
>
> RAD-CON INC.
>
>


-- 
Simple Content Management
http://www.ceamus.com/* This file (c) Copyright 2004, 2005, 2006 Lazarus Internet Development
 *
 * Permission is given to use this source code for personal or 
 * non-profit use free of charge, so long as this copyright is
 * maintained.  You may use this source code for commercial use
 * so long as you have obtained a license from Lazarus Internet
 * Development.  A license may be purchased by writing to
 * Clay Dowling <[EMAIL PROTECTED]>
 *
 * If you modify this source code and distribute it you must 
 * indicate such in this header, and provide all support for
 * your modified version.
 *
 * $Id: sqlmodule.c 142 2006-05-28 17:43:28Z clay $
 */

/* Create a C module which will return an array of strings, one for each
 * of the SQL statements in the passed file name
 *
 * This program runs on the SQL dumps/scripts produced by most SQL databases.
 * It's only requirement is that each statement ends with a semicolon (';').
 * This rules out SQL Server, which terminates a statement with GO on a line
 * by itself.  It should be a pretty easy adaptation though if such a feature
 * is needed.
 *
 * Comments and empty lines are ignored.
 * Lines with text are added to the array with a newline appended and
 * any single or double quotes escaped.
 * Lines which have a semi-colon cause a new array entry to be created
 */

#include 
#include 
#include 
#include 

#define LINE_SIZE 2048

void markcomment(char*);
const char* readline(FILE*);
void escapeline(char*);
void trim_trailing_space(char*);

int main(int argc, char** argv) {

  FILE* in;
  FILE* out;
  char* filename;
  char* base;
  char* outfile;
  const char* line;

  if (argc == 1) {
fprintf(stderr, "usage: %s file.sql\n", argv[0]);
return EXIT_FAILURE;
  }

  in = fopen(argv[1], "r");
  if (!in) {
perror(argv[1]);
return EXIT_FAILURE;
  }
  
  filename = (char*)calloc(1, strlen(argv[1]) + 1);
  strcpy(filename, argv[1]);
  base = strrchr(filename, '.');
  if (base) *base = '\0';
  outfile = (char*)calloc(1, strlen(filename) + 6);
  snprintf(outfile, strlen(filename) + 6, "mod%s.c", filename);

  out = fopen(outfile, "w");
  if (!out) {
perror(outfile);
return EXIT_FAILURE;
  }

  fprintf(out, "/* auto-generated SQL module */\n\n");
  fprintf(out, "char** getsql() {\n");
  fprintf(out, "  static char *sql[] = {\n");

  while((line = readline(in))) {
  	if (strlen(line) > 0) {
	  fprintf(out, "  \"%s\"", line);
  if (strchr(line, ';')) fprintf(out, ",\n");
	  fprintf(out, "\n");
  	}
  }
  fprintf(out, "  0};\n\n");
  fprintf(out, "  return sql;\n");
  fprintf(out, "}\n");

  fclose(in);
  fclose(out);
  
 
  return EXIT_SUCCESS;

}

const char* readline(FILE* in) {

static char line[LINE_SIZE];
memset(line, 0, LINE_SIZE);

if (fgets(line, LINE_SIZE/2, in)) {
  markcomment(line);
  trim_trailing_space(line);
  escapeline(line);
  return line;
}
else
  return NULL;

}

void markcomment(char* line) {

char* pos;

pos = strstr(line, "--");
if (pos)
	*pos = '\0';

}

void escapeline(char* line) {

static char work[LINE_SIZE];
char* dst;
char* src;

memset(work, 0, LINE_SIZE);
dst = work;
src = line;
while(*src) {
	

[sqlite] Creating a database from inside a program

2006-12-29 Thread Rob Richardson
Greetings!

 

I need to be able to create a database with a known schema from inside a
program.  I used sqlite3.exe's .schema command to export the SQL needed
to create the schema for my new database.  I can create the database by
the following steps:

 

C:\: sqlite3 newdatabase.db

Sqlite3> .read schemafile.txt

 

At that point, my database is ready to go.  

 

But, sqlite3 is supposed to be able to accept commands on its command
line.  For example, the schema file was generated using this command:

 

C:\: sqlite3 existingdatabase.db .schema > schemafile.txt

 

So, I thought I could just issue the following command from inside my
program:

 

Sqlite3 newdatabase.db .read schemafile.txt

 

But, when I issue this command from the DOS prompt,  it gives me an
error message saying that there is no command named "read".  (Note the
lack of a leading period.)  If this won't work from a DOS prompt, I'm
sure it won't work from my program.  So, what is the recommended way to
create a new database and its schema from inside a program?  In case it
matters, I'll be using Visual C# 2005 and the SQLite.net.dll file from
SourceForge.

 

Thank you very much!

 

Rob Richardson

RAD-CON INC.