I’m quite new to SQLite databases and need help using an external text file to create an initial database to store a bunch of items that can be selected and added to a second database. To put this all in context, I want to have an initial database that stores a list of grocery store items. Each item will have two traits with them, a NAME and a TYPE. So there are items such as( NAME Bread, TYPE Bakery. NAME Grape Juice, TYPE Beverage. NAME Toothbrush, TYPE Toiletries. )
I need to go from something like this:
GROCERYSTORE.TXT
-- -- -- -- -- --
NAME - TYPE
Bread - Bakery
Rolls - Bakery
Juice - Beverages
Soda - Beverages
to this:
dBHelper.insert("Bread", "Bakery");
or:
dBHelper.insert(name, type);
for each item in the text file.
First of all, I would like to know how to format this text file, or any other type of file that could be read and inserted into a database if a text file is not the easiest way to go about this.
Second, how would I go about reading this text file and inserting it into the database?
I’m thinking I would use a buffered reader and inputstream within a for loop to go and insert each item into the database. I’m just not sure how to parse each line to give each item a name and a type to be inserted into the database, and how to format a text file to be read.
Any help would be greatly appreciated as I have found very little about using external files to import data into an sql database that pertains to my situation.
In my apps that have a static or default database I use an xml file for my data sort of like this:
default_grocerystore.xml stored in assets folder:
Then in the
onCreatein my database class I create my database tables I populate them with the xml like this: