Send to Printer


ST 4U 144: Updating Rows in a Database using VA Smalltalk

October 12, 2011 9:54:58.155

Today's Smalltalk 4 You looks at updating rows in a database using VA Smalltalk. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:


If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:

Today we'll look at updating data in a database using VA Smalltalk - to see how to connect, please go back to this tutorial, as we are building on it here.

Once you have a connection to your database set up (with a user who has permissions to make changes), you can update data pretty easily. First, set up a querySpec for the data you plan to change.

First, let's execute a query to see what we have now:

resultCollection := OrderedCollection new.
querySpec := AbtQuerySpec forEntireTable: table.
result := connection resultTableFromQuerySpec: querySpec.
result do: [:eachRow | resultCollection add: (eachRow)].
^resultCollection first asDictionary

That gives us this, if we inspect the results:

Existing Data

Now let's create a query to grab the data, and change it:

querySpec := (AbtQuerySpec new)
	statement: 'SELECT * from PEOPLE where PEOPLE.ZIPCODE = 12345';
	hostVarsShape: (nil).

Now we'll do the following steps:

  • Get the data from the database
  • Update the data in Smalltalk
  • Copy the data back to the database

oldRow := (connection resultTableFromQuerySpec: querySpec) first.
newRow := (oldRow deepCopy) 
	at: 'zipcode' put: 23456;
table := (connection openTableNamed: 'PEOPLE')
	atRow: oldRow putRow: newRow;

"commit it"
connection commitUnitOfWork

Now we'll do the query again, to ensure that we have done the update:

Updated Data

That pretty much wraps it up for today - we'll look at some more examples next time.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , ,

[st4u144-iPhone.m4v ( Size: 4039980 )]

posted by James Robertson

 Share Tweet This