# Filtering the tableView with args from SQL

**URL:** <https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333>\
**Category:** Widgets\
**Created:** [October 18, 2015, 6:46pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333 "2015-10-18T18:46:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 18, 2015, 6:46pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/1 "2015-10-18T18:46:54Z")

</div>

This is a function from my gamePlay file, the event is parsed from a button to the function **updateSql(event)** and the arg from the button is a SQL argument. **engineResponse(sql)**

I.e. (button clicked: sql =&nbsp;“SELECT \* FROM&nbsp; birds WHERE PATTERN = ‘Stripes’” &nbsp;– this is the buttons _self.id_

[LUA]

function updateSql(event)  
&nbsp; tableView:deleteAllRows()  
– &nbsp; The event parsed from the button is sql, like:&nbsp;“SELECT \* FROM&nbsp; birds WHERE PATTERN = ‘Stripes’”

count = 0  
&nbsp;&nbsp;&nbsp; for row in db:nrows(sql) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = count +1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count]={}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].NAME = row.NAME  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].LATIN = row.LATIN  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].INFO = row.INFO  
&nbsp;&nbsp;&nbsp; end

&nbsp;&nbsp;&nbsp; for i = 1, #dbTable do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local isCategory = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowHeight = 70  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowColor = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default = { colorConverter(114, 72, 30, 0) },  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local lineColor = { colorConverter(20, 20, 0, 0) }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tableView:insertRow  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowHeight = rowHeight,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowColor = rowColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lineColor = lineColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp; end  
end

[/LUA]

My problem is that I can’t seam to figure out how to filter out the rows that is ~= sql

So I can start up with all the rows in my sql file and filter out when clicking on different buttons

Hope this makes sense to anyone.

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 19, 2015, 8:24pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/2 "2015-10-19T20:24:57Z")

</div>

I fixed it with a function that builds and update the sql query:

[lua]

function sqlViewBuilder(argA, argB)  
&nbsp; sql = "SELECT \* FROM birds WHERE "&nbsp; … tostring(argA) … " = " … “’” … tostring(argB) … “’”  
&nbsp; return sql  
end

for i = 1, #dbTable do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local isCategory = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowHeight = 70  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowColor = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default = { colorConverter(114, 72, 30, 0) },  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local lineColor = { colorConverter(20, 20, 0, 0) }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tableView:insertRow  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; –&nbsp; isCategory = isCategory,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowHeight = rowHeight,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowColor = rowColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lineColor = lineColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; end

&nbsp; return sql

[/LUA]

If I do a call just to check:

[LUA]

sqlViewBuilder(‘PATTERN’, ‘Spotted’)

[/LUA]

It´s crucial to clear the tableView before drawing a new one, so I do that before i call the sqlViewBuilder i.e. in another function

[LUA]

tableView:deleteAllRows()

[/LUA]

I get this result in the terminal:

SELECT \* FROM birds WHERE PATTERN = ‘Spotted’

Now it filters my scene view nice and tidy 🙂

Next thing I will test is to make a table that I feed in all the arguments in separate strings and the do an unPack()

I will have to make a function that will deal with AND, OR for the selections to be nice and complex …but that´s another story

I will post that too for those who´s interested

…Corona coding is fun!

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 21, 2015, 7:41pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/3 "2015-10-21T19:41:25Z")

</div>

Here´s the _table constructor way_ I mentioned earlier in the post.

Scenario: You want to construct a complex sql query based on input from buttons or other display objects that feeds the string into an array instead of just returning the string. The little knowledge I got in SQL is that when building a fairly complex query, I have to use a \*start sequence that is like “SELECT \* FROM &nbsp;myDB WHERE”. After that I can use AND / OR operators to narrow the search more and more like:

“SELECT \* FROM&nbsp; myDB WHERE Shoes = ‘Red’ AND Hair = ‘Green’ OR Hair = ‘Blue’”

here is how I did it (suppose we already have inserted stuff from i.e. some button that return strings back):

[LUA]

local mySqlView =  
{  
&nbsp; “Shoes = ‘Red’”,  
&nbsp; “Hair = ‘Green’”,  
&nbsp; “Nails = ‘Red’”  
}

[/LUA]

and then we have a function that iterates over&nbsp;mySqlView table and puts the \*start sequence first, then it puts AND between all the children of the table -(I didn´t include OR operator because I just need AND in my game ;D).

It is obvious that we use these operators between the entries in the array except the last child because an OR or an AND is always followed by something, right? This function puts the operator between every child until it reaches the end and on the last it skips the operators.

Then I use table.concat() to concatenate all the entries in the table. I have a variable called sep(separator) I use at the start and the end just because SQL requires it.

[LUA]

local function functionSqlView()  
local counter = 1  
local res = {}  
local sep = ‘"’  
table.insert(res, "SELECT \* FROM&nbsp; myDB WHERE ")  
&nbsp; for \_,v in ipairs(mySqlView) do  
&nbsp;&nbsp; if counter \< #mySqlView then  
&nbsp;&nbsp;&nbsp; table.insert( res, (v …&nbsp; " AND "))  
&nbsp;&nbsp; else  
&nbsp;&nbsp;&nbsp; table.insert( res, v )  
&nbsp;&nbsp; end  
&nbsp;&nbsp;&nbsp; counter = counter + 1  
end  
&nbsp; print( sep … table.concat( res ) … sep )  
end

[/LUA]

Then, when I call the function [Lua]&nbsp;functionSqlView()[/LUA]

I get this in return in the terminal:

“SELECT \* FROM&nbsp; myDB WHERE Shoes = ‘Red’ AND Hair = ‘Green’ AND Nails = ‘Red’”

If we look at my first example over I can just replace the last command, print, in the function functionSqlView with: [LUA] return sql =&nbsp;sep … table.concat( res ) … sep &nbsp;[/LUA]

(this is not tried yet, but I guess it´s a valid tail call 🙂

Hope this can be of any help and as I said earlier, I´m not very good at sql so any comments and suggestions here are more than welcome!

Have fun

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 30, 2015, 7:00pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/4 "2015-10-30T19:00:23Z")

</div>

Well, I have to say that it didn´t work. The problem is that I can’t seem to update the view with right information, it just change the first entries in the tableView and it won’t &nbsp;filter out those I dont want to have in the list.

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [October 31, 2015, 3:21am UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/5 "2015-10-31T03:21:15Z")

</div>

I’m not sure I really understand the question at hand. Are you needing help with SQL as a language?

Rob

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 31, 2015, 6:34pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/6 "2015-10-31T18:34:13Z")

</div>

Not really I think my problem is how to get the tableView to update with my new sql query (In this game I like to first list every entry in the db and then I like to filter out and narrow the search so to say)

I use composer and everything but the updatefunction works nice! Heres a stripped version of the playGame file:

[LUA]

– THE PLAYGAME FILE:

– LOCALS FWD  
local composer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = require “composer”  
local scene&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = composer.newScene()  
local path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = system.pathForFile( “Birds.sqlite”, system.ResourceDirectory ) – IM NOT WRITING TO THE DB FILE  
local imagePath&nbsp;&nbsp;&nbsp;&nbsp; = “Images/Birds/”  
local bgImage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = imagePath … “bgTheme.jpg”  
local text, themeText, bg, t, count, bgItem, bg, colorConverter, tableView, leftRib, woodPanel, butNext, butBack  
local leftIndent&nbsp;&nbsp;&nbsp;&nbsp; = 50  
local dbTable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local rowBg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local rowName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local rowLatin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local playButton&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local onRowRender&nbsp;&nbsp;&nbsp; = {}  
local onRowTouch&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local funtionSqlView = {}  
local db&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = sqlite3.open( path )  
local \_W, \_H&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = display.contentWidth, display.contentHeight  
local group&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
local containerGroup = display.newGroup( )  
local dashboardGroup = display.newGroup( )

–GLOBALS FWD  
mySqlView&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {} – I CHOOSE TO HAVE THIS GLOBAL FOR NOW  
\_G.specPool&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}  
\_G.catText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = “Push one of the buttons”  
updateSql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = {}

– FUNCTIONS  
function functionSqlView() – THIS IS THE SQL STATEMENT BUILDER (YOU CAN SEE IN THE TERMINAL)… LAME NAME, I KNOW HAHA  
&nbsp; local counter = 1  
&nbsp; local res&nbsp;&nbsp;&nbsp;&nbsp; = {}  
&nbsp; local sep&nbsp;&nbsp;&nbsp;&nbsp; = ‘"’&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; – FOR LAZYNESS

&nbsp; table.insert(res, "SELECT \* FROM birds WHERE ")  
&nbsp; for \_,v in ipairs(mySqlView) do  
&nbsp;&nbsp;&nbsp; if counter \< #mySqlView then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table.insert( res, (v …&nbsp; " AND ")) – I WILL DEAL WITH ‘OR’ LATER ;D  
&nbsp;&nbsp;&nbsp; else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table.insert( res, v )  
&nbsp;&nbsp;&nbsp; end  
&nbsp;&nbsp;&nbsp; counter = counter + 1  
&nbsp; end  
&nbsp; sql = sep … table.concat( res ) … sep – CONCATENATES THE WHOLE STATEMEMT STRING  
&nbsp; updateSql(sql)&nbsp;

– HERE IS WHERE I START TO GET LOST, I HAVE THE STATEMENT AND I WANT TO UPDATE AND REMOVE  
– THE ROWS THAT DOESN´T MEET THEESE CRITERIAS… FOLLOW DOWN TO UPDATESQL FUNCTION  
end

local function onSystemEvent( event )  
if ( event.type == “applicationExit” ) then  
&nbsp; db:close()  
end  
end

local function colorConverter(r, g, b, a) &nbsp;– JUST SO I CAN USE RGB VALUES DIRECT FROM PHOTOSHOP OR ILLUSTRATOR  
&nbsp;&nbsp; local colorR, colorG, colorB, alphA  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; colorR = r/256; colorG = g/256; colorB = b/256  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alphA = a or nil  
&nbsp;&nbsp; return colorR, colorG, colorB, alphA  
end

function updateCatText() – FOR UPDATING THE GLOBAL CATEGORY TEXT OVER THE BUTTONS  
if themeText then display.remove( themeText) end  
themeText = display.newText( tostring(\_G.catText), 0, 0, \_G.msgFont , 34 )  
&nbsp;&nbsp;&nbsp; themeText.anchorX, themeText.anchorY = 0, 0  
&nbsp;&nbsp;&nbsp; themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 20  
&nbsp;&nbsp;&nbsp; themeText:setFillColor( colorConverter(236, 210, 135, 1) )

return true  
end

–[[

SCENARIO:

I start the app with the sql query showing all rows in db without any limit, like this:

“SELECT \* FROM birds”

The function over called functionSqlView() produces sql queries based on what buttons i decide to click on.

Let´s say I hit two buttons, then my sql query could look like this:

“SELECT \* FROM birds WHERE WINGS = ‘Long’ AND TAIL = ‘Short’”

WHAT COMES HERE IS WHAT I REALLY DONT KNOW!!

1. I GOT TO TRAVERSE THE RIGHT ROWS FROM THE DB
2. THE LIST OF BIRDS IN THE TABLEVIEW WILL SHOW LESSER AND LESSER ENTRIES  
&nbsp;&nbsp;&nbsp; THE MORE I NARROW THE SEARCH.

–]]

function updateSql(event)  
tableView:deleteAllRows() – FIRST I LIKE TO REMOVE THE ROWS AND THAT WORKS OF COURSE

&nbsp;&nbsp;&nbsp; for i = 1, #dbTable do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local isCategory = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowHeight = 70  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowColor = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default = { colorConverter(114, 72, 30, 0) },  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local lineColor = { colorConverter(20, 20, 0, 0) }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; – Insert the row into the tableView  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tableView:insertRow  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; –&nbsp; isCategory = isCategory,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowHeight = rowHeight,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowColor = rowColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lineColor = lineColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; end

end

– THIS ONE WORKS NICE AND IT TAKES YOU OVER TO A SPEC PAGE AND SO ON…  
local function birdSpec(self, event)  
&nbsp;&nbsp; if event.phase == “began” then  
&nbsp;&nbsp; if \_G.debug == 1 then print("\*\* birdSpec: \n\n") end&nbsp;&nbsp;&nbsp; – DEBUG  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \_G.specPool.name&nbsp; = self.specName  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \_G.specPool.latin = self.specLatin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \_G.specPool.info&nbsp; = self.specInfo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; composer.gotoScene( “playSpecScene” )  
&nbsp;&nbsp; elseif event.phase == “cancelled” or event.phase == “ended” then  
&nbsp;&nbsp; end  
&nbsp;&nbsp; return true  
end

function onRowRender( event )  
&nbsp;&nbsp;&nbsp; local phase = event.phase  
&nbsp;&nbsp;&nbsp; local row = event.row

&nbsp;&nbsp;&nbsp; row.bg = display.newRoundedRect( 0, 0, display.contentWidth -48, 50, 12 )  
&nbsp;&nbsp;&nbsp; row.bg.strokeWidth = 0  
&nbsp;&nbsp;&nbsp; row.bg:setFillColor( colorConverter(236, 210, 135, 1) )  
&nbsp;&nbsp;&nbsp; row.bg.alpha = 1  
&nbsp;&nbsp;&nbsp; row.bg.anchorX = 1  
&nbsp;&nbsp;&nbsp; row.bg.anchorY = 0  
&nbsp;&nbsp;&nbsp; row.bg.x = display.contentWidth  
&nbsp;&nbsp;&nbsp; row.bg.y = 10  
&nbsp;&nbsp;&nbsp; row:insert( row.bg )

&nbsp;&nbsp;&nbsp; row.title = display.newText( row, dbTable[row.index].NAME, 0, 0, \_G.msgFont , 24 )  
&nbsp;&nbsp;&nbsp; row.title.x = leftIndent + 10  
&nbsp;&nbsp;&nbsp; row.title.anchorX = 0  
&nbsp;&nbsp;&nbsp; row.title.y = row.contentHeight \* 0.15+ row.bg.y  
&nbsp;&nbsp;&nbsp; row.title:setFillColor( colorConverter(114, 72, 30, 1) )

&nbsp;&nbsp;&nbsp; row.ingress = display.newText( row, dbTable[row.index].LATIN, 0, 0, \_G.msgFont , 14 )  
&nbsp;&nbsp;&nbsp; row.ingress.x = leftIndent + 10  
&nbsp;&nbsp;&nbsp; row.ingress.anchorX = 0  
&nbsp;&nbsp;&nbsp; row.ingress.y = row.title.y +20  
&nbsp;&nbsp;&nbsp; row.ingress:setFillColor( colorConverter(114, 72, 30, 1) )

&nbsp;&nbsp;&nbsp; row.image = display.newImageRect(row, tostring(imagePath … dbTable[row.index].NAME) … “.jpg”, 45,45 )  
&nbsp;&nbsp;&nbsp; row.image:addEventListener( “touch”, row.image)  
&nbsp;&nbsp;&nbsp; row.image.touch = birdSpec  
&nbsp;&nbsp;&nbsp; row.image.anchorX = 1  
&nbsp;&nbsp;&nbsp; row.image.anchorY = 0.25  
&nbsp;&nbsp;&nbsp; row.image.x = display.viewableContentWidth - row.image.width/2  
&nbsp;&nbsp;&nbsp; row.image.y = row.title.y

&nbsp;&nbsp;&nbsp; – PARSE VARIABLE TO THE GLOBAL SPECPOOL TABLE  
&nbsp;&nbsp;&nbsp; row.image.specName = dbTable[row.index].NAME  
&nbsp;&nbsp;&nbsp; row.image.specLatin = dbTable[row.index].LATIN  
&nbsp;&nbsp;&nbsp; row.image.specInfo = dbTable[row.index].INFO

end

– “scene:create()”  
function scene:create( event )  
&nbsp;&nbsp; local sceneGroup = self.view

&nbsp;&nbsp; --Bird.group = Bird.group  
&nbsp;&nbsp;&nbsp; bg = display.newImageRect( bgImage, \_W, \_H, 375, 667, true )  
&nbsp;&nbsp;&nbsp; bg.anchorX, bg.anchorY = 0, 0  
&nbsp;&nbsp;&nbsp; bg.alpha = 1  
&nbsp;&nbsp;&nbsp; sceneGroup:insert(bg)

&nbsp;&nbsp;&nbsp; leftRib = display.newImageRect( “leftRib.png”, 48, 793, true )  
&nbsp;&nbsp;&nbsp; leftRib.anchorX, leftRib.anchorY = 0, 0  
&nbsp;&nbsp;&nbsp; leftRib.alpha = 1  
&nbsp;&nbsp;&nbsp; sceneGroup:insert(leftRib)

&nbsp;&nbsp;&nbsp; woodPanel = display.newImageRect( “woodPanel.png”, 375, 131, true )  
&nbsp;&nbsp;&nbsp; woodPanel.x, woodPanel.y = display.contentWidth/2, display.contentHeight  
&nbsp;&nbsp;&nbsp; woodPanel.anchorX, woodPanel.anchorY = 0.5, 1  
&nbsp;&nbsp;&nbsp; woodPanel.alpha = 1  
&nbsp;&nbsp;&nbsp; sceneGroup:insert(woodPanel)

&nbsp;&nbsp;&nbsp; themeText = display.newText( tostring(\_G.catText), 0, 0, \_G.msgFont , 18 )  
&nbsp;&nbsp;&nbsp; themeText.anchorX, themeText.anchorY = 0, 0  
&nbsp;&nbsp;&nbsp; themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 30  
&nbsp;&nbsp;&nbsp; themeText:setFillColor( colorConverter(236, 210, 135, 1) )  
&nbsp;&nbsp;&nbsp; sceneGroup:insert(themeText)  
end

– “scene:show()”  
function scene:show( event )  
&nbsp;&nbsp; local sceneGroup = self.view  
&nbsp;&nbsp; local phase = event.phase  
&nbsp;&nbsp; if ( phase == “will” ) then  
&nbsp;&nbsp; composer.removeHidden( )&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp; elseif ( phase == “did” ) then

– load the data into a table array

– WHEN I FIRE IT UP THE FIRST TIME IT WILL SHOW ALL ENTRIES  
if sql == nil then sql = “SELECT \* FROM birds” print(“Initiated SQL \*”) end

&nbsp;&nbsp;&nbsp; count = 0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for row in db:nrows(sql) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = count +1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count]={}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].NAME = row.NAME  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].LATIN = row.LATIN  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbTable[count].INFO = row.INFO  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end

&nbsp;&nbsp;&nbsp; tableView = widget.newTableView  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; top = 0,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width = \_W+1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; height = display.contentHeight,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hideBackground = true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; isBounceEnabled = true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listener = tableViewListener,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onRowRender = onRowRender,  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; for i = 1, #dbTable do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local isCategory = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowHeight = 70  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local rowColor = {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default = { colorConverter(114, 72, 30, 0) },  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local lineColor = { colorConverter(20, 20, 0, 0) }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; – Insert the row into the tableView  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tableView:insertRow  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; –&nbsp; isCategory = isCategory,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowHeight = rowHeight,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rowColor = rowColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lineColor = lineColor,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; end  
&nbsp;&nbsp;&nbsp; sceneGroup:insert(tableView)

&nbsp; end

end

– “scene:hide()”  
function scene:hide( event )  
&nbsp;&nbsp; local sceneGroup = self.view  
&nbsp;&nbsp; local phase = event.phase  
&nbsp;&nbsp; if ( phase == “will” ) then  
&nbsp;&nbsp; elseif ( phase == “did” ) then  
if tableView and tableView:getNumRows()\>1 then tableView:deleteAllRows()  
&nbsp; print(“TableView iterated and deleted from scene hide in playGame”)  
end

\_G.catText = “”  
updateCatText()

&nbsp;&nbsp; end  
end

function scene:destroy( event )  
local sceneGroup = self.view

end

– Listener setup  
scene:addEventListener( “create”, scene )  
scene:addEventListener( “show”, scene )  
scene:addEventListener( “hide”, scene )  
scene:addEventListener( “destroy”, scene )

return scene

[/LUA]

Hope anyone can help me

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [October 31, 2015, 8:09pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/7 "2015-10-31T20:09:47Z")

</div>

I understand the problem now. I, however; don’t see where your creating your buttons and populating mySqlView. Though I think I have enough here to answer the basic question.

You’re over complicating your “functionSqlView” function. I would drop all the table stuff and just deal with strings. Lets start with the assumption that your buttons can produce three pieces of information:&nbsp;&nbsp; Key (“WING”, “TAIL”, etc.), Value (“Long”, “Short”, etc.) and condition (“AND”, “OR”, nil). I would create a table call attributes and for each entry, have it contain these three values:

local birdAttributesQuery = {}

birdAttributesQuery[#birdAttributesQuery + 1] = { key = “WING”, value = “Long”, condition = “AND” },

birdAttributesQuery[#birdAttributesQuery + 1] = { key = “TAIL”, value = “Short”, condition = nil }

The #birdAttributesQuery + 1 will auto increment the table.

Then you could build your functionSqlView() to do:

```
local function functionSqlView() &nbsp;&nbsp;&nbsp; local query = "SELECT \* FROM birds WHERE" &nbsp;&nbsp;&nbsp; for i = 1, #birdAttributesQuery do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query = query .. " " .. birdsAttributesQuery[i].key .. " = " .. birdAttributesQuery[i].value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if birdAttributesQuery[i].condition then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; query = query .. " " .. birdAttributesQuery[i].condition &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end -- -- now execute your query -- delete the previous tableView rows -- loop over the returned query --&nbsp;&nbsp;&nbsp;&nbsp; repopulate the table values you use to populate the tableView row in onRowRender() --&nbsp;&nbsp;&nbsp;&nbsp; insert a new tableView row (triggering the onRowRender) -- end end

```

Your onRowRender() and a bunch of your functions should come above this in your code (so they are forwardly declared properly to help with the globals…

Rob

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 31, 2015, 8:59pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/8 "2015-10-31T20:59:47Z")

</div>

Thanx for the answer Rob  
I have no trouble with the building and executing sql queries. My trouble is how I remove the rows that does NOT meet my criterias and how they will be removed from the display and the respective table. I have been reading mouch about widgets and making this work in a composer environment like I want. If I´m thinking loud here I like to think like this:  
I iterate through the db file in ipairs with the sql query string as argument - this gives me the rows that meet the criterias that I ask for  
NOW: How do I remove the ones that shouldn´t be there?  
I have really tried for some time now in composer ( I see that other coders like Ingemar use api calls that I cant find in the api list, why?)

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [October 31, 2015, 9:06pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/9 "2015-10-31T21:06:48Z")

</div>

I think this is were I’m having a disconnect. You remove all the rows from the tableview. This gives you an empty tableView.&nbsp; Then if your SQL query returns just the rows you want, insert the rows into the tableView using the data returned from the query.

If you see Ingemar use API calls that you can’t find, that means he’s using some library either of his creation or of someone else. He could also have taken the open source version of our widgets and modified them with extra features.

Rob

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [October 31, 2015, 9:21pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/10 "2015-10-31T21:21:53Z")

</div>

This is JUST what im having trouble doing Rob, I have the query string, I have deleted all the rows, but I cant populate with the new query. It wont work for me ☹ What and where do I put what you sugest in your most recent post?

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [October 31, 2015, 11:10pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/11 "2015-10-31T23:10:34Z")

</div>

Maybe something like:  
&nbsp;

```
local function functionSqlView() local query = "SELECT \* FROM birds WHERE" for i = 1, #birdAttributesQuery do query = query .. " " .. birdsAttributesQuery[i].key .. " = " .. birdAttributesQuery[i].value if birdAttributesQuery[i].condition then query = query .. " " .. birdAttributesQuery[i].condition end end tableView:deleteAllRows() -- FIRST I LIKE TO REMOVE THE ROWS AND THAT WORKS OF COURSE count = 0 dbTable = {} for row in db:nrows( query ) do count = count +1 dbTable[count]={} dbTable[count].NAME = row.NAME dbTable[count].LATIN = row.LATIN dbTable[count].INFO = row.INFO local isCategory = false local rowHeight = 70 local rowColor = { default = { colorConverter(114, 72, 30, 0) }, } tableView:insertRow { -- isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, } end end
```

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [November 1, 2015, 7:10pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/12 "2015-11-01T19:10:07Z")

</div>

I try the new code and I must say that I like the auto increment tweak you did on&nbsp;birdAttributesQuery and that you define the value so clear as you did here Rob! Mine works to but Its a littlest more “foggy” haha 🙂

but I just get no rows at all, is this because I have to force onRowrender to render? (My debug system tells me that it do not trigger onRowrender after I click.

What is the best practice on forcing row rendering?

It stopps just after deleting the rows and nothing more happens

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [November 1, 2015, 8:49pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/13 "2015-11-01T20:49:06Z")

</div>

The on screen rows should auto render when they are inserted. any thing going on in your logs?

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [November 1, 2015, 9:16pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/14 "2015-11-01T21:16:08Z")

</div>

Nothing going on in the logs, uhh, this drives me nuts really! man. I cant get this to work at all. It all stops when I try to update the rows.

I sendt you a zip here the other day, can you please see if its just me or a bug or something?

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [November 1, 2015, 9:44pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/15 "2015-11-01T21:44:08Z")

</div>

Can you zip up your latest with the suggestions I made above?

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [November 2, 2015, 11:30am UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/16 "2015-11-02T11:30:09Z")

</div>

Heres the link: www.ferrobet.no/NatureApp.zip

I have adjusted the content so it matches the db file 🙂

The way I made it is like the class:&nbsp;ButtonsBirds.lua -(inside the Classes folder), inserts the button args into the table mySqlView trough the function&nbsp;engineResponse() --this can be like if I press the Red bodycolour button it will send&nbsp;“BODYCOLOUR = ‘Red’” or in your way: table.insert(birdAttributesQuery, unpack({ key = “ART”, value = “Andefamilien”, condition = “AND” })) , right?

OK, I will be extremely glad if you can help with this “little thing” that stops my whole project Rob 🙂

Thanx for all help

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [November 3, 2015, 1:25pm UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/17 "2015-11-03T13:25:48Z")

</div>

Rob

Did you find anything?

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [November 4, 2015, 1:24am UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/18 "2015-11-04T01:24:00Z")

</div>

I’ve not gotten to it yet. With any luck I can look at it tonight.

Rob

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [November 4, 2015, 4:10am UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/19 "2015-11-04T04:10:47Z")

</div>

Your app comes up with a black screen and no errors in the console.&nbsp; Can I get working project?

Rob

---

<div class="post-metadata">

**Author:** ![Hendrix000007](https://forums.solar2d.com/user_avatar/forums.solar2d.com/hendrix000007/32/1396_2.png) [@Hendrix000007](https://forums.solar2d.com/u/Hendrix000007)\
**Post date:** [November 4, 2015, 10:50am UTC](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333/20 "2015-11-04T10:50:28Z")

</div>

Ahh, sorry Rob I uploaded a wrong version

Heres the link: www.ferrobet.no/NatureApp.zip

This is not with your piece of code because the function functionSqlView() produces the right statements when you click one of the buttons at the bottom. I have been reading one of your replies in another forum post where you discuss where to put the different components of the tableView structure when dealing with composer and going back and forth through composer pages/scenes.

So I rearranged my code and took the onRowrender function from will show over to create scene then it started to re-render my rows again 🙂

Anyway, I think my real q here is: how do I sort and show the data I now have to the display?

I think the problem lies at **line 61** in playGame file

When this is sorted out, I will use your approach on the functionSqlView function

…I liked it ;D

[Next page](https://forums.solar2d.com/t/filtering-the-tableview-with-args-from-sql/336333.md?page=2)
