Raised This Month: $32 Target: $400
 8% 

SQL insert into if not exists


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 15:17   SQL insert into if not exists
Reply With Quote #1

How to insert for example this query but abort if it already exists within database?

Format(sQuery, sizeof(sQuery), "INSERT INTO LastRequest_players (SteamID, wins, Name) VALUES (\"%s\", '%d', \"%N\")", SteamID, LRWins[client], client);
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 08-03-2018 , 15:20   Re: SQL insert into if not exists
Reply With Quote #2

Code:
INSERT IGNORE INTO
maybe you could also need
Code:
INSERT ... ON DUPLICATE KEY UPDATE
https://dev.mysql.com/doc/refman/5.5/en/insert.html
__________________
coding & free software

Last edited by shanapu; 08-03-2018 at 15:22.
shanapu is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 15:24   Re: SQL insert into if not exists
Reply With Quote #3

Quote:
Originally Posted by shanapu View Post
Code:
INSERT IGNORE INTO
maybe you could also need
Code:
INSERT ... ON DUPLICATE KEY UPDATE
https://dev.mysql.com/doc/refman/5.5/en/insert.html
None of them works ( SQLite by the way )
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 08-03-2018 at 15:27.
eyal282 is offline
shanapu
Veteran Member
Join Date: Apr 2015
Location: .de
Old 08-03-2018 , 15:26   Re: SQL insert into if not exists
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
SQLite by the way
Code:
INSERT OR IGNORE
https://www.sqlite.org/lang_insert.html
__________________
coding & free software
shanapu is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 15:29   Re: SQL insert into if not exists
Reply With Quote #5

Quote:
Originally Posted by shanapu View Post
And
Code:
ON CONFLICT DO UPDATE
Thanks, I'll try the first.

Edit: INSERT OR IGNORE still duplicates and ON CONFLICT DO NOTHING ( instead of update ) gives an error.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334

Last edited by eyal282; 08-03-2018 at 15:37.
eyal282 is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-03-2018 , 15:48   Re: SQL insert into if not exists
Reply With Quote #6

PHP Code:
replace into 
EDIT:
Aha sorry din't get the question right.

I've sent something in the other thread :p
https://forums.alliedmods.net/showth...=309603&page=3

Last edited by mug1wara; 08-03-2018 at 15:54.
mug1wara is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-03-2018 , 15:49   Re: SQL insert into if not exists
Reply With Quote #7

Quote:
Originally Posted by mug1wara View Post
PHP Code:
replace into 
I want to ignore an existing column ( I think it's called column ) because it is safer.
__________________
I am available to make plugins for pay.

Discord: Eyal282#1334
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-04-2018 , 03:36   Re: SQL insert into if not exists
Reply With Quote #8

Duplication is based on keys afaik. Show your table structure (its definition) and also what do you consider a "duplicate"? Same SteamID only?
__________________
klippy is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 08-04-2018 , 04:13   Re: SQL insert into if not exists
Reply With Quote #9

Make sure your SteamID column is a unique or primary key.

Then use INSERT INTO .. ON DUPLICATE UPDATE ..

http://www.mysqltutorial.org/mysql-i...te-key-update/
__________________
Neuro Toxin is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 08-04-2018 , 06:50   Re: SQL insert into if not exists
Reply With Quote #10

Hi Klipp.

@eyal, you want to ignore an existing row or column.

Row:
results.RowCount

Column:
results.FetchInt
mug1wara is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:41.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode