This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

SQL Example


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mitchell
~lick~
Join Date: Mar 2010
Old 01-01-2012 , 16:22   SQL Example
Reply With Quote #1

Ive been searching for a while now, and this is the only thing bothering me wondering how to work it, SQL im kindly asking some one to post a little plugin the saves and gets a value from a local SQL database so users could learn from it. Im a better learner when i have an actual code snippet to work off of, and right now i cant find a simple plugin to do that with out getting a lot of values saved etc. Im not asking for the sourcemod link to SQL tutorial, i have already looked at it a dozen times and still seems foreign for me.
Mitchell is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 01-01-2012 , 21:44   Re: SQL Example
Reply With Quote #2

Actually it's not that hard, but there are many ways to do it depending on what you want to do.
How many rows you have, how many fields you have, is it numerical, should the database created or does it exist, et cetera.
If you have an example which is more near to what you want to achieve it should be better huh?
Also i mostly use my DBhandle globally and never close it, most ppl create it only by needing AFAIK.

Yours sincerely
Impact
__________________

Last edited by Impact123; 01-01-2012 at 21:45.
Impact123 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-02-2012 , 00:18   Re: SQL Example
Reply With Quote #3

Thanks for the information impact, maybe i should say as an example as like storing... lets say a time like: GameTime();, or maybe even better a player's played time and storing it, and getting it when ever a command is called? I dont know if any body would like to help me out with that would they? Learning purposes only of course.
Mitchell is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 01-02-2012 , 02:31   Re: SQL Example
Reply With Quote #4

Okay, i wrote up this short example, i am 100% sure it is not the best way to do this, im not so much an user of SQL.
I've commented every second line, so you should have no problem understand it.
I have learned from: Last connect, CSS Bank, SoD Stats, and a few more i can't remember.

I have an older example with player rounds from where i began scripting SourcePawn, i can send it to you privately, you'll see the reason ;).

Yours sincerely
Impact
Attached Files
File Type: sp Get Plugin or Get Source (SQL_Example.sp - 1866 views - 3.0 KB)
__________________

Last edited by Impact123; 01-02-2012 at 02:44.
Impact123 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-02-2012 , 10:21   Re: SQL Example
Reply With Quote #5

Thanks man! I can't wait to look at it!

actually i would love to see your other round script, thanks!

Last edited by Mitchell; 01-02-2012 at 10:23.
Mitchell is offline
lokizito
Veteran Member
Join Date: Dec 2010
Location: Brazil
Old 01-03-2012 , 08:20   Re: SQL Example
Reply With Quote #6

The only thing missing in Impact's example is that whenever you get a value from a player, (from cmds args, name, etc), you should always use SQL_EscapeString to avoid SQL injection.
__________________
[CS:S] RankMe - Why pay for a rank plugin if you can have one for free?
[CS:S] RankMe Connect Announcer - Announce when a player connect
Translation Project - Let's make the plugins to be international
lokizito is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 01-03-2012 , 11:20   Re: SQL Example
Reply With Quote #7

Quote:
Originally Posted by lokizito View Post
The only thing missing in Impact's example is that whenever you get a value from a player, (from cmds args, name, etc), you should always use SQL_EscapeString to avoid SQL injection.
What do you mean?
Mitchell is offline
lokizito
Veteran Member
Join Date: Dec 2010
Location: Brazil
Old 01-03-2012 , 17:05   Re: SQL Example
Reply With Quote #8

For example, in this query:
Code:
Format(Query, sizeof(Query), "INSERT OR IGNORE INTO players VALUES ('%s', '%s')", Id, Name);
If, the Name string was from GetClientName, or GetCmdArg, it could contain like '. It could break your query and be used for making another queries. For example if the player name was something like
Code:
sometext'); DELETE FROM players WHERE 1=1
Your query would be executed, but also, it would delete all the records from your table.
Did i made it clear enough (not very good at explanations)?

EDIT: Using SQL_EscapeString make sure that your query won't be changed by any character that would do it, by escaping then on the database, like ' becomes \' and so.
__________________
[CS:S] RankMe - Why pay for a rank plugin if you can have one for free?
[CS:S] RankMe Connect Announcer - Announce when a player connect
Translation Project - Let's make the plugins to be international

Last edited by lokizito; 01-03-2012 at 18:43.
lokizito is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 01-03-2012 , 17:41   Re: SQL Example
Reply With Quote #9

Quote:
Originally Posted by lokizito View Post
For example, in this query:
Code:
Format(Query, sizeof(Query), "INSERT OR IGNORE INTO players VALUES ('%s', '%s')", Id, Name);
If, the Name string was from GetClientName, or GetCmdArg, it could contain like '. It could break your query and be used for making another queries. For example if the player name was something like
Code:
sometext'); DELETE FROM players WHERE 1=1
Your query would be executed, but also, it would delete all the records from your table.
Did i made it clear enough (not very good at explanations)?
^^ He's talking about SQL injection. You always need to protect against injection. More info: http://en.wikipedia.org/wiki/SQL_injection
__________________
Dr. McKay is offline
flamingkirby
Junior Member
Join Date: Nov 2013
Old 11-27-2013 , 19:11   Re: SQL Example
Reply With Quote #10

Quote:
Originally Posted by Impact123 View Post
Okay, i wrote up this short example, i am 100% sure it is not the best way to do this, im not so much an user of SQL.
I've commented every second line, so you should have no problem understand it.
I have learned from: Last connect, CSS Bank, SoD Stats, and a few more i can't remember.

I have an older example with player rounds from where i began scripting SourcePawn, i can send it to you privately, you'll see the reason ;).

Yours sincerely
Impact
Wow. Thanks for this simple example even though it was not meant for me. Was trying to find out how to do this too as i find the wiki page useless to some extent =/.
__________________
flamingkirby is offline
Reply



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 16:25.


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