Parth Bhoiwala
Parth Bhoiwala

Reputation: 1322

Database tool for Android other than SQLite?

I am new to Android programming and I have made a few simple apps which use SQLite to store user's data. Now I am working on a little more complex app in which I need to implement many-to-many relationship among the tables. database table setup

So basically, I have three layers (3 Tables) that would be connected to each other and I can't find a good tutorial or any documentation on how to do it. I've spent weeks on researching this. I also looked into realm-database but it's complicated for many-to-many table setup.

So is there any easier solution to this for a beginner? Or is there another tool that I can use to accomplish my task. Any suggestions would be helpful. Thank you :)

Upvotes: 1

Views: 128

Answers (4)

alex
alex

Reputation: 6409

Your example isn't a many to many relationship. It's a one to many, each country can only exist in one continent and each state in only one country.

You can get the structure you want by adding a reference to the parent type's ID.

CREATE TABLE continent (
  _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
)

CREATE TABLE country (
  _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  continentId INTEGER NOT NULL,
  name TEXT NOT NULL,
  FOREIGN KEY continentId REFERENCES continent(_id)
)

CREATE TABLE state (
  _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  countryId INTEGER NOT NULL,
  name TEXT NOT NULL,
  FOREIGN KEY countryId REFERENCES country(_id)
)

To select all the countries on a continent, just ask SQL using the correct ID.

SELECT * 
FROM country 
WHERE continentId = ?

Or you can join them together.

SELECT * 
FROM continent 
  JOIN country ON continent._id = country.continent
  JOIN state ON country._id = state.countryId

Upvotes: 3

Code-Apprentice
Code-Apprentice

Reputation: 83527

To connect two tables in a many to many relationship, create a third table with three columns. The first column is just a standard is for the primary key. The other two columns are secondary keys into the two original tables. Googling " many to many relationship" will provide more details.

Upvotes: 0

Ashish Rawat
Ashish Rawat

Reputation: 5829

Yes you can use Ultralite database from SAP. It supports joins as well.

More details here http://scn.sap.com/community/developer-center/mobility-platform/blog/2012/08/23/how-to-open-an-ultralite-db-udb

Upvotes: 0

Justin Conroy
Justin Conroy

Reputation: 376

You can do many-to-may relationships with SQLite. For the example shown you just need some XREF tables. For example (pseudocode):

Table CONTINENT(
     ContinentID 
    ,ContinentName
)

Table COUNTRY(
     CountryID
    ,CountryName
)

Table CONTINENT_COUNTRY_XREF (
     Continent_Country_XrefID
    ,ContinentID
    ,CountryID    
)

Hope this helps.

Upvotes: 2

Related Questions