Problem in database

by pramod.deore » Fri, 12 Nov 2010 18:21:12 GMT


Sponsored Links
 Hi all,

         I have a table which has four columns (roomid primary key,
roomaname, switchid, and switchname). Now if I tried to insert a
record in this table then if roomid is not exist in the table then
record is added successfully. (And it is obvious ).

        But what I want is if suppose primary key is exist already and
I tried to insert the record then last two columns values (i.e
switchid and switchname) is added to that record.

        I think it is not possible. But still want to take experts
opinion.

Thanks

--



Problem in database

by Kostya Vasilyev » Fri, 12 Nov 2010 18:38:03 GMT


 If a record (row) already exists, you need to perform an update, not insert.

Before:

roomid: 1001
roomname: Scheherezade's bedroom
switchid, switchname: null

Update:

UPDATE tablename
SET switchid = 512, switchname = 'blahblah'
WHERE roomid = 1001

After:

roomid: 1001
roomname: Scheherezade's bedroom
switchid: 512
switchname: blahblah

You can use SQLiteDatbase.update for this:

 http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html #update(java.lang.String, 
android.content.ContentValues, java.lang.String, java.lang.String[])


-- Kostya

12.11.2010 13:21, pramod.deore :
Hi all, I have a table which has four columns (roomid primary key, roomaname, switchid, and switchname). Now if I tried to insert a record in this table then if roomid is not exist in the table then record is added successfully. (And it is obvious ). But what I want is if suppose primary key is exist already and I tried to insert the record then last two columns values (i.e switchid and switchname) is added to that record. I think it is not possible. But still want to take experts opinion. Thanks
-- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --


Sponsored Links


Problem in database

by pramod.deore » Fri, 12 Nov 2010 18:52:52 GMT


 Hi, Kostya Thanks. But I think it only override older value. what I
want is

If suppose I have
roomid:101
roomName:Hall
switchid:202
switchName:AC

Now if suppose I insert a record as(101,Hall,203, Light)

then record is added as
roomid:101
roomName:Hall
switchid:202,203
switchName:AC,Light.

I hope you understand what I am trying to say.






--



Problem in database

by Kostya Vasilyev » Fri, 12 Nov 2010 19:06:55 GMT


 Ah.

That's done with a "one to many relationship", where you have two tables 
linked by a "foreign key".


This is a fundamental topic with SQL databases, Google should find 
plenty of articles.


For example, this:

 http://www.databaseprimer.com/relationship_1tox.html 

Or this:

 http://www.sqlite.org/foreignkeys.html 

-- Kostya

12.11.2010 13:52, pramod.deore :
Hi, Kostya Thanks. But I think it only override older value. what I want is If suppose I have roomid:101 roomName:Hall switchid:202 switchName:AC Now if suppose I insert a record as(101,Hall,203, Light) then record is added as roomid:101 roomName:Hall switchid:202,203 switchName:AC,Light. I hope you understand what I am trying to say.
If a record (row) already exists, you need to perform an update, not insert. Before: roomid: 1001 roomname: Scheherezade's bedroom switchid, switchname: null Update: UPDATE tablename SET switchid = 512, switchname = 'blahblah' WHERE roomid = 1001 After: roomid: 1001 roomname: Scheherezade's bedroom switchid: 512 switchname: blahblah You can use SQLiteDatbase.update for this: http://developer.android.com/reference/android/database/sqlite/SQLite... , android.content.ContentValues, java.lang.String, java.lang.String[]) -- Kostya 12.11.2010 13:21, pramod.deore :
Hi all, I have a table which has four columns (roomid primary key, roomaname, switchid, and switchname). Now if I tried to insert a record in this table then if roomid is not exist in the table then record is added successfully. (And it is obvious ). But what I want is if suppose primary key is exist already and I tried to insert the record then last two columns values (i.e switchid and switchname) is added to that record. I think it is not possible. But still want to take experts opinion. Thanks
-- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
-- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --



Problem in database

by Daniel Drozdzewski » Fri, 12 Nov 2010 19:08:17 GMT


 



Pramod,

to achieve the above you will need to change your data model.
It is obvious that in your case each room can have multiple switches.
By storing this data in the ROOM table, you are breaking few rules.(*)

Create separate table for switches and additional table that maps from
roomid -> switchid
Then to "add" extra switches to a room, you either insert new switch
into switch table or just search for switchid, if added switch already
exists in your data and then add it to this table that achieves 1 to
many mapping between roomid and switchid.

You have to have a look at data modelling and '1 to many'
relationships between entities.

(*) we are talking data normalisation and various forms of this; By
normalising your data, you will improve consistency and reduce
duplication, but retrieval will cost you more time, since it will rely
on JOIN operations. This is not too optimised in SQLite... You will
have to make few judgement calls, but it will be dependant on your
particular problem.

-- 
Daniel Drozdzewski

--



Problem in database

by pramod.deore » Fri, 12 Nov 2010 19:16:04 GMT


 Thanks Kostya for useful Links and Daniel for great explanation.

On Nov 12, 4:08pm, Daniel Drozdzewski <daniel.drozdzew...@gmail.com>




--



Other Threads

1. Handling Camera

Hi

 am using the following code to get the preview for my camera , but my
app getting force closed, dont know y, can anyone help me on this.


setContentView(R.layout.camera);
          final Preview preview = new
Preview(getApplicationContext());
            FrameLayout layout = (FrameLayout)
findViewById(R.id.preview);
            try{

                if(layout!=null &&  preview!=null){
                    layout.addView(preview);   /* Here where my app
getting force closed */
                }else{
                    showToast("its null");
                }
            }catch(Exception e){
                showToast(e.getMessage());

            }


Thanks and Regards



 Raju .G

-- 

2. hardware update wizard doesn't start when I plug in the phone with the USB cable

I'm experimenting with android development on my work XP laptop.  I
installed the SDK and the ADT in my Eclipse 3.5.2 environment.  I then
connected my device to my laptop with the USB cable.  Although my
computer appeared to recognize that a device had been connected, it
didn't bring up the "Hardware Update Wizard".  The installation
instructions for the USB driver don't mention this scenario.

When I open up "Device Manager" in "Computer Management" I don't see
any hint about how I can make this happen.

I then opened up Explorer and I noticed a new drive with the name
"Motorola".  When I visited that, I noticed the following files:

Autorun.inf
MotoConnect_MSI_Installation_01.01.30.msi
Motorola_End_User_Driver_Installation_4.7.1.msi
Motorola_End_User_Driver_Installation_x64_4.7.1.msi
config.ini
ins.vbs
run.bat*
setup.exe*

When I ran "setup.exe", it brought up a dialog titled "Motorola Mobile
Drivers Installation 4.7.1".  It sounds like this is similar to what
the Android setup instructions describe, but not quite the same.

How do I install the Android USB driver if the "Hardware Update
Wizard" doesn't start?

-- 

3. Camera application

4. Problem with <TabHost> not being the root of the layout

5. Making an Flash Application or Browser Based Application non exitable on Android Tablet PC

6. Twitter Integration with Android app

7. Why my apps can't be searched out by some phone model?