View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

ODBC/MySQL Database Object Binding

Smalltalk Database Data Binding(ODBC/MySQL)

So it's a pain hand creating insert statements from data objects using smalltalk. It just really is. Say you have something like this

query := 'INSERT INTO users (name, pwd,type) VALUES (','"',name,'"',',','"',pwd,'"',',','"',type,'"',')'.

This, to be honest is just terrible. Not only do you manually have to concatenate the different parts of the string and manually add the parentheses and commas, if you have a Date value, you have to format the Date string to a way that the database will understand. This is impossible to do in the case of smalltalk without writing your own formatting method.

Now, for a better solution. Data binding. Instead of specifically creating the string yourself, you can use data binding to have it do it for you. Basically, you give it an object and then tell it the accessor methods for the instance variables of that object that you want to be inserted.

In this case, it would look something like the following(assuming that a is the object you are trying to insert.

session prepare: 'INSERT INTO users(name,pwd,type) VALUES(',':name,',':pwd,',':type',')'.
session bindInput:a.
session execute.

Yah its that easy. In fact its freaking awesome.

So yah, there is more information about data binding the databaseDevGuide inside the documentation, but this is the gist of it. No more hand coding of insert statements.

NOTE: I only used this using an ODBC/MySQL database. There are ways of doing the binding for the other types that I believe are extremely similar if not exactly the same, but no gurantees.

Link to this Page