docs.roxen.comBack to normal mode
DocsRoxen2.1TutorialsDatabase Tutorial
Copyright  2001, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com

The query() function

The query() method of the Sql.sql object is the "simple" query interface. It is meant to be used for those queries that return little or no data.

It's signature could look frightening:

array(mapping(string:string|float|int)) query (string sql)

but it isn't that bad, really.

The returned value is an array, one element for every row, of mappings whose indices are the column names, and values the column contents.

So in order to access the "foo" column in the fourth returned row, you'll use

mixed datum = db[4]->foo;

If there are no results, the method will return an empty array.

Find out the country code for Italy


string country_code_for_italy() {
    object db=Sql.sql("mysql://user:password@localhost/sample");
    array result=db->query("select code from ids where name='Italy'");
    if (sizeof(result)>0) { //if there is any result
        return result[0]->code;
    }
    return 0; //no code found
} 

The reason why this interface is only suited for simple queries is that it will fetch the whole results set and store it locally. It's not that big a deal for small databases, but make a small mistake in specifying the query on an HUGE database, and it will be tens or hundreds of megabytes to fetch. Talk about bloat... If you're going to retrieve potentially huge data-sets, you'll need the big_query interface instead. It's a bit more complex to use, but it will allow you fetch results on demand.