SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 06/06/2019, 08:58 PM   #1
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 19
Reputation: 61
Default MySQL Prepared Statements

Introduction
Hi, I'll keep it short and simple.
This emulates prepared statement when using the MySQL plugin.
Thank you for reading this really long introduction.

Download: Click me
Important: sampctl required! check github repository for installation instructions.

Reading Data (using inline)
Code:
new stmt_readloop = MySQL_PrepareStatement(MySQLHandle, "SELECT * FROM spawns");

// Run Threaded on statement
inline OnSpawnsLoad() {
    new
    spawnID,
    Float:spawnX,
    Float:spawnY,
    Float:spawnZ,
    Float:spawnA;

    MySQL_BindResultInt(stmt_readloop, 0, spawnID);
    MySQL_BindResultFloat(stmt_readloop, 1, spawnX);
    MySQL_BindResultFloat(stmt_readloop, 2, spawnY);
    MySQL_BindResultFloat(stmt_readloop, 3, spawnZ);
    MySQL_BindResultFloat(stmt_readloop, 4, spawnA);

    while(MySQL_Statement_FetchRow(stmt_readloop)) {
        printf("%i, %.3f, %.3f, %.3f", spawnID, spawnX, spawnY, spawnZ, spawnA);
    }
    MySQL_StatementClose(stmt_readloop);
}
MySQL_ExecuteThreaded_Inline(stmt_readloop, using inline OnSpawnsLoad);
Writing Data
Code:
new Statement: stmt_insert = MySQL_PrepareStatement(MySQLHandle, "INSERT INTO accounts(username, password, salt, money, kills, deaths) VALUES (?,?,?,?,?,?) " );

// Arrow values in questions (first 0, second is 1, etc ...)
MySQL_Bind(stmt_insert, 0 , "patrickgtr");
MySQL_Bind(stmt_insert, 1 , "patrickgtrpassword");
MySQL_Bind(stmt_insert, 2 , "pgtrhash");
MySQL_BindInt(stmt_insert, 3, 100);
MySQL_BindInt(stmt_insert, 4, 200);
MySQL_BindInt(stmt_insert, 5, 300);

MySQL_ExecuteParallel(stmt_insert);
MySQL_StatementClose(stmt_insert);
Functions

Open/Close
Code:
MySQL_StatementClose(Statement:statement)
MySQL_PrepareStatement(MySQL:handle, const query[])
MySQL_Statement_RowsLeft(&Statement:statement)
MySQL_Statement_FetchRow(Statement:statement)
Writing
Code:
MySQL_Bind(Statement:statement, param, const str[]) 
MySQL_BindInt(Statement:statement, param, value)
MySQL_BindFloat(Statement:statement, param, Float:value)
Reading
Code:
MySQL_BindResult(Statement:statement, field, const result[], len = sizeof(result))
MySQL_BindResultInt(Statement:statement, field, &result)
MySQL_BindResultFloat(Statement:statement, field, &Float:result)
Executing
Code:
MySQL_ExecuteThreaded(Statement:statement, const callback[] = "", const fmat[] = "", {Float,_}:...)
MySQL_ExecuteParallel(Statement:statement, const callback[] = "", const fmat[] = "", {Float,_}:...)
MySQL_ExecuteThreaded_Inline(Statement:statement, Func:callback<>)
MySQL_ExecuteParallel_Inline(Statement:statement, Func:callback<>)
PatrickGTR is offline   Reply With Quote
Old 06/06/2019, 10:28 PM   #2
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 923
Reputation: 89
Default Re: MySQL Prepared Statements

Ah something like SQLite, nice job.
Symon is offline   Reply With Quote
Old 07/06/2019, 11:38 PM   #3
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 19
Reputation: 61
Default Re: MySQL Prepared Statements

Quote:
Originally Posted by KeyOfKey View Post
Ah something like SQLite, nice job.
SQLite Improved yes, it's the core.

Added account example script.

https://github.com/PatrickGTR/MySQL-...le/account.pwn
PatrickGTR is offline   Reply With Quote
Old 08/06/2019, 06:31 AM   #4
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 618
Reputation: 57
Default Re: MySQL Prepared Statements

Good one bro, it will be useful for newbie. +5 star!
SiaReyes is offline   Reply With Quote
Old 07/04/2020, 04:06 PM   #5
PatrickGTR
Little Clucker
 
PatrickGTR's Avatar
 
Join Date: Sep 2010
Location: United Kingdom
Posts: 19
Reputation: 61
Default Re: MySQL Prepared Statements

Library updated.

- DEBUG renamed to MYSQL_PREPARE_DEBUG for unique naming & avoid conflict with other script.
- SET debug to false by default.

Let me know what you think, the library is pretty stable and neatens up your code! Take a look at this for example, https://gist.github.com/PatrickGTR/f...e5e37585704a89.
PatrickGTR is offline   Reply With Quote
Old 03/07/2020, 01:46 PM   #6
XoMoX
Big Clucker
 
Join Date: Nov 2018
Posts: 51
Reputation: 0
Default Re: MySQL Prepared Statements

Simple with style i like it very much although i get confused about it sometimes but just follow the instructions and other stuffs LMAO +1
__________________
𝓧𝓸𝓶𝓸𝓧.𝓧𝓸𝓶𝓸𝓧.𝓧𝓸𝓶𝓸𝓧
MY WORKS

Vehicle /lock /unlock and /givekeys system:https://forum.sa-mp.com/showthread.php?p=4152748#post4152748
XoMoX is offline   Reply With Quote
Old 06/07/2020, 09:45 AM   #7
GameOvr
Huge Clucker
 
GameOvr's Avatar
 
Join Date: Jun 2018
Posts: 296
Reputation: 9
Default Re: MySQL Prepared Statements

Here we go... mysql stmts in samp, gr8 work dude!!
__________________
GameOvr is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
If Statements MichiCZ Scripting Help 4 10/04/2019 01:24 PM
BlueG's MySQL - Nested select statements causes crash denNorske Scripting Help 3 07/01/2017 04:29 PM
[Include] MySQL Prepared Stataments Dayvison_ Lançamentos/Releases 9 30/03/2016 04:43 AM
Un Prepared Servers... Cole_William General 22 20/05/2014 09:35 AM
for statements. TaLhA XIV Scripting Help 7 06/09/2012 09:14 AM


All times are GMT. The time now is 07:10 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.