Hello:
So, I am trying to set-up a database that has four tables. I need to get the row IDs that connects the tables together. I can create the tables and insert values them as followed:
[lua]-- Create tables
local path = system.pathForFile(“myPlayBook.db”, system.DocumentsDirectory)
db = sqlite3.open( path )
local tablesetup = [[CREATE TABLE IF NOT EXISTS Play (ID INTEGER PRIMARY KEY AUTOINCREMENT, playName TEXT, Category TEXT);]]
print(tablesetup)
db:exec( tablesetup )
local tablesetup = [[CREATE TABLE IF NOT EXISTS Player (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Team TEXT, X INTEGER, Y INTEGER, Routed BOOLEAN, PlayID INTEGER);]]
print(tablesetup)
db:exec( tablesetup )
local tablesetup = [[CREATE TABLE IF NOT EXISTS Route (ID INTEGER PRIMARY KEY AUTOINCREMENT, PlayerID INTEGER, lineType TEXT, routeTip TEXT, tipX INTEGER, tipY INTEGER, tipA INTEGER, PlayID INTEGER);]]
print(tablesetup)
db:exec( tablesetup )
local tablesetup = [[CREATE TABLE IF NOT EXISTS routePoints (ID INTEGER PRIMARY KEY AUTOINCREMENT, pointNum INTEGER, X INTEGER, Y INTEGER, RouteID INTEGER, PlayID INTEGER);]]
print(tablesetup)
db:exec( tablesetup )
– Inserted Values
db:exec([[INSERT INTO Play VALUES (NULL, ‘]]…“34-Defense1”…[[’,’]]… categorySeletion… [[’);’]])
print(db:exec(“SELECT MAX(ID) FROM Play”)) – RETURNS 0
WHY???
for i=1, playersGroup.numChildren do
local iPlayer = playersGroup[i]
if iPlayer.routed == true then
db:exec([[INSERT INTO Player VALUES (NULL, ‘]] … iPlayer.player …[[’,’]]… iPlayer.team …[[’,’]]…iPlayer.x …[[’,’]]… iPlayer.y …[[’,’]]…‘true’…[[’,’]]…db:exec(“SELECT MAX(ID) FROM Play”)…[[’);’]])
db:exec([[INSERT INTO Route VALUES (NULL, ‘]] … db:exec(“SELECT LAST(Player) AS LastID FROM Player”) …[[’,’]]… iPlayer.lineType …[[’,’]]… iPlayer.tip …[[’,’]]… iPlayer.tipX …[[’,’]]… iPlayer.tipY …[[’,’]]… iPlayer.tipA …[[’,’]]…db:exec(“SELECT MAX(ID) FROM Play”)…[[’);’]])
for w = 1, #iPlayer.points do
db:exec([[INSERT INTO routePoints VALUES (NULL, ‘]] … w …[[’,’]]… iPlayer.points[w].x …[[’,’]]… iPlayer.points[w].y …[[’,’]]… db:exec(“SELECT LAST(Route) AS LastID FROM Route”) …[[’,’]]… db:exec(“SELECT MAX(ID) FROM Play”)…[[’);’]])
end
else
db:exec([[INSERT INTO Player VALUES (NULL, ‘]] … iPlayer.player …[[’,’]]… iPlayer.team …[[’,’]]… iPlayer.x …[[’,’]]… iPlayer.y …[[’,’]]… ‘false’ …[[’,’]]… db:exec(“SELECT MAX(ID) FROM Play”)…[[’);’]])
end
end[/lua]
My problem is inserting the reference to the ID of Play to Player in PlayID , ID of Player and ID of Play to Route, etc…
I am able get the values of everything else by:
[lua] --debug only
for row in db:nrows(“SELECT * FROM Play ORDER BY ID DESC LIMIT 0”) do
print( row.ID, row.playName, row.Category)
end
print(db:exec(“SELECT * FROM Play ORDER BY ID DESC LIMIT 0”))
–print(db:last_insert_rowid(“SELECT * FROM Play”))
–[[
print(db:exec(“SELECT * FROM Play ORDER BY ID DESC LIMIT 0”))
print(db:exec(“SELECT LAST(Play) AS ID FROM Play”))
for row in db:nrows(“SELECT * FROM Player”) do
– I can get row.whatever
–print(row.Routed, row.PlayID)
end
for row in db:nrows(“SELECT * FROM Route”) do
– I can get row.whatever
–print(row.PlayerID, row.lineType)
end
for row in db:nrows(“SELECT * FROM routePoints”) do
– I can get row.whatever
–print(row.RouteID, row.PlayID)
end
–]][/lua]
I’ve tried every function I can think of the get the max Row ID to no avail.
Please Help.
Thanks,
Willy J.
[import]uid: 66859 topic_id: 12954 reply_id: 312954[/import]