Quote:
Lar_123, could you put the code or some file with the publication of the most frequently used functions SQLite for display list of data, insert of new records, change, delete, query on the condition?
A few months ago I posted a 'Getting Started w SQLite' example (Mackavi hosted the download). At that time I did not have INSERT or UPDATE function working.
That sample Pub had some 'universal' queries built-in. Which was fine for reading from the DB. But I had difficulty
editing the DB via 'universal' SQL statements. The point being, the UPDATE or INSERT statements are better done specific to your dataset and current scenario.
Some hints:
-- you'll want the "Getting Started" pubs previously posted, to use the following
-- use the Firefox plugin, SQLite Manager. Under its "Execute SQL" feature it is easy to construct and test an INSERT statement... etc.
-- then Copy & Paste that SQL statement into your OpusScript... and there you
modify it with Variables to pick-up the values you need.
Code:
INSERT INTO DBtable1 VALUES( NULL,'James','Nautical','Docking','7842','Sunnyburg')
Code:
var NewRecordSQL = "" ;
NewRecordSQL = "INSERT INTO "+ TableVariable +" VALUES("+anArray1ofVALUES+")" ;
//next let's send the above SQL Statement to Test2, which is a copy of the function Test() (found in ScriptObject TestSQLite_1)
//Test2 should be modified to take a parameter/arg which is the SQL Statement string
Test2( NewRecordSQL )
There is a 'catch' to making the above work. SQLite likes VALUES to be in quotemarks. (except when Primary Key is Integer).
so...
before the Insert or Update is sent to SQLite, we prepare Opus data to be SQL 'values'
Code:
RecINSERT_VALUES[0] = 'NULL' ; //assumes Autoincrementing PRI KEY. Do NOT do this if have Text or other key.
//next series is changed 010609 to start with i=1
for ( var i = 1 ; i < NumberofColumns.length ; i++ )
{
RecINSERT_VALUES[i] = "'"+NewRecord_Data[i]+"'" ;
//now we convert any Inputing VALUES which are Blank, thus 'undefined', to NULL
if ( RecINSERT_VALUES[i] == "'undefined'" )
{
RecINSERT_VALUES[i] = 'NULL' ;
}
}
then you may or may not need to run a Loop to ravel the Array (I use the loop to do other things here as well):
Code:
anArray1ofVALUES[ii]+= ", "+RecINSERT_VALUES[ii] ;
For Updating, start with something simple and test as suggested above. When you want to script it... here is somewhat of a template (again, I had certain things in mind when I constructed this... there ARE simpler ways)
Code:
//Records_Array1 is an Array returned from a current Query SELECT statement. It is two-dimensional... rows and columns. It contains what we're changing and then Updating
//This can be much simplified by hardcoding with Names of Columns and the Changed-Value (but you give up some flexibility)
var usethisSQL = "UPDATE " +TableVariable1+ " SET " +Records_Array1[0][currindex ]+ " = "+"'" + Records_Array1[ EDITEDrecord ][currindex +1] + "'"+ " WHERE ( " + ConstraintFIELD + " = '"+ConstraintVALUE+"' );" ;
//note: I sometimes keep my "'" separate and visible so I do not 'lose' them
//=====================================================
Test2( usethisSQL ) ;
In case anyone has gotten lost trying to follow this, the SQLite demonstration .imp has 3 Script Objects included which do the 'behind the scenes' work of using SQLite DB with Opus. The ScriptObject "TextSQLite_1" or similar named script is a convenient place to create "function Test2()" noted above. I would suggest separate ScriptObject to create these Opus functions:
function dbINSERT1(args)
function dbUPDATE1(args) etc.
and build-in your variables there, including SQL Queries as constructed in examples above... make your function calls to "Test2(arg)" from there. There is a lot of power here... since data read from the Database is placed in a volatile Array named "Rows"... which you can make persistent by making Records_Array1 = Rows ; //(or similar named Array variable)
That's what you are operating on in Opus.
added Hint:
-- when you execute a DB UPDATE or INSERT statement from an Opus Pub, you can immediately go over to SQLite Manager ( Alt+TAB to Firefox/sqlite_manager) and HIT the REFRESH button.... you will see your changes to the Database in the "Browse & Search" Tab in SQLite Manager.
Good cheer.
Lar