7

Trying to convert an SqlCe database to SQLite, I export it to a .sql file. Now how would I use sqlite.exe to create a database from this .sql file?

Where to put the sql3.exe file?
What command syntax to use, in cmd prompt or in the sqlite.exe shell?

3 Answers 3

9

Use following command line:

sqlite3 -init dump.sql newsqlite.db ""

It will create new SQLite database file newsqlite.db by executing statements from dump.sql. Empty string "" is needed for sqlite3 to quit automatically.

If newsqlite.db file already existed with some data, import may fail unless you use IF NOT EXISTS for all table and index creation statements.

Sign up to request clarification or add additional context in comments.

3 Comments

Cheers! Still, cmd prompt gives me the message : -- Loading resources from script.sql then Error: incomplete SQL: |- (| beeing a rectangle), while sqlite3.exe shell for the same command gives me just a new line ...> Either way not much of an error message, no line number, no nothing. Is that typical?
For this to work, your script.sql must be valid - it clearly isn't. I'd suggest to debug your script line by line. In windows, you should install SQLite Expert Personal and use this GUI to carefully debug your script - there is something wrong in it.
Thank you so much for the hint with the empty string! It costs me ca. 4 hours to find this. Why is this not properly documented for Windows users? If I understand the syntax right, then it is like this for automation-tasks: sqlite3.exe -init "[scriptname]" "[databasename]" ""
7

Put sqlite3.exe wherever you want, as long as you remember that place and you're able to start sqlite3 from there.

Applying a script to a database (maybe a newly-created one), in command prompt:

sqlite3.exe my-new-db.somesuffix < myscript.sql

Executing a script within interactive sqlite3 session:

sqlite3.exe my-new-db.somesuffix
....
.read myscript.sql
....

Both variants are valid and usable at times. (Note: if your .sql was generated for non-sqlite database, I'd expect that it will require some changes to work in sqlite3. And things like stored procedures and user-defined functions will be definitely lost).

4 Comments

Thanks! So from cmd I get Error: incomplete SQL: |-, and from sqlite3 shell it just gives more ....> lines when hitting enter, no message. In neither way any database file was created. Is that all I can expect for error message?
incomplete SQL is probably caused by syntax differences between sqlite and your source database, we can't solve it without looking at the code. As of the ....> prompt in sqlite shell, I think you omitted the leading dot before read. It should be .read and not just read. (Why everyone asking questions feels so confident when it comes to editing commands provided in the answers?)
I'm not editing your commands, but maybe I don't use them correctly. Should I create the database before running these commands? I don't seem to get even an empty database created, or shouldn't that happen when executing the first line you gave in sqlite3 session? Or how to create an empty database from cmd?
Database is created after a single successful sql statement (even a do-nothing statement like select 1 as result -- but then it can be a file of size 0). You can work with an existing empty database if you prefer, but it's better to start from scratch each time while you're debugging your script (otherwise you'll have to solve problems with objects already existing from previous attempts: add DROP TABLE IF EXISTS... before each CREATE TABLE, etc).
0

Although the thread is years old but I have not seen anywhere the option I propose:

set "database=example.db"
%~dp0sqlite3.exe -cmd ".read script.sql" %database%.

This option after executing the script leaves open a command window with sqlite running and the database open.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.