1

I am trying to insert a large number of records into a SQLite database. I get the above error if I try to use the sqlite3_exec C-API.

The code looks like this:

ret = sqlite_exec(db_p,".import file.txt table", NULL, NULL, NULL);

I know that the .import is command line, but can there be any way that you can do a extremely large insert of records that takes minimal time. I have read through previous bulk insert code and attempted to make changes but these are not providing the desired results.

Is there not a way to directly insert the string into the tables without having intermediate API's being called?

2
  • 1
    Could you add a snippet of code that is generating SQL and calling sqlite3_exec? This error is usually caused by either a syntax error or formatting error. Commented Feb 23, 2011 at 12:36
  • ret = sqlite_exec(db_p,".import file.txt table", NULL, NULL, NULL); Commented Feb 23, 2011 at 12:40

2 Answers 2

3

.import is most probably not available via the API. However there's one crucial thing to speed up inserts: wrap them in a transaction.

BEGIN;
lots of insert statements here;
COMMIT;

Without this, sqlite will need to write to the file after each insert to keep the ACID principle. The transaction let's it write to file later in bulk.

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

3 Comments

This is the right answer. SQLite is a library. .import and other dot-commands are part of the SQLite shell, which is just a sample program (though official) built upon SQLite. However you should add semicolons after BEGIN and COMMIT.
do I need to have the sqlite3_exec between each transaction for individual inserts that I do here? Thanks :-)
@SportyMe: take a look at the source code for the shell utility (sqlite.org/src/…). Search for "import" including the quotes. They create a transaction using BEGIN/END, use a prepared statement for the INSERT calling sql3_bind_text for each column and sql3_step for each row. The code is quite readable.
0

The answer to the syntax error could well be, that your strings are not enclosed in quotes in your SQL statement.

1 Comment

There has to be a way to use the command line equivalents in the C API functions? I have posted the code snippet that the error is reported as above.

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.