ExecuteSQLQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Undo revision 66549 by Abdo Nour (talk))
Tag: Undo
Line 11: Line 11:


===Required Arguments===
===Required Arguments===
*'''query:''' استعلامات SQL. يتم وضع علامة "?" علي المواضع التي سوف يتم ادراج قيم المعلومات فيها.
*'''query:''' An SQL query. Positions where parameter values will be inserted are marked with a "?".


===Optional Arguments===
===Optional Arguments===

Revision as of 21:33, 15 May 2020

This function executes an arbitrary SQL query and returns the result rows if there are any. It allows parameter binding for security (SQL injection is rendered impossible).

[[{{{image}}}|link=|]] Note: This function only acts upon registry.db. Use dbQuery to query a custom SQL database.

Syntax

table executeSQLQuery ( string query [, var param1 [, var param2 ... ] ] )

Required Arguments

  • query: An SQL query. Positions where parameter values will be inserted are marked with a "?".

Optional Arguments

  • paramX: A variable number of parameters. These must be strings or numbers - it is important to make sure they are of the correct type. Also, the number of parameters passed must be equal to the number of "?" characters in the query string.

String parameters are automatically escaped by adding a backslash (\) before ' and \ characters.

Returns

Returns a table with the result of the query if it was a SELECT query, or false if otherwise. In case of a SELECT query the result table may be empty (if there are no result rows). The table is of the form:

{
    { colname1=value1, colname2=value2, ... },
    { colname1=value3, colname2=value4, ... },
    ...
}

A subsequent table represents the next row.

Example

Below are examples of equivalents for the deprecated executeSQL functions. Note that ` (backtick) can optionally be used to surround table and row names. It usually a good idea to do this to avoid name clashes with SQL reserved words.

مثال لعمل جدول جديد:

executeSQLQuery("CREATE TABLE IF NOT EXISTS players (clothes_head_texture TEXT, clothes_head_model TEXT, name TEXT)") -- اذا لا يوجد جدول بنفس الإسم يصنع جدول بإسم players ويضع كولمن بداخل الجدول
executeSQLQuery("CREATE TABLE IF NOT EXISTS `players` (`clothes_head_texture` TEXT, `clothes_head_model` TEXT, `name` TEXT)") -- اذا لا يوجد جدول بنفس الإسم يصنع جدول بإسم `players` ويضع كولمن بداخل الجدول

مثال لحذف رو من الجدول:

playerName = getPlayerName(thePlayer) -- إحضار اسم اللاعب
executeSQLQuery("DELETE FROM players WHERE name=?", playerName) -- حذف الرو الخاص باللاعب المعرف بالكولمن name من خلال اسمه
executeSQLQuery("DELETE FROM `players` WHERE `name`=?", playerName) -- حذف الرو الخاص اللاعب المعرف بالكولمن `name` من خلال اسمه

مثال لحذف الجدول بالكامل:

executeSQLQuery("DROP TABLE players" ) -- حذف الجدول المسمي بـ players
executeSQLQuery("DROP TABLE `players`" ) -- حذف الجدول المسمي بـ `players`

Example equivalents for executeSQLSelect:

playerName = getPlayerName(thePlayer) -- إحضار اسم اللاعب
executeSQLQuery("SELECT score,health FROM players WHERE name=?", playerName ) -- الحصول علي المعلومات المخزنة التي تحتوي علي نقاط ودم اللاعب من خلال اسمه المعرف بجدول players وبكولمن name
executeSQLQuery("SELECT `score`,`health` FROM `players` WHERE `name`=?", playerName ) -- الحصول علي المعلومات المخزنة التي تحتوي علي نقاط ودم اللاعب من خلال اسمه المعرف بجدول `players` وبكولمن `name`

مثال لوضع رو جديد بالكولمن:

playerName = getPlayerName(thePlayer) -- احضار اسم اللاعب
colorName = "Blue" -- لون الإسم
soundName = "sound.mp3" -- الصوت الخاص بالإسم
executeSQLQuery("INSERT INTO players(name,color,sound) VALUES(?,?,?)", playerName, colorName, soundName ) -- اختيار الجدول players ووضع رو الاسم بكولمن name ووضع رو اللون بكولمن color ووضع رو الصوت بكولمن sound.
executeSQLQuery("INSERT INTO `players`(`name`,`color`,`sound`) VALUES(?,?,?)", playerName, colorName, soundName ) -- اختيار الجدول `players` ووضع رو الاسم بكولمن `name` ووضع رو اللون بكولمن `color` ووضع رو الصوت بكولمن `sound`.

مثال لتحديث معلومات رو معين بالكولمن:

playerName = getPlayerName(thePlayer) -- اسم اللاعب
colorName = "Blue" -- لون الاسم
soundName = "sound.mp3" -- الصوت الخاص بالاسم
executeSQLQuery("UPDATE players SET color='green',sound='somehead' WHERE name=?", playerName ) -- تحديث الرو الخاص بإسم اللاعب وتعريف المعلومات عن طريق كتابة التعريف بجانب كل كولمن ليتم حفظه في رو اللاعب
executeSQLQuery("UPDATE players SET color=?,sound=? WHERE name=?", colorName, soundName, playerName ) -- تحديث الرو الخاص بإسم اللاعب وتعريف المعلومات عن طريق المعلومات التي جلبناها من قبل فوق
executeSQLQuery("UPDATE `players` SET `color`=?,`sound`=? WHERE `name`=?", colorName, soundName, playerName ) -- تحديث الرو الخاص بإسم اللاعب ولكن بجدول وكولمن مختلف


هذا المثال يعرض قائمة اللاعبين المسجلين في قاعدة البيانات والذين لدليهم اكثر عدد من المال.

function listPlayersWithMoreMoneyThan(thePlayer, command, amount) -- عمل وظيفة
    local players = executeSQLQuery("SELECT id, name FROM players WHERE money > ?", tonumber(amount)) -- اختيار اسم اللاعب من خلال اكثر عدد من المال المسجل في القاعدة وتعريفها بإسم players ليتم جلبها فيما بعد
    outputConsole("Players with more money than " .. amount .. ":", thePlayer)
    for i, playerdata in ipairs(players) do -- اختيار معلومات اللاعب من القاعدة المعرفة بإسم players
        outputConsole(playerdata.id .. ": " .. playerdata.name, thePlayer) -- اظهار رقم الرو و اسم اللاعب في اف8
    end
end

addCommandHandler("richplayers", listPlayersWithMoreMoneyThan) -- كتابة الامر rechplayers لتشغيل الوظيفة


This example shows the amount of money a certain registered player has.

function showPlayerMoney(thePlayer, command, playerName) -- عمل وظيفة
    local result = executeSQLQuery("SELECT money FROM players WHERE name=?", playerName) -- جلب عدد المال من قاعدة players من خلال الاسم
    if(#result == 0) then -- نتأكد اذا الجدول = 0
        outputConsole("No player named " .. playerName .. " is registered.", thePlayer) -- يرسل ب اف8 للاعب ان اسم اللاعب ليس مسجل
    else -- اذا الجدول لا يساوي 0
        outputConsole("Money amount of player " .. playerName .. " is " .. result[1].money, thePlayer) -- يكتب اسم اللاعب وعدد المال المسجل في القاعده لديه
    end -- انهاء التحقق
end -- انهاء الوظيفة
addCommandHandler("playermoney", showPlayerMoney) -- كتابة الامر playermoney لتشغيل الوظيفة

Notice the lack of single quotes around the "?" in this example, even though it represents a string. executeSQLQuery will see that the playerName variable is a string and take care of the correct execution of the query by itself.

The advantage of using executeSQLQuery is that it is immune to users trying to exploit the query with an SQL injection attack. playerName may contain special characters like ', " or -- that will not influence the query, unlike the older approach where playerName would be concatenated into the query string.

By: [Abdo Nour]

See Also