papam77 Posted June 10, 2013 Share Posted June 10, 2013 I need to connect toptimes to DB, where's error ? -- -- databasetable_server.lua -- -- A Lua table which is loaded/saved from the sqlite database -- Handled column types are TEXT and REAL -- SDatabaseTable = {} SDatabaseTable.__index = SDatabaseTable SDatabaseTable.instances = {} dbConnect("mysql","dbname=zahrej;host=mysql.mmhost.eu","zahrej","Its Secret") --------------------------------------------------------------------------- -- -- SDatabaseTable:create() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:create(name,columns,columnTypes) local id = #SDatabaseTable.instances + 1 SDatabaseTable.instances[id] = setmetatable( { id = id, name = name, columns = columns, columnTypes = columnTypes, rows = {}, }, self ) SDatabaseTable.instances[id]:postCreate() return SDatabaseTable.instances[id] end --------------------------------------------------------------------------- -- -- SDatabaseTable:destroy() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:destroy() SDatabaseTable.instances[self.id] = nil self.id = 0 ped = nil vehicle = nil end --------------------------------------------------------------------------- -- -- SDatabaseTable:postCreate() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:postCreate() -- Set column types as strings if not set while #self.columnTypes < #self.columns do table.insert( self.columnTypes, 'TEXT' ) end end --------------------------------------------------------------------------- -- -- SDatabaseTable:safestring() -- -- -- --------------------------------------------------------------------------- function safestring( s ) -- escape ' return s:gsub( "(['])", "''" ) end function qsafestring( s ) -- ensure is wrapped in ' return "'" .. safestring(s) .. "'" end function qsafetablename( s ) return qsafestring(s) end --------------------------------------------------------------------------- -- -- SDatabaseTable:load() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:load() for i=1,10 do if self:tryLoad() then return end end end --------------------------------------------------------------------------- -- -- SDatabaseTable:tryLoad() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:tryLoad() outputDebug( 'TOPTIMES', 'SDatabaseTable: Loading ' .. self.name ) self.rows = {} local cmd -- CREATE TABLE self:createTable() -- SELECT -- Build command cmd = 'SELECT * FROM ' .. qsafetablename( self.name ) local sqlResults = executeSQLQuery( cmd ) if not sqlResults then return false end -- Process into rows self.rows = {} for r,sqlRow in ipairs(sqlResults) do local row = {} for c,column in ipairs(self.columns) do row[column] = sqlRow[column] end table.insert( self.rows, row ) end -- Make copy to detect changes self.rowsCopy = table.deepcopy(self.rows) return true end --------------------------------------------------------------------------- -- -- SDatabaseTable:save() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:save() -- See if save required local bChanged = false if not self.rowsCopy or #self.rows ~= #self.rowsCopy then bChanged = true else for r,row in ipairs(self.rows) do for c,col in ipairs(self.columns) do if self.rows[r][col] ~= self.rowsCopy[r][col] then bChanged = true break end end if bChanged then break end end end if not bChanged then return end outputDebug( 'TOPTIMES', 'SDatabaseTable: Saving ' .. self.name ) -- Being save executeSQLQuery( 'BEGIN TRANSACTION' ); local cmd -- DELETE TABLE -- Build command --cmd = 'DELETE FROM ' .. qsafetablename( self.name ) cmd = 'DROP TABLE IF EXISTS ' .. qsafetablename( self.name ) executeSQLQuery( cmd ) -- CREATE TABLE self:createTable() -- Rebuild -- For each row for r,row in ipairs(self.rows) do -- INSERT INTO cmd = 'INSERT INTO ' .. qsafetablename( self.name ) .. ' VALUES (' for c=1,#self.columns do if c > 1 then cmd = cmd .. ', ' end local key = self.columns[c] if type(row[key]) == 'number' then cmd = cmd .. row[key] or 0 else cmd = cmd .. qsafestring( row[key] or '' ) end end cmd = cmd .. ')' executeSQLQuery( cmd ) end executeSQLQuery( 'END TRANSACTION' ); -- Make copy to detect changes self.rowsCopy = table.deepcopy(self.rows) end --------------------------------------------------------------------------- -- -- SDatabaseTable:createTable() -- -- -- --------------------------------------------------------------------------- function SDatabaseTable:createTable() local cmd -- CREATE TABLE -- Build command cmd = 'CREATE TABLE IF NOT EXISTS ' .. qsafetablename( self.name ) .. ' (' for c=1,#self.columns do if c > 1 then cmd = cmd .. ', ' Link to comment
xXMADEXx Posted June 10, 2013 Share Posted June 10, 2013 it is probably because the host dosn't allow remote connections. If you hosting computer on your local computer, i recommend that you use XAMPP, but if you have hosting, and they don't provide MySQL, use this website: http://www.freesqldatabase.com/ 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