egorkoltyshev Posted March 31, 2022 Share Posted March 31, 2022 (edited) 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 March 31, 2022 by egorkoltyshev Link to comment
Gw8 Posted March 31, 2022 Share Posted March 31, 2022 (edited) use the information you need, then save the complete table in the created object. car = createVehicle(v.model, v.x, v.y, v.z) car:setData("dbData",result[ i ]) Edited March 31, 2022 by Gw8 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