The records are then processed one by one. An SQL ‘ Insert‘ statement is constructed, incorporating the values from the record in the records object. If successfully found, the file is assigned to a reader object and the rows of data, or records, are assigned to a records object. A further check is made to see if the CSV file exists. If it does, a connection to the database is established. 'firstname','lastname','title','dob'īelow is an example of how Java can be used to import data from a CSV file called ‘personimport.csv’, into a table called ‘person’, which was used in the examples for selecting, inserting, updating and deleting data. For this to work the Apache Commons CSV dependency needs to be used, along with the SQLite JDBC Driver used previously.įirstly, a check is made to see if the database file actually exists. In the example contents of a CSV file below, each row contains information relating to a person, including their first name, last name, title and date of birth in the format YYYY-MM-DD (four digit year, two digit month and two digit day). Each row in the file thereafter is a record of related data.
The first row in a CSV file often contains headers for each column of data. A CSV file, or Comma Separated Values file, is a delimited file that uses commas to separate values. In order to import data into an SQLite database, it must be in a suitable format.