0

friends,

I am doing an Android project in my company, still some small work is remaining, I need your help to complete the task.

The problem is...

I have created two tables in which, table1 has an empty column, for purpose for saving name...

The table2 has a list of names, the objective is only the names from this list should be should be saved in the table1's empty column other than that it shouldn't accept any of the name typed manually.

Samuel
  • 9,883
  • 5
  • 45
  • 57
Shivputra
  • 3
  • 3
  • quite unclear question, plz elaborate..... – Richa Sep 02 '11 at 06:34
  • in table2 column, a list of names had been stored, the objective is the table1 should allow the names from table2 to be stored in it. – Shivputra Sep 02 '11 at 07:01
  • you mean you want to copy ur table's column's recods into another table ? – Richa Sep 02 '11 at 07:07
  • nope that table2 is created for autocompletetextview... while saving the names in the table2 should stored in table1 condition is table1 should accept the name from table2. – Shivputra Sep 02 '11 at 07:10

2 Answers2

1

You appear to want to make the list of names a validation: if the user wishes to save a name to table1, the name must already exist in table2.

Typically this would be done as in the following example, in which only the products listed in PRIZEPRODUCTS can be entered into PRIZEWINNERS table: someone could not win a Boat, for example, given the data below:

           PRIZEPRODUCTS
           id
           productname

           1|TV
           2|iPad
           3|backpack



           PRIZEWINNERS
           id
           productid
           winner


           ALTER TABLE PRIZEWINNERS
           ADD CONSTRAINT PRIZEWINNERS_PRIZEPRODUCTS_FK
           FOREIGN KEY(productid) REFERENCES PRIZEPRODUCTS(id)

SQLite doesn't create the foreign key using ALTER TABLE but as part of the create-table statement. See here for the syntax. For enabling foreign key support in Android (2.2), see here.

Now, you can establish the foreign key on the [productname] column if [productname] were the key of PRIZEPRODUCTS. In other words, you could make person-name the key of the table rather than having a PersonID. But if that name is changed in the validation table, it can break the foreign key relationship, unless ON UPDATE CASCADE is enabled, but I am not sure if this is supported in Android.

Community
  • 1
  • 1
Tim
  • 5,371
  • 3
  • 32
  • 41
0

I hope below query will work for you.

insert into table1(name) values (select name from table2 where id=?).

Thanks.

Tyrant
  • 60
  • 1
  • 5