Easy way to do a database select, update,delete or insert

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Easy way to do a database select, update,delete or insert

#1

Postby Segi » Thu Mar 23, 2017 10:22 pm

I wanted to share this in case some people don't know this but as of Ebase 5.2 there's a newer and much easier way to execute a database command which doesn't use a database resource. It needs a lot less code compared to the old way and I've been rewriting code that used the old way to use the new way.

The old way would look like this:

Code: Select all

sql="SELECT Username FROM Users";
var username="";

// Get the path for the selected menu item from the DB
var con=system.getDatabaseConnection("DBCONNECTION");
var rs,stmt;

try {
     stmt=con.prepareStatement(sql);
     rs=stmt.executeQuery();

     while (rs.next()) {
           username=rs.getString("Username");
     }
} catch (e) {
     // Dump the SQL for debugging purposes
     print("An error occurred reading the database with the query " + sql);
     print(e);

     if(rs) rs.close();
     if(stmt) stmt.close();
     if(con) con.close();

     event.stopExecution();
} finally {
     if(rs) rs.close();
     if(stmt) stmt.close();
     if(con) con.close(); 
}
With the new way, you'd do the same thing like this:

Code: Select all

services.database.executeSelectStatement("DBCONNECTION","SELECT Username FROM Users",

function (columnData) {
	   username=columnData.Username;
});
The one thing I've noticed though is that because of the way that JavaScript variable scopes work, any local variables that you want to assign to inside of the function have to be declared outside of the function

In other words, don't declare any local variables inside of the function.
0 x

Steve James
Ebase User
Posts: 331
Joined: Mon Mar 10, 2014 8:34 am

#2

Postby Steve James » Wed Mar 29, 2017 12:29 pm

Hi yes it is very useful, one word of warning is this new approach cannot protect you from sql injection as it cannot handle prepared statements.
0 x


Who is online

Users browsing this forum: No registered users and 122 guests