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

  • SEARCH
  • Home
  • 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 6606459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:23:53+00:00 2026-05-25T19:23:53+00:00

For a simple android app I’m creating as a teaching tool for myself (for

  • 0

For a simple android app I’m creating as a teaching tool for myself (for using relational dbs/SQL among other things – pardon the simplicity of the question if you will). I’m pre-creating a sqlite db to ship with the application. I’m doing this based on the following SO question.

I’ve got two tables with a many to many relationship and a junction table to define those relationships as follows:

CREATE TABLE Names (_id INTEGER PRIMARY KEY,
                    name TEXT
                   );

CREATE TABLE Categories (_id INTEGER PRIMARY KEY,
                         category TEXT
                        );

CREATE TABLE Name_Category (name_id INTEGER,
                            category_id INTEGER,
                            PRIMARY KEY (name_id, category_id),
                            foreign key (name_id) references Names(_id),
                            foreign key (category_id) references Categories(_id) 
                           );

I’ve got sets of insert statements to fill the Names and Categories tables. I’m now faced with the task of filling the junction table. I’m sure that I could create the insert statements by hand by looking up the ids of the names and categories that I want to match, but that seems a bit silly.

In order to automatically create the insert statements for the junction table, I imagine that I could create a script based on a set of name and category pairs that will search for the appropriate ids and dump an insert statement. (I came up with this as I was asking the question and will research it. Don’t you love it when that happens?)

Does anybody have any suggestions for ways to do this?

EDIT I added the foreign keys because, as pointed out below, they’ll help maintain integrity between the tables.

EDIT #2 To solve this, I created a simple Perl script that would take a text file with name – category pairs and dump them out into another file with the appropriate SQL statements.

The name – category text file has a format as follows:

'Name' 'Category'

The Perl script looks like this:

use strict;
use warnings;

open (my $name_category_pair_file, "<", "name_category.txt") or die "Can't open name_category.txt: $!";
open (my $output_sql_file, ">", "load_name_category_junction_table.sqlite") or die "Can't open load_name_category_junction_table.sqlite: $!";

while (<$name_category_pair_file>) {
    if (/('[a-zA-Z ]*') ('[a-zA-Z ]*')/) {
        my $sql_statement = "INSERT INTO Name_Category VALUES (
                             (SELECT _id FROM Names WHERE name = $1),
                             (SELECT _id FROM Categories WHERE category = $2))\;\n\n";

        print $output_sql_file $sql_statement;
    }
}

close $name_category_pair_file or die "$name_category_pair_file: $!";
close $output_sql_file or die "$output_sql_file: $!";
  • 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-05-25T19:23:54+00:00Added an answer on May 25, 2026 at 7:23 pm

    You can use this insert in your script or code (replacing the strings or using ?):

    insert into Name_Category values(
       (select _id from Categories where category='CAT1'),
       (select _id from Names where name='NAME1'));
    

    Also, you can alter the Name_Category table to constraint on the values that can be inserted and/or deleted:

    CREATE TABLE Name_Category ( name_id INTEGER NOT NULL,
       category_id INTEGER NOT NULL,
       PRIMARY KEY (name_id, category_id),
       foreign key (name_id) references Names(_id),
       foreign key (category_id) references Categories(_id));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an android app that among other things also posts a
Hi im trying to create a simple android app using the Eclipse IDE, all
I'm playing with a simple android app using the emmulator running android-7 (2.1) and
I am creating just a simple android app which will respond to received. I
I'm creating a simple Android App, it will be a map with a number
I have a simple Android App using simple js, jquery mobile and phonegap that
I am creating a simple android app to view a comic book. The pages
I have created a small and simple android app. I tried installing it on
I have a simple question. I am trying to design a simple Android app,
I would like to implement the following simple pattern in an android app: Activity

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.