Jump to content

Database Creation


[XGN]Razor

Recommended Posts

Hello, i want to do an experience system, im watching one based on elementData(this:)

   addEvent ( "onZombieWasted", true ) 

    addEventHandler ( "onZombieWasted", root,

        function ( theKiller )

            triggerClientEvent(theKiller, "gret", getRootElement())

        end

    )

   

function saveAccountData(player, progCount)

    local data = getElementData(player,"lvl")

    setAccountData(getPlayerAccount(player),"lvl",data)

end

addEvent("updateLevel",true)

addEventHandler("updateLevel",getRootElement(),saveAccountData)

 

 

function onPlayerQuitSaveData ()

    saveAccountData(source,"lvl")

end

addEventHandler( "onPlayerQuit", root, onPlayerQuitSaveData)

 

 

function onPlayerLoginLoadData(_, account)

local lvl = getAccountData(account, "lvl")

        setElementData(source,"lvl",lvl)

        triggerClientEvent("setLevelTriggered", source, lvl)

end

addEventHandler("onPlayerLogin", root,onPlayerLoginLoadData)

 

Im really newbie in the sql world, exist any guide or something to learn how to create it and how to use it?

Thanks very much for reading, sorry for bad english.

Link to comment

Creating a database is rather simple all you have to use is dbConnect other SQL functions are

dbConnect 
dbExec 
dbQuery 
dbPoll 
dbFree 

Be sure to look at them before using SQL, here's a tutorial by JR10 about SQLite - > Click Here

In your case, you want to make a database, all you need to do is

connection = dbConnect("sqlite","mydb.db") 
-- Here we have initialized a variable connection which stores our connection with mydb, the database will be created automatically. 

You can also work with MySQl but you will need to set up a server for it. I recommend you SQLite. For checking the database you can use SQlite Browser

Link to comment
Exactly, i dont know how to start the creation.

Okay;

First off, SQL is a programing language and 'MySQL' and 'SQLite' are a database systems,and what you need to know about the different between SQLite and MySQL is only in the system, the language is the same.

So, I suggest you to start with SQLite system of it is storing all data in a file atleast of MySQL you must have to require a SQL server to connect your data AND as far as you're still begginer, SQLite should be easy for you to handle it for now.

Anyway,

So, to start scripting with SQL language in MTA you'll have to read more about these db functions i'll write the link in below:

Note: those functions are SERVER Sided only and SQL language is made to be scripted in Server side

dbFunctions

After reading those functions, Let's learn how to create a database and how to to open a connection with the database.

Our data base would be as a file because we're using SQLite system.

In this case we should use this function to open the connection with the database.

dbConnect

dbConnect will connect to

--Let's name our SQLite file (mydb.sql) now the function dbConnect will be able to open a connection to this file. 
  
local connection = dbConnect ( "sqlite" , "mydb.sql" ) --I did sqlite in the first argument because it's the type of database, second argument is for the database file. 

Now let's check if the connection is succesfully connected with our database.

local connection = dbConnect ( "sqlite" , "mydb.sql" ) 
    if (not connection) then --If not connected then return false 
        outputDebugString("Failed to connect to SQLite file.")   
    else     
        outputDebugString("SQLite file has been connected succesfully.") --else return true. 
    end 

Link to comment
Thanks very much!, one more question :S, how can i send info to the database (for example the lvl and the exp) and get it? i need to use tables?

You've to create a TABLE, like that

dbExec(connection,"CREATE TABLE IF NOT EXISTS tableName(column1 TEXT,column2 INT) 
 

And ABOUT INSERT or UPDATE data into a table; like that

--INSERT Data into a table a new table existing 
dbExec(connection,"INSERT INTO tableName(column1, column2) VALUS (?,?)", name, id) 

--UPDATE Data on a table already has datas and existing 
dbExec(connection,"UPDATE tableName SET column2=? WHERE column1='"..id.."'",name) 

Regards,

-KariM

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...