Reputation: 344
I want to store the json data into my database within cordova . Im using angular js to retrieve the json data . one of my json data is a image source which is in string format (base 64). please help me to store the data into cordova DB
My json looks like : {"PlayList":[{"videoId":"bMvZymcZAFE","playlistTitle":"UAE Exchange India Briefing","playlistId":"PLPQxCdv2_-qjo0yucR6FmKzC-DqvFx83L","videoTitle":"UAE Exchange India Celebrates Customer Loyalty Month","videoPosition":"1","imgSrc":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAkICAgICQkGCAgHCAcHCAcHCAgHBwcHBwgHBwcIBwcHChALBwgOCQcHDRUNDhER"}],"errorMsg":"SUCCESS","errorFlag":"S"
script :
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.uaeexchangeindia.com/wp-content/themes/uaexindia/youtube-links-rand-mobapp.php?playlistId=PLPQxCdv2_-qjo0yucR6FmKzC-DqvFx83L&limit=0")
.then(function (response) {
$scope.names = response.data.PlayList;});
});
Upvotes: 0
Views: 825
Reputation: 344
Finally i found my solution ..! Here is the code
var obj = $.parseJSON(data);
$.each(obj.PlayList, function (index, i)
{
var videoId = i.videoId;
var imgSrc = i.imgSrc
save(videoId,imgSrc );
});
},
error:function()
{
alert("error");
$(".load_div").hide();
}
});
function save(videoId,imgSrc){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
var myDB= window.openDatabase("Database", "1.0", "Cordova Demo",10485760);
myDB.transaction(function(transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS YOUTUBE_VIDEO (id primary key, videoID text, imgSRc varchar)', [],
function(tx, result) {
},
function(error) {
alert("Error occurred while creating the table.");
});
});
myDB.transaction(function(transaction) {
var executeQuery = "INSERT INTO YOUTUBE_VIDEO (videoID,imgSRc) VALUES (?,?)";
transaction.executeSql(executeQuery, [videoId,imgSrc]
, function(tx, result) {
var newImage = document.createElement('img');
newImage.src = "data:image/png;base64,"+imgSrc;
document.getElementById("img").innerHTML = newImage.outerHTML;
},
function(error){
alert('Error occurred');
});
});
myDB.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM YOUTUBE_VIDEO WHERE id = ?', [1], function (tx, results) {
}, null);
});
}
Upvotes: 0
Reputation: 465
Cordova Supports storage mainly in 3 ways - Local storage, websql and indexDB.
I would suggest to use pouchDB instead, This will take care of storing in available db, and you can store data in the same json format. PouchDB
OR else
you can go ahead and use sqlite plugin. As you are using angular for your application its advised to use ng-cordova . Link to ng-cordova plugin for sqlite
Upvotes: 1