Store an array to sqlite-db


#1

Hi,

is there anay way to store an array to my sqlite-db?

Thank you!


#2

I’m not really experienced with sqlite-db, but you could always use a json_encoded string I guess?


#3

it is simply:
$scope.insert = function() {
angular.forEach(yourarray, function(value, index) {
//EXAMPLE
var query = “INSERT INTO people (firstname, lastname) VALUES (?,?)”;
$cordovaSQLite.execute(db, query, [value.firstname, value.lastname]).then(function(res) {
var message = "INSERT ID -> " + res.insertId;
console.log(message);
alert(message);
}, function (err) {
console.error(err);
alert(err);
});
});
}


#4

i guess he mean store a full array or full json, like:

skills = {"cs":{"front-end":["HTML5","CSS"],"back-end":["php,aws"]}} / etc...
var query = "INSERT INTO people (firstname, lastname,skills) VALUES (?,?,?)";
$cordovaSQLite.execute(db, query, [value.firstname, value.lastname,value.skills])

i’m about to start sqlite development if i get this before any one post the answer i will explain later how to do, so, sorry for not giving the right answer


#5

How to show all tables in Sqlite? I tried table but the result was an error…


#6

Why would you for any reason store an array in a SQL database? Or a JSON data for that matter?


#7
Store an array of SQLite database ? here you go :


.controller('VarietySearchCtrl', function ($scope, $state, $ionicPlatform, $cordovaSQLite, $ionicHistory, $ionicViewService) {
    $scope.listItems = [];
    $scope.whichItem = $state.params.aId; // this if you would like to set paramUrl
    $ionicPlatform.ready(function () {
        console.log('$ionicPlatform.ready called');
        var query = "SELECT * FROM tables ORDER BY id"; //change to any SQL queries
        $cordovaSQLite.execute(db, query, []).then(function (res) { //modify query,modify this
            if (res.rows.length > 0) {
                for (var i = 0; i < res.rows.length; i++) {
                    $scope.listItems.push(res.rows.item(i));
                }
            }
        }, function (err) {
            console.error(err);
        });
    });
});