Read and write the same file

Thank you very much, now I will continue to study your modules to have a good knowledge of how to integrate it into my project.

I have a question for you.
is it possible that each created user can have his own database to store for example: Status, level, score etc…?

You could do that, but why not just store more info in the userDB? You’re already storing the user name and password. You could just add additional fields to the user records.

This is, in my opinion better, because the data is self-contained in one place and you don’t risk leaving data-behind if you should delete a user.

Regardless, there is no rule. You can organize your data any way you want. You just need to decide on a system of rules for maintaining the data and to then write code to support those rules.

Thanks for your answer, it was just a question in case I need it. what you say is exactly what i want. when I delete the user all the information associated with him will also be deleted at the same time, without me having to delete them myself.

by the way could you add one or two additional fields in your last models please. (username, password, e-mail, phone etc.)

is it possible to take a data from a database1 and make it selectable as a text field into another database2.

I think you can manage this.

  1. Add new functions in the module to get/set data:
function m.setPhoneNumber( name, phone )
    local rec = m.getRecordByUserName( name )
    if ( not rec ) then
        return false
    end

    rec.phone = phone
 
   table.save( m._userDB, "userDB.txt" )

   return true
 end
function m.getPhoneNumber( name )
    local rec = m.getRecordByUserName( name )
    if ( not rec ) then
        return nil
    end

    return rec.phone
 end
  1. After logging in, keep track of the user who is logged in and use that name when accessing the ‘DB’.

You can organize your data any way you like.

You can use GUIs and logic to access data, but you can’t ‘make it selectable’.

So far you’ve been storing data in tables and using GUIs to allow input. You’ll have to extend that concept.

If you’re going to have more than one ‘user db’ you need to modify the module to handle having more than one ‘DB’ table loaded. I’m unclear what benefit this provides. It sounds a little like you’re getting interested in real databases, in which case you may want to learn about mySQL which is the local DB format supported in Corona.

This is totally different from what we’ve been doing so far. So far, we’ve used the term ‘DB’ very loosely. Really what we’ve done is create an ad hoc data storage and retrieval module for a limited set of data fields and records.

After spending a week studying your modules I learned a lot. although it’s a bit different from other databases but I’ll end up finding the majority of what I wanted to do. I asked you the question because I thought there was another way to create selectable data.
I still have two more questions:

    • is it possible to display a word on the screen and allow the user to rename it without using a data table?
    • using the database can we rename a user without deleting him? because by deleting the user you also delete his data.

Please remember. What we’ve made so far is not using a traditional database format. It is just a table containing tables saved and restored from permanent storage.

While the way it is organized and managed probably meets the definition of a database I’d be careful referring to it that way or you’ll confuse folks down the line who hear/read the word database and think things like mySQL, Mongo, etc.

On to your questions (restated below):

1a. Is it possible to display a text object on the screen and change the text that object displays? Yes. That is what we did with your feedback labels.

1b. Is this mechanism dependent upon a data table? No.

  1. Can you change the name field in of record in your current ‘database’? Yes. There is nothing controlling what is in those fields other than the code you write. This is why I referred to this solution above (last post) as ad hoc data storage. Other than the code we’ve written, there is nothing controlling what gets put in those fields.

Hello my friend !
I leave all the files in place and I create two modules, one which goes with the base userDB.txt and the other with Admin.txt. Now I want to remove all content from UserDB.txt without modifying the other one.
I don’t know the code. Do you have an idea ?

thank you my man !
everything works perfectly. Now I can add code, logic and modify as needed table, string, module etc…
it’s cool.

Hi my friend !

I am trying to do that


 local numRecords = user_db_module.getRecordCount()
if( numRecords > 0 ) then
   for i = 1, numRecords do
local rec1 = user_db_module.getRecordByIndex( i )
 -----------------------------------------------------------------------------------

local nom1 = display.newText(sceneGroup,  rec1.name, 10, 50, native.systemFont, 20 )
      nom1:setFillColor(0, 0, 0)
      nom1.x = 310   
      nom1.anchorY = 0
     nom1.isVisible = false

end  
end

local function invisible( event )
if(btnlistelect.isVisible == true) then   nom1.isVisible = true    end      ----------------- Works
if(btnlistelect.isVisible == false) then  nom1.isVisible = false   end   ---------------    not works


   end
   Runtime:addEventListener( "enterFrame", invisible )
   

it seems that it is not possible to call the object under " if "

  1. It is possible, but I would not write the code that way (see my version below).
  2. Don’t forget to maintain clean and consistent indentation. Makes code easier to read and share.
  3. Your variable/object names are either in non-English language and/or not very meaningful.

Tips:

  • Use descriptive and clear names.
  • Don’t give in to the temptation to use short names to make typing ‘easier’.
  • Good indentation + variable/object/function/file names makes for easier to maintain apps/games.

Suggested Code Change

You can just do this to achieve same goal:

local function invisible( event )
    nom1.isVisible = btnlistelect.isVisible
end

Runtime:addEventListener( "enterFrame", invisible )

the error message still persist :

select.lua:1392: attempt to index global ‘nom1’ (a nil value)
16:54:06.150 stack traceback:

Ok, based on your proposal I have an idea that comes to mind. is that I can’t call an object that is inside a function while I’m outside.
I now have to create another hidden object from the beginning to act as an interface between the interior and the exterior.

I only have to call this hidden object the outer being and it will give instruction to the inner object.

I will do it now.

okay !
it’s works like that

---here  the function to move  mo to x or y position  -------
----------------when the button  --btnlistelect.isVisible--------it  change the inside  hidden object  to y position  and then the y position make the nom1 visible   .......
------------------


--------------------hidden object --------

local mo = display.newImageRect(sceneGroup, "image/toolb.png", 139, 40 )
      mo.x = 349
      mo.y = 400
      mo.alpha = 0.01


 local numRecords = user_db_module.getRecordCount()
if( numRecords > 0 ) then
   for i = 1, numRecords do
local rec1 = user_db_module.getRecordByIndex( i )
 -----------------------------------------------------------------------------------

local nom1 = display.newText(sceneGroup,  rec1.name, 10, 50, native.systemFont, 20 )
      nom1:setFillColor(0, 0, 0)
      nom1.x = 310   
      nom1.anchorY = 0
     nom1.isVisible = false

end  
end

local function invisible( event )

if(mo.y == 800) then  nom1.isVisible = true   end
if(mo.y == -100)  then  nom1.isVisible = false   end


   end
   Runtime:addEventListener( "enterFrame", invisible )

I am sorry for the language of my variables names. I am using a short name to make it easy for me.

To reference objects created in one function elsewhere, you can forward declare variables used to store the object reference.

local nom1 -- forward declare variable nom1; visible after this line everywhere in file

local function showNom()
   nom1.isVisible = true
end

local function hideNom()
   nom1.isVisible = false
end

local function createNom()
    nom1 = display.newText(sceneGroup,  rec1.name, 10, 50, native.systemFont, 20 )
    nom1:setFillColor(0, 0, 0)
    nom1.x = 310   
    nom1.anchorY = 0
    nom1.isVisible = false
end

createNom()
hideNom()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.