Hello everyone and thanks in advance to all the helpers.
I’m trying to make a data base on a text file which I’ll be able to print a single line by selecting the line number.
Does anyone knows how to do that ?
Also, if you have a better way to make a data base for a trivia like game (All the questions and answers are written ahead ) I will appreciateit.
Here’s the code that I tried to use but it prints all the lines in the document:
[lua]local path = system.pathForFile( “myfile.txt”, system.ResourceDirectory )
local file, errorString = io.open( path, “r” )
if not file then
print( "File error: " … errorString )
else
for line in file:lines() do
print( line )
end
io.close( file )
end
file = nil
[/lua]
Not sure if it’s possible but as it’s pretty fast to read a file you could probably just insert them into a table and call the selected line from there.
local path = system.pathForFile( "build.settings", system.ResourceDirectory ) local file, errorString = io.open( path, "r" ) local lines = {} if not file then print( "File error: " .. errorString ) else for line in file:lines() do table.insert( lines, line) end io.close( file ) end file = nil print(" \*\*\* SELECT YOUR LINE AND PRINT IT \*\*\* ") print(lines[1]) print(lines[2]) print(lines[3])
Not sure if it helps but it works.
Best regards,
Tomas
Regarding your other question about a better way to save Trivia questions:
-
Save the data as JSON and not on lines
-
Save it in a database (“Questions” and “Answers” tables with a identifier (QuestionID in Answer table most likely))
Best regards,
Tomas
Not sure if it’s possible but as it’s pretty fast to read a file you could probably just insert them into a table and call the selected line from there.
local path = system.pathForFile( "build.settings", system.ResourceDirectory ) local file, errorString = io.open( path, "r" ) local lines = {} if not file then print( "File error: " .. errorString ) else for line in file:lines() do table.insert( lines, line) end io.close( file ) end file = nil print(" \*\*\* SELECT YOUR LINE AND PRINT IT \*\*\* ") print(lines[1]) print(lines[2]) print(lines[3])
Not sure if it helps but it works.
Best regards,
Tomas
Regarding your other question about a better way to save Trivia questions:
-
Save the data as JSON and not on lines
-
Save it in a database (“Questions” and “Answers” tables with a identifier (QuestionID in Answer table most likely))
Best regards,
Tomas