Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

HTML5 SQL database

HTML5 SQL database
  • Loading branch information...
commit 2dda8f5aa897b8491f35d22a4e9dd784193f910d 1 parent 69136dc
Lukasz Dzierzak authored June 17, 2012
BIN  BIN/SQL/device/SampleDatabaseSQL.bar
Binary file not shown
BIN  BIN/SQL/simulator/SampleDatabaseSQL.bar
Binary file not shown
2  Localstorage/index.html
... ...
@@ -1,6 +1,6 @@
1 1
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
2 2
 <!--
3  
-* Copyright 2010-2011 BB Jam Polska (bbjam.pl).
  3
+* Copyright 2012 BB Jam Polska (bbjam.pl).
4 4
 *
5 5
 * Licensed under the Apache License, Version 2.0 (the "License");
6 6
 * you may not use this file except in compliance with the License.
4  Localstorage/js/main.js
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
-* Copyright 2010-2011 BB Jam Polska (bbjam.pl).
  2
+* Copyright 2012 BB Jam Polska (bbjam.pl).
3 3
 *
4 4
 * Licensed under the Apache License, Version 2.0 (the "License");
5 5
 * you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@ function SaveText() {
20 20
 	var inputResult =  document.getElementById("inputElement").value;
21 21
 	
22 22
 	// Next we need to create database element and save our text.
23  
-	// Następinie tworzymy bazę danych i zapisujemy nasz tekst.
  23
+	// Następnie tworzymy bazę danych i zapisujemy nasz tekst.
24 24
 	localStorage.setItem("databaseName", inputResult);
25 25
 	
26 26
 	alert("Element saved.");
4  README.md
Source Rendered
@@ -9,7 +9,7 @@ Localstorage	- HTML5 local.storage element
9 9
 
10 10
 SQL				- HTML5 SQL database
11 11
 
12  
-In BIN folder you can find executables which you can run using simulator or BB 10 Dev Alpha devices.
  12
+In BIN folder you can find executables which you can run using BB10 simulator or BB 10 Dev Alpha devices.
13 13
 
14 14
 ---------------------------------------
15 15
 
@@ -22,4 +22,4 @@ Localstorage	- element local.storage HTML5
22 22
 
23 23
 SQL				- baza SQL HTML5
24 24
 
25  
-W folderze BIN znajdują się pliki wykonywalne, które możecie uruchomić na symulatorze lub urządzeniu BB 10 Dev Alpha.
  25
+W folderze BIN znajdują się pliki wykonywalne, które możecie uruchomić na symulatorze BB10 lub urządzeniu BB 10 Dev Alpha.
18  SQL/config.xml
... ...
@@ -0,0 +1,18 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<widget xmlns="http://www.w3.org/ns/widgets"
  3
+        xmlns:rim="http://www.blackberry.com/ns/widgets"
  4
+        version="1.0.0.0" id="sampleDatabaseSQL">
  5
+
  6
+  <author href="http://www.bbjam.pl/" rim:copyright="Copyright 2012 BB Jam Polska">BB Jam Polska</author>
  7
+
  8
+  <name>SQL database</name>
  9
+
  10
+  <description>
  11
+    A sample application to demonstrate some of the local.storage database possibilities.
  12
+  </description>
  13
+  
  14
+  <icon src="icon.png"/>
  15
+
  16
+  <content src="index.html" />
  17
+
  18
+</widget>
BIN  SQL/icon.png
36  SQL/index.html
... ...
@@ -0,0 +1,36 @@
  1
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
  2
+<!--
  3
+* Copyright 2012 BB Jam Polska (bbjam.pl).
  4
+*
  5
+* Licensed under the Apache License, Version 2.0 (the "License");
  6
+* you may not use this file except in compliance with the License.
  7
+* You may obtain a copy of the License at
  8
+*
  9
+* http://www.apache.org/licenses/LICENSE-2.0
  10
+*
  11
+* Unless required by applicable law or agreed to in writing, software
  12
+* distributed under the License is distributed on an "AS IS" BASIS,
  13
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14
+* See the License for the specific language governing permissions and
  15
+* limitations under the License.
  16
+-->
  17
+<html> 
  18
+	<head>
  19
+		<meta name="viewport" content="initial-scale=1.0,width=device-width" />
  20
+		<script type="text/javascript" src="js/main.js"></script>
  21
+	</head>
  22
+	<body onload="InitializeDatabase();">
  23
+	<!-- 	To load text when application starts we can use onload element
  24
+			Aby załadować tekst podczas uruchomienia aplikacji możemy użyć elementu onload
  25
+	-->
  26
+		<div align="center">
  27
+			<p style="font-weight: bold">Text:</p>
  28
+		<input type="text" id="inputElement">
  29
+		<br>
  30
+		<input type="button" onclick="SaveText();" value="Save">
  31
+		<br>
  32
+		<input type="button" onclick="GetText();" value="Show">
  33
+		<br>
  34
+		<input type="button" onclick="ClearText();" value="Clear database">
  35
+	</body>
  36
+</html>
93  SQL/js/main.js
... ...
@@ -0,0 +1,93 @@
  1
+var db;
  2
+/*
  3
+* Copyright 2012 BB Jam Polska (bbjam.pl).
  4
+*
  5
+* Licensed under the Apache License, Version 2.0 (the "License");
  6
+* you may not use this file except in compliance with the License.
  7
+* You may obtain a copy of the License at
  8
+*
  9
+* http://www.apache.org/licenses/LICENSE-2.0
  10
+*
  11
+* Unless required by applicable law or agreed to in writing, software
  12
+* distributed under the License is distributed on an "AS IS" BASIS,
  13
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14
+* See the License for the specific language governing permissions and
  15
+* limitations under the License.
  16
+*/
  17
+
  18
+function InitializeDatabase() {
  19
+	
  20
+// Database will be opened or created if not exist.
  21
+// Otwieramy lub tworzymy bazę danych w przypadku gdy nie istnieje.	
  22
+ db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);	
  23
+
  24
+db.transaction(function (tx) {  
  25
+// Next we need a table so we are creating it if not exists.
  26
+// Następnie musimy utworzyć table jeżeli jeszcze nie istnieje.
  27
+   tx.executeSql('CREATE TABLE IF NOT EXISTS MYTEXT (id unique, text)');
  28
+});
  29
+
  30
+GetText();
  31
+	
  32
+}
  33
+
  34
+function SaveText() {
  35
+	// We are getting our input text from input element
  36
+	// Pobieramy tekst z elementu input
  37
+	var inputResult = document.getElementById("inputElement").value;
  38
+	
  39
+	// This example is very simple so we will only update the text value in row number 1. We need to use "INSERT OR REPLACE"
  40
+	// because if row won't exsist we will use INSERT or if exists REPLACE.
  41
+	// W tym prostym przykładzie będziemy aktualizować informację tylko w wierszu 1 używając polecenia "INSERT OR REPLACE"
  42
+	// ze względu na fakt, że jeżeli wiersz nie istnieje należy go stworzyć lub zaktualizować w przeciwnym wypadku.
  43
+	db.transaction(function (tx) {  
  44
+	tx.executeSql("INSERT OR REPLACE INTO MYTEXT (id, text) VALUES (1, ?)", [inputResult]);
  45
+	});
  46
+	
  47
+	alert("Element saved.");
  48
+	clearInput();
  49
+	
  50
+}
  51
+
  52
+function GetText() {
  53
+	// In this case we are getting all elements from MYTEXT table and then displaying only text value from first row.
  54
+	// W typ wypadku pobieramy wszystkie elementy z tabeli MYTEXT a następnie wyświetlamy wartość pola text z pierwszego rzędu.
  55
+	db.transaction(function (tx) {
  56
+	tx.executeSql('SELECT * FROM MYTEXT', [], function (tx, results) {
  57
+		// Checking if element exsist
  58
+		// Sprawdzanie czy element istnieje
  59
+		if (typeof(results.rows.item(0).text) !== "undefined") {
  60
+			// Set the text value to input element
  61
+			// Ustaw tekst elementu input
  62
+			document.getElementById("inputElement").value = results.rows.item(0).text;	
  63
+		} else {
  64
+			// Or display an error when element is not exsist
  65
+			// Lub wyświetl błąd jeżeli element nie istnieje
  66
+			alert("Database is empty.");
  67
+		}	
  68
+	});
  69
+	});
  70
+	
  71
+
  72
+	   
  73
+}
  74
+
  75
+function ClearText() {
  76
+	// To clear database
  77
+	// Aby wyczyścić bazę danych
  78
+	db.transaction(function (tx) {  
  79
+	tx.executeSql("DROP TABLE MYTEXT", []);
  80
+	});
  81
+	
  82
+
  83
+	alert("Database clear.");
  84
+	clearInput();
  85
+}
  86
+
  87
+// Sample function to clear input field after operation.
  88
+// Prost funkcja czyszcząca pole input po wykonaniu danej operacji.
  89
+
  90
+function clearInput() {
  91
+	
  92
+	document.getElementById("inputElement").value = "";
  93
+}

0 notes on commit 2dda8f5

Please sign in to comment.
Something went wrong with that request. Please try again.