codeluaeveryday Posted December 16, 2011 Share Posted December 16, 2011 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 . Thank you all for your time, Chris Smith * I've been struggling with ideas for scripting!* Link to comment
bandi94 Posted December 16, 2011 Share Posted December 16, 2011 ex local abc = 0 "=" mean that i add a value for variable abc if(a==b) "==" mean that a is equal whit b if(a~=b) "~=" mean you chek if a is not equal whit b Link to comment
codeluaeveryday Posted December 16, 2011 Author Share Posted December 16, 2011 Nice thanks. I can use ~= with a small idea i had. Thanks bandi. Link to comment
codeluaeveryday Posted December 16, 2011 Author Share Posted December 16, 2011 oh yay, ill have to use string find. I can do that lol. Is anyone gonna explain sqllite? Link to comment
BinSlayer1 Posted December 16, 2011 Share Posted December 16, 2011 Here's a sql course: http://www.sqlcourse.com/index.html As far as I can see you're a beginner in scripting. I strongly recommend not to use sql for storage until you get a better understanding of scripting. I suggest xml for storage: https://wiki.multitheftauto.com/wiki/Xml Take a look at all those functions and their respective examples and try to learn from those Link to comment
JR10 Posted December 16, 2011 Share Posted December 16, 2011 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
spoty Posted April 27, 2015 Share Posted April 27, 2015 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 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 this are the loaded scores when i join the game and login this are the score when i join and did 1 drift Link to comment
TAPL Posted April 27, 2015 Share Posted April 27, 2015 @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 Posted April 27, 2015 Share Posted April 27, 2015 @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 Link to comment
TAPL Posted April 27, 2015 Share Posted April 27, 2015 It's so simply, use dbQuery for SELECT and use dbExec for create table and insert row and update row. Link to comment
Addlibs Posted April 27, 2015 Share Posted April 27, 2015 (edited) @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 You can see a few examples on dbQuery Edited April 27, 2015 by Guest Link to comment
spoty Posted April 27, 2015 Share Posted April 27, 2015 It's so simply, use dbQuery for SELECT and use dbExec for create table and insert row and update row. okay i did it right now but its still recounting the points when i rejoin Link to comment
codeluaeveryday Posted May 23, 2015 Author Share Posted May 23, 2015 This post is 4 years old... I was about to say to used dbExec and I don't even program in MTA anymore... Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now