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) {