Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ionic将数据插入SQLite_Sqlite_Ionic Framework - Fatal编程技术网

如何使用ionic将数据插入SQLite

如何使用ionic将数据插入SQLite,sqlite,ionic-framework,Sqlite,Ionic Framework,我是爱奥尼亚的新手。我想向SQLite添加来自远程服务器的数据。我已经成功地将数据填充到列表中。那么如何将此数据存储到sqlite中呢。这是我的密码。如何将此数据传递给查询。我无法执行此操作 service.js angular.module('starter.service',[]). factory('userServices',['$http',function($http){ var users = []; return { get: function(

我是爱奥尼亚的新手。我想向SQLite添加来自远程服务器的数据。我已经成功地将数据填充到列表中。那么如何将此数据存储到sqlite中呢。这是我的密码。如何将此数据传递给查询。我无法执行此操作

service.js

angular.module('starter.service',[]).
  factory('userServices',['$http',function($http){
    var users = [];

    return {
      get: function(){
        return $http.get("http://xxxxxxxxx-info").then(function(response){
          users = response.data;

          return users;

        });
      },
      remove:function(content){
        users.splice(users.indexOf(content),1);
      },
      getUser:function(chatId)
      {
        for(var i=0; i<users.length;i++){
          if(users[i].content_id === parseInt(chatId)){
            return users[i];

          }
        }
        return null;
      }
    }
  }]);

你在哪里定义db?有必要等到设备准备就绪

$ionicPlatform.ready(function () {
    var db = $cordovaSQLite.openDB({ name: "my.db" });

    // just first time you need to define content table
    $cordovaSQLite.execute(db,"CREATE TABLE content (content_id integer, display_name text)");

    userServices.get().then(function (users) {
          //users is an array of user objects
          $scope.contents = users;
          console.log($scope.contents);
          var query = "INSERT INTO content (content_id, display_name) VALUES (?,?)";
          $cordovaSQLite.execute(db, query, [users.content_id, users.display_name]).then(function (res) {
            alert(res);
            alert('Inserted');
          }, function (e) {
            alert('Error:' + e.message);
          });
    });
});
您确定您的对象用户看起来像

{
     "content_id":12,
     "display_name":"hello world"
}
而且不像

  [
    {
         "content_id":12,
         "display_name":"hello world"
    },
    {
         "content_id":13,
         "display_name":"stackoverflow"
    },
    ...
]

我只是问,因为
用户
听起来像不止一个条目。

您在哪里定义了db?有必要等到设备准备就绪

$ionicPlatform.ready(function () {
    var db = $cordovaSQLite.openDB({ name: "my.db" });

    // just first time you need to define content table
    $cordovaSQLite.execute(db,"CREATE TABLE content (content_id integer, display_name text)");

    userServices.get().then(function (users) {
          //users is an array of user objects
          $scope.contents = users;
          console.log($scope.contents);
          var query = "INSERT INTO content (content_id, display_name) VALUES (?,?)";
          $cordovaSQLite.execute(db, query, [users.content_id, users.display_name]).then(function (res) {
            alert(res);
            alert('Inserted');
          }, function (e) {
            alert('Error:' + e.message);
          });
    });
});
您确定您的对象用户看起来像

{
     "content_id":12,
     "display_name":"hello world"
}
而且不像

  [
    {
         "content_id":12,
         "display_name":"hello world"
    },
    {
         "content_id":13,
         "display_name":"stackoverflow"
    },
    ...
]

我只是问,因为
用户
听起来像不止一个条目。

它包含多个条目当你必须循环每个条目并将它们逐个添加到数据库时它包含多个条目当你必须循环每个条目并将它们逐个添加到数据库时