JSDB     Home    License    Download    Tutorial    Reference    Projects    Feedback    History
[ English | français ]

Web / HTTP

var w = new Stream('http://www.jsdb.org/')

writeln(w.readLine());
var r = new Record;
w.readMIME(r);

writeln('Header\n',r.toString(),'\n');

var data = w.readFile();
writeln('Page\n',data);

w.close();
Open a new HTTP stream, sending a HTTP GET,
and using a proxy if necessary.
Print the HTTP response.
A data storage object for the header
Read a MIME header and store it in r.

Show the full HTTP header

Read the entire web page into a string
Print the web page.

Close the network connection.

Email

var m = new Mail('SMTP','username',
  'password','pop-server',
  'smtp-server','utf-8',
  'me@email.com');

m.send('me@email.com','subject','Hi there!');

var inbox = m.get();

if (inbox.count)
{
  writeln('mail from ',inbox.get(1,'Sender'));
  writeln(inbox.getMessage(1));
  writeln(inbox.getHTML(1));
}
m.close()
New Mail interface
  Does MD5 SMTP authentication, and
  the POP3/SMTP driver parses multipart
  messages. See the class reference for details.

Send a new message

Read the inbox. Mail.get() returns a Table.

If we have a message...

  Who sent it?
  Message text
  Message HTML, for multipart messages.

Close the server connections.

Databases

Table.create("myfile.dbf","NAME 30, SEQUENCE 10")
var myTable = new Table("myfile.dbf");

for each(var n in ["Alice","Bob","Cynthia"])
 myTable.add(new Record("NAME=" + n))

for (var i = 1; i <= myTable.count; i++)
{
  writeln(i, ": ", myTable.get(i, 'NAME') );


  myTable.set(i,'SEQUENCE',i-1);
}
Create a new xBase file with two columns
Open an xBase file in read-write mode

Add a few records


Database tables are numbered starting with 1.

Table.get(row,column) can take either a column
number (starting with 1) or a field name.

Table.set(row,column,value) makes immediate
changes to the data file.

Read a SQL table, build an index, and search for a value.

var db = new ODBC("DSN=driver;UID=login;PWD=password;");
var result = db.query("select * from mytable");

var searcher = new Index;
for (var i=1; i <= result.count; i++)
{
  searcher.add(result.get('NAME'));
}

var i = searcher.find('Mr. Smith');
var r = result.getRow(i + 1);

writeln('Data for Mr. Smith');
writeln(r.toString());

db.close().
See below for a shorter way to do this
result is a Table containing the result set.

Index is a generic class for binary string searches
Add our search data to the index


Search for a specific record.
i is a 0-based index, so add 1.
Table.getRow(row) returns a Record object.

Print the results on the screen.

Release ODBC resources. Closing db closes result.

Faster: Read a SQL table, build an index, and search for a value

var result = new Table("odbc://login:password@driver/mytable");

var searcher = result.index('NAME');

var r = result.getRow(searcher.find('Mr. Smith'));

writeln('Data for Mr. Smith');
writeln(r.toString());
result.close()
Opens an ODBC connection and fetches a table.
ODBC connections are cached and re-used.
Table.index(column) returns a 1-based Index.

Table.getRow(row) returns a Record object.

Print the results on the screen.

Close ODBC resources

Text

Read a file and count the words used

var source = new Stream("myfile.txt");
var count = new Object;
while (!source.eof)
{
  var line = source.readLine('\n');

  words = line.split(/\W/g);
  for (var i in words)
  {
    if (words[i] == '') continue;
    if (count[words[i]])
      count[words[i]]++;
    else
      count[words[i]] = 1;
  }
}

for (var i in count)
 writeln(i,': ',count[i]);
Open a text file in read mode
This is where we'll save the results
Repeat until we reach the End Of File

Read the next line from the file, using
newline as the delimiter.
Split the line into words. Use g for global.
For each word in the line...

Ignore empties
Is it a word we've seen before?
  Yes. Increment the counter.

  No. Create a new counter.

Done reading file data

For each word stored in our counter
write the word and its count

Next

More examples