1

Problem Description

I have SQLite database in which I have keep two tables, In first Table Companies I keep company ID which is unique for that company, Name of company, Websites and Emails. Every company can have several addresses, so I create second Table Addresses and keep there company ID which is not unique in this case and other information.

For Example:

If in Companies table I have record like this

7785413 MyComp http://www.mycomp.com [email protected]

In the Addresses table I can have records like this

7785413 0 Address1 +64841518549 +9985212848

7785413 1 Address2 +64841542359 +9985212848

As there is no unique columns in my second Table, I want to know how I can update records? In first case I call

database.insertOrThrow(DATABASE_TABLE_NAME, null, values);

function and if my IDs are same function throws an exception I catch it and update record. I can't do same thing in second Table as there is no unique columns and function will not throw an exception. Which is the best way to do that ?

Tables

CREATE TABLE Companies (ID TEXT UNIQUE, Name TEXT, Websites TEXT, Emails TEXT)

CREATE TABLE Addresses (ID TEXT, Position NUMERIC, Address TEXT, Tel TEXT, Mob TEXT)

5
  • Can you not create a foreign key? Commented Apr 10, 2013 at 18:15
  • sorry ? what does you mean saying foreign ? Commented Apr 10, 2013 at 18:16
  • Simplistically, its a PK which extends over to a second table. en.wikipedia.org/wiki/Foreign_key Commented Apr 10, 2013 at 18:18
  • sorry as I am not native with sqlite can you please bring an example of how I can do that for my case ? Commented Apr 10, 2013 at 18:20
  • It is not a SQLite concept, but a vital part of relational databases Commented Apr 10, 2013 at 18:23

1 Answer 1

3

You need to specify the primary key as a constraint on table creation.

CREATE TABLE Addresses(ID TEXT, Position NUMERIC, Address TEXT, Tel TEXT, Mob TEXT, PRIMARY KEY (ID , Position ));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.