Domanda

I found this answer but I'm not sure it does what I want to. It looks like it updates ALL rows in the table, where I only want to update one.

My table is very simple:

MacAddress           |    Name
1C-6F-65-C6-20-0B    |    Logan-PC
DE-AD-BE-EF-F0-0D    |    PC1
...

MacAddress is the PK and I want to simply be able to update Name if it exists or add a new row if it does not, given the MacAddress as references and a Name string.

This is the first time I'm using SQLite so I may just not be understanding Eric B's answer correctly.

È stato utile?

Soluzione

The answer you posted seems to work well for me assuming your MacAddress is a primary key in your table.

insert or replace into yourtable (macaddress, name)
values ('1C-6F-65-C6-20-0B', 'Logan-PC');

insert or replace into yourtable (macaddress, name)
values ('1C-6F-65-C6-20-0B', 'Logan-PC-Updated');

SQL Fiddle Demo

This will result in a single row, with Logan-PC-Updated as the updated name.

NOTE: If your MacAddress is not your primary key, then this method will not work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top