https://www.machinelearningplus.com/...ists-in-mysql/
I was thinking instead of testing to see if the row exists, and if it does not, as in countID is zero do an insert or countID > 0 do an update, do what the article mentions instead.
Question is would it work on a primary autoincrement primary KEY called Id?
Which I suppose you don't normally specify the Id value as it is autoincrementing.
See, I use a table that only ever has 1 row for storing program vars.
I am having to figure out if a row does not exist to do an insert, or if it does exist to do an update to the first row.
or is it just too complex to bother with.
You can use the INSERT INTO … ON DUPLICATE KEY UPDATE statement in MySQL to achieve this.
This statement will insert a new row into the table, or if a duplicate key violation occurs (i.e., a row with the same primary or unique key exists), it will update the existing row with the new values. Here’s the SQL query:
INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES
(1, 'John', 'Doe', 55000.00)
ON DUPLICATE KEY UPDATE
first_name = VALUES(first_name),
last_name = VALUES(last_name),
salary = VALUES(salary);
My sqlquery is written using parameters, like this
insert
update
I was thinking instead of testing to see if the row exists, and if it does not, as in countID is zero do an insert or countID > 0 do an update, do what the article mentions instead.
Question is would it work on a primary autoincrement primary KEY called Id?
Which I suppose you don't normally specify the Id value as it is autoincrementing.
See, I use a table that only ever has 1 row for storing program vars.
I am having to figure out if a row does not exist to do an insert, or if it does exist to do an update to the first row.
or is it just too complex to bother with.
Quote:
You can use the INSERT INTO … ON DUPLICATE KEY UPDATE statement in MySQL to achieve this.
This statement will insert a new row into the table, or if a duplicate key violation occurs (i.e., a row with the same primary or unique key exists), it will update the existing row with the new values. Here’s the SQL query:
INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES
(1, 'John', 'Doe', 55000.00)
ON DUPLICATE KEY UPDATE
first_name = VALUES(first_name),
last_name = VALUES(last_name),
salary = VALUES(salary);
insert
Code:
Sqlquery = "Insert into missextra (WhichBarc, WhichBarcPatron, LoadCombo, BarAN, FUMS, FUMA, DailyFine, WhichCallNumb, Libraryinfo1, Libraryinfo2, rNameFont, rSizeFont, rCharFont, DSIN, SLIMIT, STIME, LibraryName)
VALUES (@WhichBarc, @WhichBarcPatron, @LoadCombo, @BarAN, @FUMS, @FUMA, @DailyFine, @WhichCallNumb, @Libraryinfo1, @Libraryinfo2, @rNameFont, @rSizeFont, @rCharFont, @DSIN, @SLIMIT, @STIME, @LibraryName)"
Code:
Sqlquery = "Update missextra SET WhichBarc='@WhichBarc', WhichBarcPatron='@WhichBarcPatron', LoadCombo='@LoadCombo',
BarAN='@BarAN', FUMS='@FUMS', FUMA='@FUMA', DailyFine='@DailyFine', WhichCallNumb='@WhichCallNumb', Libraryinfo1='@Libraryinfo1', Libraryinfo2='@Libraryinfo2',
rNameFont='@rNameFont', rSizeFont='@rSizeFont', rCharFont='@rCharFont', DSIN='@DSIN', SLIMIT='@SLIMIT', STIME='@STIME',
LibraryName='@LibraryName' Where Id='1'"