var $name = document.querySelector('#name');
var $surname = document.querySelector('#surname');
var $dni = document.querySelector('#dni');
var $ol = document.querySelector('ol');
/**
* Opens an existing database or creates a new one
*
* @returns {db} Data base
*/
function open_create_database() {
// Opens or creates database
var db = openDatabase('mydb', '1.0', 'mydb description', 2 * 1024 * 1024);
// If table doesn't exist, we will create a new one with the following template
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS people " +
"( dni BIGINT PRIMARY KEY" +
", name CHAR(8) DEFAULT ''" +
", surname CHAR(16) DEFAULT ''" +
")");
});
return db;
}
/**
* Delete table from database
*
*/
function delete_table() {
var db = open_create_database();
db.transaction(function(tx) {
tx.executeSql("DROP TABLE people");
$ol.innerHTML = "";
}, function(tx, error) {
alert(error.message)
});
}
/**
* Inserts or replaces person in db
*
* @param person Person
* @param [filename] File name
*/
function insert_replace_in_db(person) {
var db = open_create_database();
db.transaction(function(tx) {
tx.executeSql("REPLACE INTO people " +
"( dni" +
", name" +
", surname" +
") VALUES (?,?,?)", [
person.dni,
person.name,
person.surname
]);
}, function(tx, error) {
alert(error.message)
});
}
/**
* Load people from the data base
*/
function load_from_database() {
var db = open_create_database();
var html = "";
$ol.innerHTML = "";
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM people ORDER BY name ASC", [],
function(tx, r) {
var e;
for (var i = 0; i < r.rows.length; i++) {
e = r.rows.item(i);
html += '<li>' + e.name + ' ' + e.surname + ', ' + e.dni + '</li>';
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.