Jump to content

General Help


Recommended Posts

Hey MTASA Contributors. I do not need help with my script yet, as i am making some preparation. There are some things i wish to know. The only way to learn what i need to know is to ask you guys.

In some scripts i see people using ~=, ect. I do not understand what these mean. Is it possible someone can explain what each one does. Except =. I need one which will be like contains. So my script will be a swearword filter. U can add words to the filter. More features may be added. But lets say they say "I hate all u fuckers". I cannot use =. Is there one which would say Contains... and so on, is it ~=?

I want to use sqllite. Could u provide a small and basic example of SQLLite. I am a fast learner :D.

Thank you all for your time,

Chris Smith

* I've been struggling with ideas for scripting!*

Link to comment
SQLITE it's similar to MySQL, but doesn't require a SQL server like MySQL does.

I will explain the new db functions.

dbConnect 

This function opens a connection to a database and returns an element that can be used with dbQuery. To disconnect use destroyElement.

So you must use dbConnect, to open a connection with the database (the .db file) which you can later use with dbQuery to execute a query.

dbConnect connects to a .db file, or create it if it doesn't exist.

The path can be the resource, another resource or the global databases folder.

Ex:

--In the same resource 
local connection = dbConnect ( "sqlite" , "newDB.db" ) 
  
--Another resource 
local connection = dbConnect ( "sqlite" , ":resourceName/newDB.db" ) 
  
--Global databases folder 
local connection = dbConnect ( "sqlite" , ":/newDB.db" ) 

And later you will use the variable connection with dbQuery.

You can destroy the connection with destroyElement.

dbQuery 

This function starts a database query using the supplied connection. Use the returned query handle with dbPoll to get the result, or dbFree if you don't want the result.

You use dbQuery to execute a query and it returns a query handler which can be used with dbPoll to get the result, or dbFree if you don't want it at all.

You can also use

dbExec 

If you want to execute a query and you don't want a result.

Ex:

The example is a continue to the above example.

local queryHandle = dbQuery ( connection , "SELECT * FROM someTable" ) 
  
--Another example 
local queryHandle = dbQuery ( connection , "CREATE TABLE IF NOT EXISTS someTable (column1 TEXT,column2 TEXT)" ) 
  
--Another example 
local queryHandle = dbQuery ( connection , "INSERT INTO someTable (column1,column2) VALUES (?,?)", "text" , "text2" ) 

The question mark can be used to insert a value as an argument, without having to quote it or anything, I use them personally.

If you don't know what to type in the query, look in the old sqlite functions wiki page for examples.

dbPoll 

This function checks the progress of a database query.

table: Returns a table when the results are ready. This automatically frees the query handle, so you do not have to call dbFree.

When you use dbQuery it returns a query handle, in order to get the result you have to use dbPoll.

Ex:

--Without timeout setting. 
local queryResult = dbPoll ( queryHandle , -1 ) 
  
--With timeout setting. 
local queryResult = dbPoll ( queryHandle , 10 ) 

The second argument:

How many milliseconds to wait for a result. Use 0 for and instant response (which may return nil). Use -1 to wait until a result is ready. Note: A wait here will freeze the entire server just like the executeSQL* functions

dbFree just frees the result no more explaining needed.

You might need this: viewtopic.php?f=91&t=34941&hilit=+sqlite#p362581.

Link to comment
  • 3 years later...

hey i have a question can you see whats wrong with this i get this warning and it dont update the points into table after reconnect

lp2Gynr.png

script

dataName  = { 
    ["anterior"] = "Last Drift", 
    ["total"] = "Total Drift", 
    ["mejor"] = "Best Drift" 
} 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite" , "Driftpoint.db" ) 
        if ( connection ) then 
            dbQuery ( connection , "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 
  
function saveDriftPoints ( ) 
    local account = getPlayerAccount ( source ); -- getting account 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local t = { } -- creating table for drift's data 
    for k, v in pairs ( dataName ) do 
        local data = getElementData ( source, v ); 
        if ( data ) then 
            t[v] = data; -- inserting values to table from ElementData 
        end 
    end 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 
  
function loadDriftPoints ( _, account ) 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if row exist 
    if ( #check > 0 ) then -- if yes 
        local d = check[1]; 
        
        local t = fromJSON ( d["data"] ); 
        if ( type ( t ) == "table" ) then 
            for k, v in pairs ( t ) do 
                setElementData ( source, k, v ); -- setting ElementData from saved drift's table in table 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, loadDriftPoints ); 

this are the score when i quit

DaTD1ac.jpg?1

this are the loaded scores when i join the game and login

DaTD1ac.jpg?1

this are the score when i join and did 1 drift

zgfUFjz.jpg?1

Link to comment

@spoty, did you read what JR10 said?

You should use dbExec if there was no result returned, like creating a table, inserting rows or updating rows.

Use dbQuery when there will be a result, e.g SELECT.

For dbQuery you must use dbPoll if you want the result or dbFree if you don't want the result, not using any of those two functions will show you the warning like the one you getting now.

Link to comment
@spoty, did you read what JR10 said?

You should use dbExec if there was no result returned, like creating a table, inserting rows or updating rows.

Use dbQuery when there will be a result, e.g SELECT.

For dbQuery you must use dbPoll if you want the result or dbFree if you don't want the result, not using any of those two functions will show you the warning like the one you getting now.

can you mayby show me how to use it then i am realy confused right now :S

Link to comment
@spoty, did you read what JR10 said?

You should use dbExec if there was no result returned, like creating a table, inserting rows or updating rows.

Use dbQuery when there will be a result, e.g SELECT.

For dbQuery you must use dbPoll if you want the result or dbFree if you don't want the result, not using any of those two functions will show you the warning like the one you getting now.

can you mayby show me how to use it then i am realy confused right now :S

You can see a few examples on dbQuery

Edited by Guest
Link to comment
  • 4 weeks later...

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...