Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:47:16+00:00 2026-06-05T02:47:16+00:00

I’m quite new to SQLite databases and need help using an external text file

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:47:17+00:00Added an answer on June 5, 2026 at 2:47 am

    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:

    <default_grocerystore>
        <grocery>
                <name>Bread</name>
                <type>Bakery</type>
        </grocery>
        <grocery>
                <name>Rolls</name>
                <type>Bakery</type>
        </grocery>
        <grocery>
                <name>Juice</name>
                <type>Beverages</type>
        </grocery>
        <grocery>
                <name>Soda</name>
                <type>Beverages</type>
        </grocery>
    </default_grocerystore>
    

    Then in the onCreate in my database class I create my database tables I populate them with the xml like this:

    private void populateGrocery(SQLiteDatabase db) {
                        ArrayList<GroceryObj> groceryArrayList;
                        groceryArrayList = buildGroceryArrayList();
                        String insertStmt = null;
                        for (int i = 0; i < groceryArrayList.size(); i++) {
                                insertStmt = "INSERT INTO " + GROCERY_TABLE + " ("
                                                + GROCERY_KEY_NAME + ", " + GROCERY_KEY_TYPE + ") "
                                                + "VALUES (\""
                                                + groceryArrayList.get(i).getName()
                                                + "\", \""+groceryArrayList.get(i).getType()+"\");";
                                db.execSQL(insertStmt);
                        }
    
    private ArrayList<GroceryObj> buildGroceryArrayList() {
                        ArrayList<GroceryObj> aL = new ArrayList<GroceryObj>();
                        DocumentBuilderFactory factory = DocumentBuilderFactory
                                        .newInstance();
                        try {
                                DocumentBuilder builder = factory.newDocumentBuilder();
                                InputStream raw = context.getAssets().open(
                                                "default_grocerys.xml");
                                Document dom = builder.parse(raw);
                                Element root = dom.getDocumentElement();
                                NodeList groceryItems = root.getElementsByTagName("grocery");
                                for (int i = 0; i < groceryItems.getLength(); i++) {
                                        String name= null;
                                        String type= null;
                                        Node item = groceryItems.item(i);
                                        NodeList groceryItem = item.getChildNodes();
                                        for (int j = 0; j < groceryItem.getLength(); j++) {
                                                Node nodeItem= groceryItem.item(j);
                                                String nodeName= noteItem.getNodeName();
                                                if (nodeName.equalsIgnoreCase("name")) {
                                                        name= nodeItem.getFirstChild().getNodeValue();
                                                } else if (nodeName.equalsIgnoreCase("type")) {
                                                        type= nodeItem.getFirstChild().getNodeValue();
                                                }
                                        }
                                        aL.add(new GroceryObj(name, type));
                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        return aL;
                }
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a reasonable size flat file database of text documents mostly saved in
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
Thanks in advance for your help. I have a need within an application to
I am using parse_ini_file to read the contents of a file however it is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.