• Gmod Lua and MySQL
    17 replies, posted
What is the best way to handle MySQL Queries in Gmod? Is mysqloo still a thing? I can find it on Facepunch but the thread is a few years old and not sure if there's other more suitable options.
tmysql: [url]https://facepunch.com/showthread.php?t=1442438[/url]
[QUOTE=ms333;49515663]tmysql: [url]https://facepunch.com/showthread.php?t=1442438[/url][/QUOTE] I've always used MySQL - even though I know it's not favorable for really big projects - but is there any reason why using MySQL in gmod should be avoided especially?
[QUOTE=Semajnad;49515688]I've always used MySQL - even though I know it's not favorable for really big projects - but is there any reason why using MySQL in gmod should be avoided especially?[/QUOTE] No. MySQL is fine and probably one of the best options for most data storage in gmod.
[QUOTE=StonedPenguin;49515731]No. MySQL is fine and probably one of the best options for most data storage in gmod.[/QUOTE] Final question :P I've never used a dll in gmod. Where do I put it and how do I include it? Once I've got the point where it's usable I can use it fine. I'm assuming it goes in bin in the main server directory? Can all add-ons then use it? How do you require/include it?
You put it inside /garrysmod/lua/bin/ (has to be created), then use include('tmysql4') and you're good to go, theres also a wrapper which makes all addons which base on mysqloo, use tmysql4.
[QUOTE=whitestar;49516081]You put it inside /garrysmod/lua/bin/ (has to be created), then use include('tmysql4') and you're good to go, theres also a wrapper which makes all addons which base on mysqloo, use tmysql4.[/QUOTE] I'm pretty sure the wrapper is designed for server owners to not have to use mysqloo. If you're going to develop, [I]please[/I] don't use mysqloo or the wrapper.
[QUOTE=BillyOnWiiU;49516493]I'm pretty sure the wrapper is designed for server owners to not have to use mysqloo. If you're going to develop, [I]please[/I] don't use mysqloo or the wrapper.[/QUOTE] Never said anything about using it in development, 'which makes all addons[B] which base on mysqloo[/B], use tmysql4'
[QUOTE=whitestar;49516516]Never said anything about using it in development, 'which makes all addons[B] which base on mysqloo[/B], use tmysql4'[/QUOTE] Ok, so I have it installed and am doing: [CODE]function SGN.SQLConnect( server, user, password, database, port ) return tmysql.initialize( server, user, password, database, port ) end local db = SGN.SQLConnect( '127.0.0.1', 'root', 'PASSWORD', 'darkrp_logs', 3306 ) db:Connect() db:Query( "INSERT INTO playerconnected ( datetime, player ) VALUES ( nil, 1 )", function( table ) PrintTable(table) end )[/CODE] and I get the error: [CODE] [ERROR] addons/sgn_base/lua/autorun/server/sv_sgn_base.lua:31: Attempted to call Connect on an already connected database 1. Connect - [C]:-1 2. unknown - addons/sgn_base/lua/autorun/server/sv_sgn_base.lua:31[/CODE] I only make the one connection?
tmysql.initialize automatically connects so you don't need to use :Connect() Also reading your first posts it seems you're a little confused between the difference between mysql, mysqloo and tmysql
[QUOTE=Coffeee;49516844]tmysql.initialize automatically connects so you don't need to use :Connect()[/QUOTE] Ah, thanks :) [editline]12th January 2016[/editline] [QUOTE=Coffeee;49516844]tmysql.initialize automatically connects so you don't need to use :Connect() Also reading your first posts it seems you're a little confused between the difference between mysql, mysqloo and tmysql[/QUOTE] Ok another question :) If I have: [CODE]function SGN.SQLQuery( query ) Database:Query( query ) end[/CODE] Can I do something like: [CODE]db:SGN.SQLQuery( query ) [/CODE] and instead do: [CODE]function SGN.SQLQuery( query ) self:Query( query ) end[/CODE]
no, you can't. Just pass the DB to SGN.SQLQuery, or store db on the SGN table so you can do self.db:Query(query) inside the function.
[QUOTE=zerf;49517237]no, you can't. Just pass the DB to SGN.SQLQuery, or store db on the SGN table so you can do self.db:Query(query) inside the function.[/QUOTE] Ahhh, so SGN.Database = CONNECTION How can I then run SGN.Database against my own function without passing it as an argument? I want to be able to pass it neatly and then use 'self' in the function instead of doing SGN.SQLQuery( db, query ).
[QUOTE=Semajnad;49517445]Ahhh, so SGN.Database = CONNECTION How can I then run SGN.Database against my own function without passing it as an argument? I want to be able to pass it neatly and then use 'self' in the function instead of doing SGN.SQLQuery( db, query ).[/QUOTE] [lua] SGN.db = database function SGN:SQLQuery(str) -- note the colon used in the function definition self.db:Query(str) end -- usage SGN:SQLQuery("DROP TABLE players;") -- note the colon used to call the function [/lua]
[QUOTE=zerf;49517870][lua] SGN.db = database function SGN:SQLQuery(str) -- note the colon used in the function definition self.db:Query(str) end -- usage SGN:SQLQuery("DROP TABLE players;") -- note the colon used to call the function [/lua][/QUOTE] Ok, thank you. Personally I think it looks neater doing it that way. I think my trouble here is I don't actually know what the colon is. I know you can use the Meta Tabls and pas objects to a function like ply:FreezeAndBring - best example I could think off. But how does the colon work in this context? Are we basically looking St the SGN table that contains all the functions /variables etc as the object, and then padding it to the functions? Is doing something like above common? Personally I think to try and avoid having the object or thing your working with a a aparameter and just have the data in there. Less repetitive code when using sql query.
[QUOTE=Semajnad;49519782]Ok, thank you. Personally I think it looks neater doing it that way. I think my trouble here is I don't actually know what the colon is. I know you can use the Meta Tabls and pas objects to a function like ply:FreezeAndBring - best example I could think off. But how does the colon work in this context? Are we basically looking St the SGN table that contains all the functions /variables etc as the object, and then padding it to the functions? Is doing something like above common? Personally I think to try and avoid having the object or thing your working with a a aparameter and just have the data in there. Less repetitive code when using sql query.[/QUOTE] I don't know what you mean by padding, but yes, self is equal to SGN in this context. Basically whenever you see self in a function declaration, just look at whatever is before the colon at the beginning of the function. [code]function SGN:SQLQuery(str)[/code] SGN is before the colon, so self is SGN. [code]SGN.table = {} function SGN.table:Function() end[/code] SGN.table is before the colon, so self is SGN.table
[QUOTE=man with hat;49519901]I don't know what you mean by padding, but yes, self is equal to SGN in this context. Basically whenever you see self in a function declaration, just look at whatever is before the colon at the beginning of the function. [code]function SGN:SQLQuery(str)[/code] SGN is before the colon, so self is SGN. [code]SGN.table = {} function SGN.table:Function() end[/code] SGN.table is before the colon, so self is SGN.table[/QUOTE] Sorry, I was my early morning typing on phone :) meant adding.
[QUOTE=Semajnad;49515627]What is the best way to handle MySQL Queries in Gmod? Is mysqloo still a thing? I can find it on Facepunch but the thread is a few years old and not sure if there's other more suitable options.[/QUOTE] Use MysqLite from FPtje, it supports mysqloo, sql and tmysql [url]https://github.com/myrage2000/MySQLite[/url]
Sorry, you need to Log In to post a reply to this thread.