Pequeño tutorial sobre un CRUD
:
Creamos o abrimos un DB:
var myDB = window.sqlitePlugin.openDatabase({name: "mySQLite.db", location: 'default'});
myDB.transaction(function(transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS phonegap_pro (id integer primary key, title text, desc text)', [],
function(tx, result) {
alert("Table created successfully");
},
function(error) {
alert("Error occurred while creating the table.");
});
});
Insertando data:
var title="Fredy Guibert";
var desc="Full Stack Web Developer";
myDB.transaction(function(transaction) {
var executeQuery = "INSERT INTO phonegap_pro (title, desc) VALUES (?,?)";
transaction.executeSql(executeQuery, [title,desc]
, function(tx, result) {
alert('Inserted');
},
function(error){
alert('Error occurred');
});
});
Select
myDB.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM phonegap_pro', [], function (tx, results) {
var len = results.rows.length, i;
$("#rowCount").append(len);
for (i = 0; i < len; i++){
$("#TableData").append("<tr><td>"+results.rows.item(i).id+"</td><td>"+results.rows.item(i).title+"</td><td>"+results.rows.item(i).desc+"</td></tr>");
}
}, null);
});
Update
$("#update").click(function(){
var id=$("#id").text();
var title=$("#title").val();
var desc=$("#desc").val()
myDB.transaction(function(transaction) {
var executeQuery = "UPDATE phonegap_pro SET title=?, desc=? WHERE id=?";
transaction.executeSql(executeQuery, [title,desc,id],
//On Success
function(tx, result) {alert('Updated successfully');},
//On Error
function(error){alert('Something went Wrong');});
});
});
Delete
myDB.transaction(function(transaction) {
var executeQuery = "DELETE FROM phonegap_pro where id=?";
transaction.executeSql(executeQuery, [id],
//On Success
function(tx, result) {alert('Delete successfully');},
//On Error
function(error){alert('Something went Wrong');});
});
Eliminar la tabla:
myDB.transaction(function(transaction) {
var executeQuery = "DROP TABLE IF EXISTS phonegap_pro";
transaction.executeSql(executeQuery, [],
function(tx, result) {alert('Table deleted successfully.');},
function(error){alert('Error occurred while droping the table.');}
);
});
Referencia: https://phonegappro.com/tutorials/phonegap-sqlite-tutorial-with-example-apache-cordova/
Si estás utilizando ionic
, este tuto te va a gustar: https://www.thepolyglotdeveloper.com/2015/12/use-sqlite-in-ionic-2-instead-of-local-storage/ y su respectivo video explicando detalle a detalle: https://www.youtube.com/watch?v=F6TUtN0VYwY