Skip to content

Instantly share code, notes, and snippets.

@arturoleon
Last active January 2, 2016 18:49
Show Gist options
  • Save arturoleon/8346157 to your computer and use it in GitHub Desktop.
Save arturoleon/8346157 to your computer and use it in GitHub Desktop.
Ejercicio de ejemplo para Titanium con bases de datos
(function(){
var bd = Ti.Database.open('baseDeDatosCompleta');
bd.file.setRemoteBackup(false);
bd.execute('CREATE TABLE IF NOT EXISTS entradas(id INTEGER PRIMARY KEY, nombre TEXT, apellido TEXT);');
var ventanaInicio = require('ventanaInicio');
ventanaInicio(bd).open();
})();
function ventanaAgregar(bd){
if(bd === undefined) var bd = Ti.Database.open('baseDeDatos');
var self = Ti.UI.createWindow({
backgroundColor:'white'
});
var botonCerrar = Ti.UI.createButton({
title: 'Cerrar',
top: 25,
right: 15
});
botonCerrar.addEventListener('click',function(){
self.close();
});
self.add(botonCerrar);
var entradaTexto = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
top: 90, left: 35,
width: 250, height: 60
});
var entradaApellido = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
top: 160, left: 35,
width: 250, height: 60
});
var enviarTexto = Ti.UI.createButton({
title:'Agregar entrada',
top:225
});
enviarTexto.addEventListener('click',function(){
if(entradaTexto.value != '' && entradaApellido.value != ''){
bd.execute('INSERT INTO entradas (nombre,apellido) VALUES (?,?)', entradaTexto.value,entradaApellido.value);
alert('Dato agregado: "'+entradaTexto.value+' '+entradaApellido.value+'".');
self.close();
}else{
alert('Debes llenar al menos un campo.');
}
});
self.add(entradaTexto);
self.add(entradaApellido);
self.add(enviarTexto);
return self;
}
module.exports = ventanaAgregar;
function ventanaEditar(id,bd){
if(bd === undefined) var bd = Ti.Database.open('baseDeDatos');
var entrada = bd.execute('SELECT id,nombre,apellido FROM entradas WHERE id='+id+';');
var ventanaListar = require('ventanaListar');
var self = Ti.UI.createWindow({
backgroundColor:'white'
});
var botonCerrar = Ti.UI.createButton({
title: 'Cerrar',
top: 25,
right: 15
});
botonCerrar.addEventListener('click',function(){
ventanaListar(bd).open();
self.close();
});
self.add(botonCerrar);
var entradaTexto = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
top: 90, left: 35,
width: 250, height: 60,
value:entrada.fieldByName('nombre')
});
var entradaApellido = Ti.UI.createTextField({
borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
color: '#336699',
top: 160, left: 35,
width: 250, height: 60,
value:entrada.fieldByName('apellido')
});
var enviarTexto = Ti.UI.createButton({
title:'Actualizar entrada',
top:225
});
enviarTexto.addEventListener('click',function(){
if(entradaTexto.value != '' && entradaApellido.value != ''){
bd.execute('UPDATE entradas SET nombre=?, apellido=? WHERE id=?;',entradaTexto.value,entradaApellido.value,id);
alert('Dato actualizado: "'+entradaTexto.value+' '+entradaApellido.value+'".');
ventanaListar(bd).open();
self.close();
}else{
alert('Debes llenar al menos un campo.');
}
});
self.add(entradaTexto);
self.add(entradaApellido);
self.add(enviarTexto);
return self;
}
module.exports = ventanaEditar;
function ventanaInicio(bd){
if(bd === undefined) var bd = Ti.Database.open('baseDeDatos');
var ventanaAgregar = require('ventanaAgregar');
var ventanaListar = require('ventanaListar');
var self = Ti.UI.createWindow({backgroundColor:'white'});
var botonCrear = Ti.UI.createButton({
title:'Agregar registro',
top: 75
});
var botonListar = Ti.UI.createButton({
title:'Ver registros',
top: 175
});
self.add(botonCrear);
self.add(botonListar);
botonListar.addEventListener('click',function(e){
ventanaListar(bd).open();
});
botonCrear.addEventListener('click',function(e){
ventanaAgregar(bd).open();
});
return self;
}
module.exports = ventanaInicio;
function ventanaListar(bd){
if(bd === undefined) var bd = Ti.Database.open('baseDeDatos');
var ventanaEditar = require('ventanaEditar');
var self = Ti.UI.createWindow({
backgroundColor:'white'
});
var botonCerrar = Ti.UI.createButton({
title: 'Cerrar',
top: 25,
right: 15
});
botonCerrar.addEventListener('click',function(){
self.close();
});
var tabla = Ti.UI.createTableView({
editable:true
});
tabla.addEventListener('delete',function(e){
bd.execute('DELETE FROM entradas WHERE id='+e.source.id+';');
alert("Entrada id: "+e.source.id+" borrada.");
});
tabla.addEventListener('click',function(e){
ventanaEditar(e.source.id,bd).open();
self.close();
});
var entradas = bd.execute('SELECT id,nombre,apellido FROM entradas;');
while (entradas.isValidRow())
{
tabla.appendRow(Ti.UI.createTableViewRow({
title: entradas.fieldByName('nombre')+" "+entradas.fieldByName('apellido'),
id: entradas.fieldByName('id')
}));
entradas.next();
}
entradas.close();
self.add(tabla);
self.add(botonCerrar);
return self;
}
module.exports = ventanaListar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment