Working with large blocks of text

Hi everyone

i am working with large blocks of text, and i am using this link:

https://coronalabs.com/blog/2014/06/17/tutorial-working-with-large-blocks-of-text/

everything is working, but i need to insert text from a database to the variable that contains the large text, check the link to see this variable

local myText = [[ …

]]

how can i insert text inside of [[…]] to work like expected in the above link?

Greetings

[[and]] are just quote marks around a string.  What you return from the database is a string already.  You don’t really need to add more quotes around it.

But it might be good to see more of your code to get a better idea of what you’re trying to do.

Rob

function scene:create( event ) local sceneGroup = self.view local path = system.pathForFile("database.sqlite", system.ResourceDirectory) db = sqlite3.open(path) -- -- width and height screen size -- local w = display.contentWidth local h = display.contentHeight local sql = "SELECT field FROM table WHERE id=3" --where i will get only one row for row in db:nrows(sql) do text = row.descricao end -- Now instead of using the next variable to show text, i want to use the variable above called text, to show the text that was queried local myText = [[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque semper mollis erat a. Praesent sit amet lorem nisl. Pellentesque interdum felis quis vehicula vestibulum. Donec ut dolor tortor.]] local paragraphs = {} local paragraph local tmpString = myText local scrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight \* 0.83, bottomPadding = 0, scrollWidth = display.contentWidth, horizontalScrollDisabled = true, scrollHeight = 10000 } local options = { text = "", width = 300, font = "HelveticaNeue", fontSize = 14, align = "left" } local yOffset = 20 repeat paragraph, tmpString = string.match( tmpString, "([^\n]\*)\n(.\*)" ) options.text = paragraph paragraphs[#paragraphs+1] = display.newText( options ) paragraphs[#paragraphs].anchorX = 0 paragraphs[#paragraphs].anchorY = 0.055 paragraphs[#paragraphs].x = 10 paragraphs[#paragraphs].y = yOffset paragraphs[#paragraphs]:setFillColor( 0 ) scrollView:insert( paragraphs[#paragraphs] ) yOffset = yOffset + paragraphs[#paragraphs].height until tmpString == nil or string.len( tmpString ) == 0 sceneGroup:insert(scrollView) end

here is a very simple example of part of my code, inside of the code i am saying what i am trying to do.

i cannot simple use the variable text to show the text. 

Thanks for the help

Seems to me your SQL query is wrong:

local sql = "SELECT field FROM table WHERE id=3" --where i will get only one row for row in db:nrows(sql) do      text = row.descricao end

SELECT field FROM table WHERE id=3

means get the field named “field” from the database named “table” where the record has an id of 3.

Therefore I would expect row to only have one member named “field”, i.e.    row.field   would be what holds the string.

Rob

Hi Rob, thanks for the answer.

I have prepared a very short and simple example with a sqlite database to show you my problem.

Check my link and download the zip. Focus on view1.lua, i am getting text from the database and print it to the terminal, but i want to display it like i am doing with the variable “myText” but the text that i get from the database don’t have [[and]] and will fail to display.

1º Run my code on simulador, the view1 is where we should focus, view2 don’t matter.

2º Check my code, and try insert the text from the database inside of the scroll view, where already i have some text.

The text that i already have is only to show you what i want, remove the text and only show in the scroll view the text that we get from the database

Really thank you for the help

Link for the sample app:

https://www.dropbox.com/s/rovv1z70cmfu2p3/test.zip?dl=0

Your first problem is this line:

local tmpString = myText

needs to be:

local tmpString = text

But then you get an error which would have been really helpful to help track this down:
 

view1.lua:66: bad argument #-1 to ‘newText’ (string expected, got nil)
    stack traceback:
        [C]: in function ‘newText’
        view1.lua:66: in function <view1.lua:13>
        ?: in function ‘dispatchEvent’
        ?: in function ‘gotoScene’
        main.lua:17: in function ‘onFirstView’
        main.lua:39: in main chunk
 

or something similar.  The second issue which is your real but is that “repeat” loops will execute once regardless.  There becomes a point where tmpString becomes nil from the string.match() but you’re still trying to use it.  Try adding this right before you create the display.newText():

      if paragraph == nil then
          break;
      end

Rob

I have implemented the code with the alterations, but i can’t resolve the error that you mention above.

Any idea about that?

function scene:create( event ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; local path = system.pathForFile("test.sqlite", system.ResourceDirectory) &nbsp; db = sqlite3.open(path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local myText = [[Lorem ipsum dolor sit amggggggggggggggggfdjkhgdd test]] &nbsp; local sql = "SELECT field1 FROM table1" &nbsp; local j = 0 &nbsp; for row in db:nrows(sql) do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = j + 1 &nbsp;&nbsp;&nbsp; text = row.field1 &nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local paragraphs = {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local paragraph &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local tmpString = text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local scrollView = widget.newScrollView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; top = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; left = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = display.contentWidth, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = display.contentHeight \* 0.83, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bottomPadding = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollWidth = display.contentWidth, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; horizontalScrollDisabled = true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollHeight = 10000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --scrollView.y = display.contentCenterY - display.contentCenterY \*0.001 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local options = { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text = "", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = 300, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; font = "HelveticaNeue", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fontSize = 14, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; align = "left" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local yOffset = 20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repeat &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraph, tmpString = string.match( tmpString, "([^\n]\*)\n(.\*)" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options.text = paragraph &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if paragraph == nil then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs+1] = display.newText( options ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].anchorX = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].anchorY = 0.055 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].x = 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].y = yOffset &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs]:setFillColor( 0 ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollView:insert( paragraphs[#paragraphs] ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yOffset = yOffset + paragraphs[#paragraphs].height &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; until tmpString == nil or string.len( tmpString ) == 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sceneGroup:insert(scrollView) end

Compare your code to this

Thank you Rob. It is working but not 100% i will fix it.

Thanks for all the help :slight_smile:

[[and]] are just quote marks around a string.  What you return from the database is a string already.  You don’t really need to add more quotes around it.

But it might be good to see more of your code to get a better idea of what you’re trying to do.

Rob

function scene:create( event ) local sceneGroup = self.view local path = system.pathForFile("database.sqlite", system.ResourceDirectory) db = sqlite3.open(path) -- -- width and height screen size -- local w = display.contentWidth local h = display.contentHeight local sql = "SELECT field FROM table WHERE id=3" --where i will get only one row for row in db:nrows(sql) do text = row.descricao end -- Now instead of using the next variable to show text, i want to use the variable above called text, to show the text that was queried local myText = [[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque semper mollis erat a. Praesent sit amet lorem nisl. Pellentesque interdum felis quis vehicula vestibulum. Donec ut dolor tortor.]] local paragraphs = {} local paragraph local tmpString = myText local scrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight \* 0.83, bottomPadding = 0, scrollWidth = display.contentWidth, horizontalScrollDisabled = true, scrollHeight = 10000 } local options = { text = "", width = 300, font = "HelveticaNeue", fontSize = 14, align = "left" } local yOffset = 20 repeat paragraph, tmpString = string.match( tmpString, "([^\n]\*)\n(.\*)" ) options.text = paragraph paragraphs[#paragraphs+1] = display.newText( options ) paragraphs[#paragraphs].anchorX = 0 paragraphs[#paragraphs].anchorY = 0.055 paragraphs[#paragraphs].x = 10 paragraphs[#paragraphs].y = yOffset paragraphs[#paragraphs]:setFillColor( 0 ) scrollView:insert( paragraphs[#paragraphs] ) yOffset = yOffset + paragraphs[#paragraphs].height until tmpString == nil or string.len( tmpString ) == 0 sceneGroup:insert(scrollView) end

here is a very simple example of part of my code, inside of the code i am saying what i am trying to do.

i cannot simple use the variable text to show the text. 

Thanks for the help

Seems to me your SQL query is wrong:

local sql = "SELECT field FROM table WHERE id=3" --where i will get only one row for row in db:nrows(sql) do &nbsp;&nbsp;&nbsp;&nbsp; text = row.descricao end

SELECT field FROM table WHERE id=3

means get the field named “field” from the database named “table” where the record has an id of 3.

Therefore I would expect row to only have one member named “field”, i.e.    row.field   would be what holds the string.

Rob

Hi Rob, thanks for the answer.

I have prepared a very short and simple example with a sqlite database to show you my problem.

Check my link and download the zip. Focus on view1.lua, i am getting text from the database and print it to the terminal, but i want to display it like i am doing with the variable “myText” but the text that i get from the database don’t have [[and]] and will fail to display.

1º Run my code on simulador, the view1 is where we should focus, view2 don’t matter.

2º Check my code, and try insert the text from the database inside of the scroll view, where already i have some text.

The text that i already have is only to show you what i want, remove the text and only show in the scroll view the text that we get from the database

Really thank you for the help

Link for the sample app:

https://www.dropbox.com/s/rovv1z70cmfu2p3/test.zip?dl=0

Your first problem is this line:

local tmpString = myText

needs to be:

local tmpString = text

But then you get an error which would have been really helpful to help track this down:
 

view1.lua:66: bad argument #-1 to ‘newText’ (string expected, got nil)
    stack traceback:
        [C]: in function ‘newText’
        view1.lua:66: in function <view1.lua:13>
        ?: in function ‘dispatchEvent’
        ?: in function ‘gotoScene’
        main.lua:17: in function ‘onFirstView’
        main.lua:39: in main chunk
 

or something similar.  The second issue which is your real but is that “repeat” loops will execute once regardless.  There becomes a point where tmpString becomes nil from the string.match() but you’re still trying to use it.  Try adding this right before you create the display.newText():

      if paragraph == nil then
          break;
      end

Rob

I have implemented the code with the alterations, but i can’t resolve the error that you mention above.

Any idea about that?

function scene:create( event ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; local path = system.pathForFile("test.sqlite", system.ResourceDirectory) &nbsp; db = sqlite3.open(path) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local myText = [[Lorem ipsum dolor sit amggggggggggggggggfdjkhgdd test]] &nbsp; local sql = "SELECT field1 FROM table1" &nbsp; local j = 0 &nbsp; for row in db:nrows(sql) do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j = j + 1 &nbsp;&nbsp;&nbsp; text = row.field1 &nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local paragraphs = {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local paragraph &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local tmpString = text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local scrollView = widget.newScrollView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; top = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; left = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = display.contentWidth, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = display.contentHeight \* 0.83, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bottomPadding = 0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollWidth = display.contentWidth, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; horizontalScrollDisabled = true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollHeight = 10000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --scrollView.y = display.contentCenterY - display.contentCenterY \*0.001 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local options = { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text = "", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = 300, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; font = "HelveticaNeue", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fontSize = 14, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; align = "left" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local yOffset = 20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; repeat &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraph, tmpString = string.match( tmpString, "([^\n]\*)\n(.\*)" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options.text = paragraph &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if paragraph == nil then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs+1] = display.newText( options ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].anchorX = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].anchorY = 0.055 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].x = 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs].y = yOffset &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs[#paragraphs]:setFillColor( 0 ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scrollView:insert( paragraphs[#paragraphs] ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yOffset = yOffset + paragraphs[#paragraphs].height &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; until tmpString == nil or string.len( tmpString ) == 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sceneGroup:insert(scrollView) end

Compare your code to this

Thank you Rob. It is working but not 100% i will fix it.

Thanks for all the help :slight_smile:

just a side note, my advise is to stop using (repeat until) and start using (while do) cicle, while only enters the cicle if the condition is true, so you don’t need the if inside the cicle to check that. btw, while is the fastest cicle too.

i’ve created a working version with database if you want to learn from it:

https://mega.co.nz/#!94I2lCRA!gdhZa0WIVtefN-nVJG_nzV72_5_6bCO38-nbGymrHBk

just a side note, my advise is to stop using (repeat until) and start using (while do) cicle, while only enters the cicle if the condition is true, so you don’t need the if inside the cicle to check that. btw, while is the fastest cicle too.