2

In my application I need to import data from csv files into sqlite database.

For working with sqlite I use FMDB library, because it is easy to use:)

Help me please, how to import data from csv file to a table?

I have a table created, corresponding to a table in csv file, so it should be ease to import.

I looked at solutions around the internet, and found terminal commands for sqlite such as

".mode csv"
".separator ';'"
".import mycsvfile.csv mytablename"

But when I try to execute this sql statements in FMDB, it fails..

My source code:

   [db beginTransaction];
   NSString *query1 = @".mode csv";
   NSString *query2 = @".separator ';'";
   NSString *query3 = @".import mycsvfile.csv mytablename";

   BOOL result = [db executeUpdate:query];
   result = [db executeQuery:query2];
   result = [db executeQuery:query3];

   [db commit];

Here after stepping over BOOL result = [db executeUpdate:query]; result equals to FALSE, indicating that it was error..

After turning on logging in FMDB, it says "incorrect syntax" in my queries.

But why does this statements work in OS X's sqlite?

Note: when i am trying to execute this commands in OS X terminal, in sqlite3, all works correctly.

3
  • can you display your .csv file structure.. Commented May 2, 2014 at 12:49
  • Hi! I solved this problem by manually reading csv file line by line and executing sqlite insert statements with that data Commented May 5, 2014 at 8:59
  • If someoune is still interested in the solution, now there is an open-source library for importing CSV to SQLite — github.com/dodikk/CsvToSqlite Commented May 28, 2014 at 11:45

1 Answer 1

3
+25

The .import command that you are using isn't part of SQLite itself, rather it is part of the SQLite CLI (Command Line Interface) shell. Docs for this tool are here. You can download the source code for the CLI tool (file is shell.c, I think) as part of the SQLite distribution from sqlite.org.

If you want to import CSV data in an iOS app to SQLite you'll need to do the manual parsing of the CSV file yourself, as well as inserting the data into tables in your SQLite database.

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

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.