I've made some progress. Here's what I've discovered-- for other newbies.
1- The Try/Catch control statement works great for catching unrecoverable errors.
2- The
put revOpenDatabase("sqlite", pathNdb,,,,,,) into gConID must use
the put into clause, because the connection id is needed to reference
the database in all future requests.
2- But the
revExecuteSQL gConID, tSQL statement-- for some reason-- cannot use the put into clause
anymore, as originally indicated in the SQLite Samplier.rev example. So
the result much be checked another way.
For instance, trying to create a database that already exists, will not generate an Try/Catch Error.
This works very well though:
if the result is not 0 then answer warning the result; exit doCreate
This generates a "Table already exits" error. Notice-- there are two statements on one line separated by a semicolon.
3- Also, be aware that the
gConID is only a numeric connection ID. It does not indicate the state or open/closed-ness of the DB.
There's probably is some way to check the state of a DB connection, though I haven't seen that yet.
4- Here's a little oddity.
the Result returns zero, as a numeric if everything is okay.
But if there's a problem,
the Result returns a string with the description of the problem.
Usually a variable is numeric or a string-- not both. I suspect this aspect of variables is common throughout Live Code.
While you can use Global or Local to define the range of a variable, I have yet to see a variable defined
specifically as a integer, long, real, or string. Maybe it's not necessary.
Here's the code I've got so far. I think-- in many ways-- it's better than the original.
Code: Select all
global gConID
local tSQL
on doConnect
try
put the defaultFolder & "/mytest.db" into pathNdb
put revOpenDatabase("sqlite", pathNdb,,,,,,) into gConID
catch theError
answer info "Problem Opening Database: " & pathNdb & " " & theError
put 0 into gConID
exit doConnect
end try
answer information "Connected to: " & pathNdb & " ID is: " & gConID
end doConnect ----------------------------------------------------
on doDisconnect
if gConID is zero then
answer info "Press the Connect Button First."
exit doDisconnect
end if
try
revCloseDatabase gConID
catch theError
answer warning theError
exit doDisconnect
end try
put 0 into gConID
answer info "Connection Terminated "
end doDisconnect ------------------------------------------------------
on doCreate
if gConID is 0 then
answer info "Press the Connect button first."
exit doCreate
end if
try
put "CREATE TABLE users(userID integer primary key, name text,email text, emailList boolean)" into tSQL
revExecuteSQL gConID, tSQL
if the result is not 0 then answer warning the result; exit doCreate
catch theError
answer warning theError
exit doCreate
end try
answer information "Table Created Sussessfully"
end doCreate --------------------------------------------------------
-- For successful queries, the revExecuteSQL command returns
-- the # of rows affected for INSERT, UPDATE and DELETE statements.
-- For all other statements, 0 is returned, so if the result is 0... then everything is good
Next I will use the revExecuteSQL to Insert random data into the Table.
Then I'll read that back with a revQuerySQL and populate a DataGrid.