title: Using CSV file in node-sqlite3 package
body: <p>I'm using the npm <code>sqlite3</code> package in my web application for a mock-up restaurant (for learning purposes). In my current implementation, when my local server is started, I'm creating a <code>menuItems</code> table, like so:</p>
  
  <pre><code>var db = new sqlite3.Database(':memory:');
  db.serialize(function() {
    db.run('CREATE TABLE menuItems ('
            + 'itemName VARCHAR(255),'
            + 'itemDescription VARCHAR(255),'
            + 'unitPrice REAL'
            + ');')
    .run("INSERT INTO menuItems (itemName, itemDescription, unitPrice) VALUES"
            + " ('Fish Filet', 'Yummy fish in a sandwich.', 9.95)")
  });
  </code></pre>
  
  <p>However, I would like for my <code>menuItems</code> table to not be hard-coded in the <code>js</code> file and would like for the actual items to be separate from this <code>js</code> file. There are a few solutions that I can think of:</p>
  
  <ol>
  <li><p>Use current implementation and hard-code commands to insert individual items into the table.</p></li>
  <li><p>Use SQLite3 to import the <code>csv</code> file and insert it into the table.*</p></li>
  <li><p>Read in items from a <code>csv</code> file and insert them via a loop.</p></li>
  <li><p>Establish database before the server starts up and pass it into the <code>sqlite3.Database()</code> constructor. </p></li>
  </ol>
  
  <p><sup>*I'd like to go with option 2. However, since this is for learning purposes, I am open to any suggestions, including but not limited to using a different database-managing package or using a different kind of file (maybe just a <code>txt</code> file or something).</sup></p>
  
  <p>I know there are <a href="http://www.sqlitetutorial.net/sqlite-import-csv/" rel="nofollow noreferrer">some ways</a> to import the <code>csv</code> file using SQLite3. I'm trying to do the same thing but from the <code>sqlite3</code> npm package. When I try to import the file via the same command (which seems to be a sqlite3-specific command) from the linked page,</p>
  
  <pre><code>db.run('.import "C:/Users/path/to/csv/file.csv"'
        + 'INTO TABLE menuItems'
        + 'FIELDS TERMINATED BY ","'
        + 'ENCLOSED BY "\'"'
        + 'LINES TERMINATED BY \'\\n\''
        + 'IGNORE 1 ROWS'
        + ');');
  </code></pre>
  
  <p>I receive the error</p>
  
  <pre><code>events.js:183
    throw er; // Unhandled 'error' event
    ^
  
  Error: SQLITE_ERROR: near ".": syntax error
  </code></pre>
  
  <p>Trying to use <a href="https://stackoverflow.com/questions/15242757/import-csv-file-into-sql-server">normal SQL syntax</a>,</p>
  
  <pre><code>    .run('BULK INSERT menuItems'
            + 'FROM "C:/Users/path/to/csv/file.csv" '
            + 'WITH '
            + '{ '
            + 'FIRSTROW = 2,'
            + 'FIELDTERMINATOR = ","'
            + 'ROWTERMINATOR = "\n",'
            + 'ERRORFILE = "C:/Users/path/to/csv/error_file.csv" '
            + 'TABLOCK'
            + '}')
  </code></pre>
  
  <p>I receive the error</p>
  
  <pre><code>events.js:183
        throw er; // Unhandled 'error' event
        ^
  
  Error: SQLITE_ERROR: near "BULK": syntax error
  </code></pre>
  
  <p>Is something in my syntax incorrect? Is there a better/working/more efficient method to do this? Option 3 seems like it would work, but I haven't tried it yet.</p>