• Welcome to SQLitening Support Forum.
 

News:

Welcome to the SQLitening support forums!

Main Menu

slBuildInsertOrUpdate and Update

Started by Fredrick Ughimi, June 26, 2009, 11:53:27 AM

Previous topic - Next topic

Fredrick Ughimi

Hello,

Been playing around SQLitening.

I want to update (Modify) a record based on the value of a textbox. In this case sUsername.

What is the correct syntax for updating a records using slBuildInsertOrUpdate and Update. I used the following:


slExe slBuildInsertOrUpdate("tblCreateUsers", sUsername & $Nul & sPassword & $Nul & sStatus, "Username = '" + sUsername + " ' ")


I got syntax error.in "="


slExe "Update tblCreateUsers Set Username = sUsername, Password = sPassword, Status = sStatus Where Username='" + sUsername + "'"


I got column not found in table error.

I guess I am missing something?  ???
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Rolf Brandt

#1
Hi Frederick,

These are two small examples to save a new record and an existing modified record.

'Create a new record
Function SaveNewRec()As Long
Local fstr As String 'the fields string
Local vstr As String 'the values string

fStr = "MANUF,REDREF,PRODUCT,TYPE"
vStr = "MyCompany" & $NUL & "'003214523'" & $NUL & _
"MyProduct" & $NUL & "MyType"

slExe slBuildInsertOrUpdate ("Parts", vstr, fStr)
End Function   

'Update an existing record
Function UpdateRec()As Long
Local fstr As String 'the fields string
Local vstr As String 'the values string
Local wstr As String 'the where string

fStr = "MANUF,REDREF,PRODUCT,TYPE"
vStr = "MyCompany" & $NUL & "'003214523'" & $NUL & _
"MyProduct" & $NUL & "MyType"
wStr = "rowid = 123"

slExe slBuildInsertOrUpdate ("Parts", vstr, fStr, wStr)
End Function     


Regards
Rolf
I like to cook with wine - sometimes I even add it to the food.
www.rbsoft.eu

Fredrick Ughimi

Hello Rolf,

That was fast.

Umm... what is really wrong with my codes?

Thank you.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

Rolf,

I see in your example you are using the rowid.
You know in the real world you might, after a search operation want to use the value of a textbox to modify the record Like:



sUsername = vd_gettext(ncbhndl, TextBoxID)
slExe slBuildInsertOrUpdate("tblCreateUsers", sUsername & $Nul & sPassword & $Nul & sStatus, "Username = '" + sUsername + " ' ")



The syntax for using the rowid would be different.

Kind regards,

Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Rolf Brandt

Frederick,

there is no problem, you can do that. Important is that if you want to update an existing record you must use the WhereSring. If you omit the WhereString a new record will be inserted.

Also if you are not putting values into all fields of the table then you must use the ColumnsString, and it must match the ValuesString. If you are using all fields then you can omit the ColumnsString, but you must put a comma as placeholder. (see example)

See extra comma!!! Very important!

slExe slBuildInsertOrUpdate("tblCreateUsers", sUsername & $Nul & sPassword & $Nul & sStatus, , "Username = '" + sUsername + " ' ")
I like to cook with wine - sometimes I even add it to the food.
www.rbsoft.eu

David Kenny

#5
Fredrick,

The slBuildInsertOrUpdate line you posted doesn't have the column names for the fields you are specifying.
Excerpt from the SQLitening doc:
QuoteColumn names are optional for Insert but required for Update

So, I think the syntax error is from the "=" in the next parameter (the rsWhere string in the documentation).
Here is how I think it should be:

slExe slBuildInsertOrUpdate("tblCreateUsers", sUsername & $Nul & sPassword & $Nul & sStatus, "UserName,Password,Status", "Username = '" + sUsername + " ' ")


In the second code block you are sending three variable names in the SQL statement, when you should be sending the contents of the variables.  Just as you correctly inserted the sUsername into the where clause, you should do the same for each of the columns as such:
slExe "Update tblCreateUsers Set Username = '" + sUsername + "', Password = '" + sPassword + "', Status = '" + sStatus + "' Where Username='" + sUsername + "'"

Now, in both cases, you are using the where clause to make sure the update only happens to records where the Username field is equal to the one from your Textbox.  So, the 'set Username = sUsername is not necessary.  It shouldn't fail however.  Here is how I think both should look:
slExe slBuildInsertOrUpdate("tblCreateUsers", sPassword & $Nul & sStatus, "Password,Status", "Username = '" + sUsername + " ' ")

slExe "Update tblCreateUsers Set Password = '" + sPassword + "', Status = '" + sStatus + "' Where Username='" + sUsername + "'"


Regards,

David

Ah, I see I cross posted with Rolf.  Rolf, you are correct if he is doing an Insert.  But I believe his two different code blocks were attempts to solve the same problem, and the second code block is clearly an update. Therefore, the parameter cannot be omitted.

Rolf Brandt

You are right, David. It says in Fred's documentation:
"Column names are optional for Insert but required for Update."

Rolf
I like to cook with wine - sometimes I even add it to the food.
www.rbsoft.eu

Fredrick Ughimi

Hello,

Thank you Rolf and David.

>>Column names are optional for Insert but required for Update

This makes all the difference when I am updating all the fields. I need to read the documentation more. :)

>>See extra comma!!! Very important!

Noted. This would work if I am updating just a field.

Best regards,


Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Andrew Lindsay

OK, I need some help here, and I can't quite grasp what the manual is saying.

I have 4 different CSV files, all in the format of

DateTime, Real.

They contain data every second for Tension, Pitch Roll and Heave, so each idividual file would be a series of entries as such-


Tension.CSV

RecordTime   Tension
2009-01-01 00:00:0073
2009-01-01 00:00:0167
2009-01-01 00:00:0353
2009-01-01 00:00:0472
2009-01-01 00:00:0590
2009-01-01 00:00:0877
2009-01-01 00:00:0947
2009-01-01 00:00:1019
2009-01-01 00:00:1190
2009-01-01 00:00:1378
2009-01-01 00:00:1482
2009-01-01 00:00:1550
2009-01-01 00:00:1721
2009-01-01 00:00:1884
   

Pitch.CSV

RecordTimePitch
39814.00001157410.635480334372697
39814.00003472220.651912825162737
39814.00004629630.960518554952992
39814.00005787040.0918474916201155
39814.00006944450.319918163823353
39814.00008101850.941540053614541
39814.00009259260.746957004452512
39814.00011574080.907714781997423
39814.00012731480.825096233881558
39814.00013888890.623002920037419
39814.0001504630.0988242513928097
39814.00018518520.423528689520101
39814.00019675930.164773031247083


Roll.CSV

RecordTimeRoll
398140.226449782983676
39814.00003472220.508779557441737
39814.00005787040.0975702516256709
39814.00008101850.695856147544947
39814.00010416670.554725874769032
39814.00011574080.0694166052752716
39814.00013888890.0500894191504688
39814.00017361120.686395584500251
39814.00018518520.106952609077364
39814.00019675930.7232638244345
39814.00020833340.874211940270543



Heave.CSV

RecordTimeHeave
398140.533813121206404
39814.00001157410.550086162941062
39814.00002314820.824738836166552
39814.00003472220.328173923893668
39814.00004629630.390095363264371
39814.00008101850.752438962289054
39814.00009259260.192342174956707
39814.00010416670.601769796791728
39814.00011574080.444666057148828
39814.00012731480.310802052318191
39814.00013888890.0131939787769038
39814.00016203710.285008667883267
39814.00017361120.526707486478498
39814.00019675930.887415995622519
39814.00020833340.432657835526106


For a combined table which would look like


RecordTimeTensionPitchRollHeave
3981473$NULL0.2264497829836760.533813121206404
39814.0000115741670.635480334372697$NULL0.550086162941062
39814.0000231482$NULL$NULL$NULL0.824738836166552
39814.0000347222530.6519128251627370.5087795574417370.328173923893668
39814.0000462963720.960518554952992$NULL0.390095363264371
39814.0000578704900.09184749162011550.0975702516256709$NULL
39814.0000694445$NULL0.319918163823353$NULL$NULL
39814.0000810185$NULL0.9415400536145410.6958561475449470.752438962289054
39814.0000925926770.746957004452512$NULL0.192342174956707
39814.000104166747$NULL0.5547258747690320.601769796791728
39814.0001157408190.9077147819974230.06941660527527160.444666057148828
39814.0001273148900.825096233881558$NULL0.310802052318191
39814.0001388889$NULL0.6230029200374190.05008941915046880.0131939787769038
39814.000150463780.0988242513928097$NULL$NULL
39814.000162037182$NULL$NULL0.285008667883267
39814.000173611250$NULL0.6863955845002510.526707486478498
39814.0001851852$NULL0.4235286895201010.106952609077364$NULL
39814.0001967593210.1647730312470830.72326382443450.887415995622519
39814.000208333484$NULL0.8742119402705430.432657835526106


Any assistance would be appreciated.

Regards

Andrew

Fred Meier

#9
Below are the slBuildInsertOrUpdate statements for your first two records.

slExe slBuildInsertOrUpdate("TableName", "39814" & $NUL & _
                                         "73" & $NUL & _
                                         "Null"  & $NUL & _
                                         ".226449782983676"  & $NUL & _
                                         ".533813121206404")

slExe slBuildInsertOrUpdate("TableName", "39814.0000115741" & $NUL & _
                                         "67" & $NUL & _
                                         ".635480334372697"  & $NUL & _
                                         "Null"  & $NUL & _
                                         ".550086162941062")


Of course you would want to user variables and some kind of loop as shown below.
The GetTension, GetPitch, GetRoll, and GetHeave functions would return the proper
value for the passed RecordTime or "Null" if none.


   Local lsRecordTime as String

   do
      lsRecordTime = GetRecordTime
      if isfalse len(lsRecordTime) then exit do
      slExe slBuildInsertOrUpdate("TableName", lsRecordTime & $NUL & _
                                               GetTension(lsRecordTime) "67" & $NUL & _
                                               GetPitch(lsRecordTime)  & $NUL & _
                                               GetRoll(lsRecordTime)  & $NUL & _
                                               GetHeave(lsRecordTime))

   loop

Andrew Lindsay

Fred,

Do you have an example of a prepared SQLite statement, using 'placeholders'?

Regards

Andrew

Fredrick Ughimi

Fred,

Is it slBuildInsertOrUpdate or slBuildInsertOrDelete?

May be a new function?
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fred Meier

Andrew

Not sure what you mean by 'placeholders'?  If you are referring
to the ability to insert/update binary data then Example C has sample code
using slExeBind along with slBuildBindDat. 

Note that I discovered a doc error in the Sub BindAndQuery in Example C
(will be fixed in next release).  It should be as follows:

Sub BindAndQuery
' The boss has decided to store a memo in the customer table. This memo
' can be very long and is confidential so we must compress and encrypt.
' This example will use the Alter Table and Bind statements.

' The syntax and doc for slExeBind is:
'  slExeBind  (rsStatement String, rsBindDats String, [rsModChars String]) Long
'     Statement contains one (no multiples like slExe) Insert or Update
'     statement.  Will replace all of the '?' expressions in Statement with the
'     corresponding BindDat entry passed in BindDats.  A BindDat entry is the
'     specially formated string returned by the slBuildBindDat function.  Pass
'     BindDats as multiply concatenated BindDat entries to handle multiple '?'
'     expressions.  The first '?' will be replaced with the first BindDat entry,
'     the second '?' with the second BindDat entry, etc.  Excess '?' expressions
'     will be set to Null while excess BindDat entries will rais an error.  The
'     following example will insert three columns as follows:
'       ColName1 = Blob, ColName2 = Compressed Text, ColName3 = Compressed Encrypted Blob.
'         slExeBind slBuildInsertOrUpdate("TableName", _
'                                         "?" & $NUL & "?" & $NUL & "?" & $NUL, _
'                                          "ColName1, ColName2, ColName3"), _
'                                          slBuildBindDat("This is some Blob data") & _
'                                          slBuildBindDat("This is some copressed Text", "TC") & _
'                                          slBuildBindDat("This is a copressed and encrypted Blob", "CE")
'     ModChars:
'        Em = Return errors. This will override the global return errors flag.
'             m is the optional message display modifier and can be:
'                0 = No message is displayed.  This is the default.
'                1 = Display a warning message with OK button.  Error is
'                    returned when OK pressed.
'                2 = Display a question message with OK and Cancel buttons.
'                    If they press OK, error is returned.  If they press
'                    Cancel, will exit process.
'        e  = Do not return errors, display message and exit process. This
'             will override the global return errors flag.
'     Returns zero if processed OK. Else, depending on ModChars and the global
'     return errors flag, will either display error and exit or will return
'     the error number.

' The syntax and doc for slBuildBindDat is:
'  slBuildBindDat             (rsData String, [rsModChars String]) String
'     Returns a BindDat entry which is a specially formatted string required
'     by slExeBind.  Data contains the value you want converted into a BindDat.
'     A BindDat(s) is required to be passed to slExeBind.  The returned data
'     may also be compressed and/or encrypted.  If an error occurs then the
'     return value will be an empty string.  Use slGetError or slGetErrorNumber
'     to determine the error.
'     ModChars:
'        B = Bind as Blob.  This is the default.
'        C = Compress the data.
'        N = Encrypt the data.
'        T = Bind as Text.  Default is to bind as Blob.
'        Note: If both C and N then will first compress and then encrypt.


Fred Meier

Fredrick

My mistake,  I have corrected to slBuildInsertOrUpdate,  thanks for catching.