Advertisement
Guest User

Untitled

a guest
Oct 18th, 2014
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Exemplo WEB SQL Database HTML5 / JavaScript</title>
  5. <meta charset="UTF-8">
  6. </head>
  7. <body>
  8. <script>
  9.  
  10. // Criando banco
  11. var db = openDatabase("Teste", "1.0", "Teste", 1024);
  12. if(window.openDatabase){
  13. console.log("Banco de dados Criado");
  14. } else {
  15. console.log("Erro ao criar banco de dados");
  16. }
  17.  
  18. // Criando tabela
  19. db.transaction(function(tx) {
  20. tx.executeSql("create table if not exists teste (nome varchar(100))",null,sucesso,erro);
  21. mostraRegistros();
  22. console.log("Tabela Criada com Sucesso");
  23. });
  24.  
  25. // LOGS DE ERROS:
  26. function erro(tx,erro){
  27. console.log("Erro ao executar comando\n"+erro.message);
  28. }
  29. function sucesso(tx,sucesso){
  30. console.log("Comando executado com sucesso",sucesso.message);
  31. }
  32.  
  33. //insere no banco
  34. function insere(){
  35. db.transaction(function(tx) {
  36. var nome = document.getElementById('nome').value;
  37. alert(nome);
  38. console.log("Executando SQL...");
  39. tx.executeSql("insert into teste (nome) values (?)",[nome] ,sucesso,erro);
  40. console.log("Executando SQL... OK");
  41. mostraRegistros();
  42. });
  43. };
  44.  
  45. //limpa tabela
  46. function deleta(){
  47. db.transaction(function(tx) {
  48. tx.executeSql("delete from teste",null,sucesso,erro);
  49. mostraRegistros();
  50. console.log("Tabela Deletada");
  51. });
  52. }
  53.  
  54. //mostra registros
  55. function mostraRegistros(){
  56. var html = "";
  57. db.transaction(function(transaction){
  58. transaction.executeSql("SELECT * FROM teste", [] ,
  59. function(transaction, result){
  60. for(var i = 0; i < result.rows.length; i++){
  61. html += "Nome:"+result.rows.item(i)[['nome']]+"<br>";
  62. }
  63. document.getElementById('registros').innerHTML = html;
  64. },
  65. null
  66. );
  67. });
  68. }
  69. </script>
  70.  
  71. <input id="nome">
  72. <button id="salva" onclick="insere()">Salvar</button>
  73. <button id="deleta" onclick="deleta()">Limpar Tabela</button>
  74. <p id="registros"></p>
  75. </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement