Jump to content

Loading data from a MySQL database


Recommended Posts

Hello! A few questions about loading data from the database, in my case, for example, transport. But by analogy, in the future I plan to load all the other information from the database.

The code I have written so far:

Spoiler
local tableName = 'vehicles'
local vehicle = {}
local totalVehicles = 0

addEventHandler("onResourceStart", getResourceRootElement(), function()
	if(exports.mysql:get_mysql_database_connection()) then
		local load_time = getTickCount()
		local query = dbQuery(exports.mysql:get_mysql_database_connection(), "SELECT * FROM `"..tableName.."`")
		local result, numrows = dbPoll(query, -1)
		if (result and numrows > 0) then
			for _, v in pairs(result) do
				vehicle[v.id] = createVehicle(v.model, v.x, v.y, v.z)
				
				setElementData(vehicle[v.id], 'id', v.id)
				setElementData(vehicle[v.id], 'model', v.model)
				setElementData(vehicle[v.id], 'x', v.x)
				setElementData(vehicle[v.id], 'y', v.y)
				setElementData(vehicle[v.id], 'z', v.z)
			
				totalVehicles = totalVehicles + 1
			end
			outputServerLog("[VEHICLES] Загружено "..totalVehicles.." транспортных средств за "..getTickCount() - load_time.." мс")
			dbFree(query)
		else
			outputServerLog("[VEHICLES] Таблица с транспортом не найдена или пуста.")
		end
	else
		outputServerLog("[VEHICLES] Не удалось подключиться к базе данных.")
	end
end)

 


1. Am I creating the vehicle in the vehicles table correctly and storing the data in elementData? Or should we also use the Vehicle[v.id]['x'] = v.x table to store the data?

vehicle[v.id] = {}		
vehicle[v.id]['x'] = v.x


2. Will there be any problems and how convenient it is if I use its id from the database as the transport number and not as it is written in the table 1,2,3..n

for _, v in pairs(result) do				
	vehicle[v.id] = createVehicle(v.model, v.x, v.y, v.z)

or
 

for i, v in pairs(result) do				
	vehicle[i] = createVehicle(v.model, v.x, v.y, v.z)


Sorry for my English.

Edited by egorkoltyshev
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...