In this example you can insert, show, delete and search the data with ScriptDB with Google Drive:
// Script-as-app template.
function doGet()
{
var myapp = UiApp.createApplication().setTitle('Connect with DB');
var mypanel = myapp.createVerticalPanel();
var myname = myapp.createTextBox().setId('Name').setName('Name');
var myage = myapp.createTextBox().setId('Age').setName('Age');
var mycity = myapp.createTextBox().setId('City').setName('City');
var mygrid = myapp.createGrid(3, 2);
mygrid.setWidget(0, 0, myapp.createLabel('Name:'));
mygrid.setWidget(0, 1, myname);
mygrid.setWidget(1, 0, myapp.createLabel('Age:'));
mygrid.setWidget(1, 1, myage);
mygrid.setWidget(2, 0, myapp.createLabel('City'));
mygrid.setWidget(2, 1, mycity);
var mybutton = myapp.createButton('Insert record');
var handler = myapp.createServerHandler('insertData');
handler.addCallbackElement(myname);
handler.addCallbackElement(myage);
handler.addCallbackElement(mycity);
mybutton.addClickHandler(handler)
var showData = myapp.createButton('Show all data stored');
var handler = myapp.createServerHandler('showData');
showData.addClickHandler(handler)
var clearData = myapp.createButton('Delete all data of database');
var handler = myapp.createServerHandler('clearData');
clearData.addClickHandler(handler)
var searchData = myapp.createButton('Search record');
var handler = myapp.createServerHandler('searchData');
handler.addCallbackElement(myname);
handler.addCallbackElement(myage);
handler.addCallbackElement(mycity);
searchData.addClickHandler(handler)
var textArea = myapp.createTextArea().setId('mylog').setName('mylog').setWidth('500').setHeight('200');
mypanel.add(mygrid);
mypanel.add(mybutton);
mypanel.add(showData);
mypanel.add(clearData);
mypanel.add(searchData);
mypanel.add(textArea);
myapp.add(mypanel);
return myapp;
}
function insertData(e) {
var myapp = UiApp.getActiveApplication();
var name = e.parameter.Name;
var age = e.parameter.Age;
var city = e.parameter.City;
var db = ScriptDb.getMyDb();
var ob = {type: "mytable",
name: {first: name, age:age},
address: {city: city}
};
var result = db.save(ob);
showData(null);
myapp.close();
return myapp;
}
function showData(e) {
var myapp = UiApp.getActiveApplication();
var db = ScriptDb.getMyDb();
var results = db.query({});
myapp.getElementById('mylog').setText("");
var msg = "";
while (results.hasNext()) {
var result = results.next();
msg = msg + "type: " + result.type + ", name: " + result.name.first + ", age: " + result.name.age + ", city: " + result.address.city + "\n";
}
myapp.getElementById('mylog').setText(msg);
myapp.close();
return myapp;
}
function clearData(e) {
var myapp = UiApp.getActiveApplication();
var db = ScriptDb.getMyDb();
while (true)
{
var result = db.query({});
if (result.getSize() == 0) {
break;
}
while (result.hasNext()) {
db.remove(result.next());
}
}
showData(null);
myapp.close();
return myapp;
}
function searchData(e) {
var myapp = UiApp.getActiveApplication();
var name = e.parameter.Name;
var age = e.parameter.Age;
var city = e.parameter.City;
var db = ScriptDb.getMyDb();
var obage = (age == "") ? {} : {age: age} ;
var obname = ( name == "" ) ? obage : {first: name, age: obage} ;
var obcity = (city == "") ? {} : {city: city};
var ob = {type: "mytable", name: obname, address: obcity };
Logger.log(ob);
var stored = db.query(ob);
var msg = "";
while (stored.hasNext())
{
var result = stored.next();
msg = msg + "type: " + result.type + ", name: " + result.name.first + ", age: " + result.name.age + ", city: " + result.address.city + "\n";
}
myapp.getElementById('mylog').setText(msg);
myapp.close();
return myapp;
}