New to SQL

Hi All - I’m following the example in Introduction to Programming with Xojo.pdf and the recordset for the database is nil after I try to insert a record set into the table. I’ve added message boxes galore to follow the code and everything appears to be working as expected but the record set is still nil. The link below is to my source code. Any help is much appreciated.

https://dl.dropboxusercontent.com/s/vaur33vg0q7c9uv/testDB.xojo_binary_project?dl=0

check errorMessage of the database object in that case

sql=sql+"FROM AddressBook" sql=sql+"ORDER BY name"

from your project. I don’t see a space being put between AddressBook and ORDER. That causes probably a syntax error.

[quote=126679:@Christian Schmitz] sql=sql+"FROM AddressBook" sql=sql+"ORDER BY name"

from your project. I don’t see a space being put between AddressBook and ORDER. That causes probably a syntax error.[/quote]

Brilliant. My dumb mistake. Thanks.

Welcome :slight_smile:

If I may offer a bit of advice since you’re new to db programming with Xojo: Get into the habit of checking for the db error after every operation. Xojo does NOT generate an exception for an SQL error so it’s up to you to do that.

Recordsets are NEVER nil unless there’s an SQL error. Every SQLSelect, SQLExecute, InsertRecord should have an error check. You do that and you’ll discover issues sooner rather than later.

I’d also recommend looking into Prepared Statements.
They really need to be used when the data that is to be inserted into the db is controlled by the user.

Language reference: http://documentation.xojo.com/index.php/PreparedSQLStatement
User Guide - Framework Page 129

Hhhmmm…I might be wrong but I believe that DatabaseRecord and Recordset objects use PreparedStatements behind the scenes for inserts and updates, respectively. For someone new to the language I’d stick with the basic for a while.

Bob… you are right… i notice that when i look at my CubeSQL log file… PreparedStatements has been use behind the scene.

I don’t know if this is true of all of the database plugins or not.