SQLite - cross reference search
Posted: Mon Apr 27, 2015 7:49 pm
Could use some help with some SQLite code.
I have a number of objects (x) that are consumed by another object (y) in the DB. The names of each object, x and y, are unique. All x objects are in one table and all y objects are in another table. Let’s call the tables xTable and yTable. Any x object can be consumed or occur in any y object but only once in each y object. What I need to accomplish is get a list of the y objects that have consumed a x object.
Here’s a simple scenario of what I’m trying to accomplish. I’ll use fruit and fruit baskets to explain. Let’s say I have a bunch of different fruit and I was to tell what fruit has been added to what basket.
I have a table called “Baskets” and a table called “Fruit”. The user will be defining the names of the baskets and the names of the fruit by text entry.
I need to search “Banana” and get a list of the baskets that have Bananas in them. I have a another table called CrossRef which is empty except for a “RefID” column. The columns are named for each fruit object and the basket names are stored in the corresponding fruit column.
When the users creates a fruit object.
When the user does the search for the baskets, this is what I have so far to get the list of baskets.
If anyone has suggestions or if you think of a better way to handle this please let me know.
I have a number of objects (x) that are consumed by another object (y) in the DB. The names of each object, x and y, are unique. All x objects are in one table and all y objects are in another table. Let’s call the tables xTable and yTable. Any x object can be consumed or occur in any y object but only once in each y object. What I need to accomplish is get a list of the y objects that have consumed a x object.
Here’s a simple scenario of what I’m trying to accomplish. I’ll use fruit and fruit baskets to explain. Let’s say I have a bunch of different fruit and I was to tell what fruit has been added to what basket.
I have a table called “Baskets” and a table called “Fruit”. The user will be defining the names of the baskets and the names of the fruit by text entry.
I need to search “Banana” and get a list of the baskets that have Bananas in them. I have a another table called CrossRef which is empty except for a “RefID” column. The columns are named for each fruit object and the basket names are stored in the corresponding fruit column.
When the users creates a fruit object.
Code: Select all
## Because columns cannot contain spaces.
replace space with "_" in pFruit_Name
## Add the name to the CrossRef Table
put "ALTER TABLE CrossRef ADD '"&pFruit_Name&"' varchar(255)" into tSQLStatement
revExecuteSQL sDatabaseID,tSQLStatement
Code: Select all
## Populate the variable with the name of the fruit
put item 1 of tData into tThisFruit
## Because columns cannot contain spaces
replace space with "_" in tThisFruit
## This does not return the list. If I replace ‘"&tThisFruit&"' with the column name it works fine
put "SELECT '"&tThisFruit&"' FROM CrossRef" into tSQLStatement2
answer tSQLStatement2
put revDataFromQuery(tab,return,sDatabaseID,tSQLStatement2) into tData2
filter tData2 without empty
answer tData2