2

ionite로 모바일 응용 프로그램에 SQLite 데이터를 추가하고 싶습니다. 다음 사이트 https://blog.nraboy.com/2014/11/use-sqlite-instead-local-storage-ionic-framework/에서 단계별 자습서 단계를 수행했지만 여전히 물리적 장치에서 작동하지 않습니다. 조언. 된 index.html에서 내가 내가ionic cordovaSQLite 플러그인이 실제 장치에서 작동하지 않습니다.

이 내가 만든 다음 코드 첫 번째 코드

 var db=null; 
    var myApp=angular.module('myApp',['ionic','ngCordova']) 
    .run(function($ionicPlatform, $cordovaSQLite) { 
    $ionicPlatform.ready(function() { 
    if(window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
    } 
    if(window.StatusBar) { 
     StatusBar.styleDefault(); 
    } 

    db = $cordovaSQLite.openDB("test.db"); 
    $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)"); 
    console.log("hello"); 
}) 

을}) 쓴 내 app.js에 다음 코드 `

<script src="lib/ionic/js/ionic.bundle.js"></script> 

<script src="lib/ionic/js/angular-ui/angular-ui-router.js"></script> 
<script src="lib/ionic/js/angular/angular-resource.js"></script> 



<!-- Needed for Cordova/PhoneGap (will be a 404 during development) --> 
<script src="js/app.js"></script> 
<script src="js/ng-cordova.min.js"></script> 
<script src="cordova.js"></script>` 

를 넣어 people이라는 테이블과 로그인 컨트롤러에 하나의 행을 사람들 테이블에 삽입합니다.

controller('loginController',function($scope,$cordovaSQLite){ 

    var query = "INSERT INTO people (id,firstname, lastname) VALUES (?,?,?)"; 
    $cordovaSQLite.execute(db, query, [1,"khaled", "omar"]).then(function(res) { 
     $scope.name=res.insertId; 
    }, function (err) { 
     $scope.name="error"; 
    }); 

그리고 login.html에서 {{name}}을 썼지 만 브라우저에서 실행할 때 followin 오류가 발생합니다 undefined의 'openDatabase'속성을 읽을 수 없으며 실제 장치에서 실행하지만 여전히 작동하지 않습니다

+0

나는이 많은 정보로 어떤 대답도 얻지 못할 것이라고 생각합니다. 당신은 아마 당신의 코드를 보여줘야하고, 예상되는 것은 무엇이고, 어떤 에러가 있는지 등등. – AtanuCSE

+0

괜찮아요. 세부 사항을 적어 줘서 고마워요. –

+0

다행히 제 튜토리얼을 사용 했어요! 'db = $ cordovaSQLite.openDB ("test.db");를주의 깊게 살펴보면 올바르지 않다는 것을 알 수 있습니다. 매개 변수는 문자열이 아닙니다. 그것은 대상이되어야합니다. '{name : "test.db"} "를 거기에 넣고 어떻게되는지 알려주십시오. –

답변

1

코드가 제대로 작동하고 있습니다. 오류는 컨트롤러의 문제에서 비롯된 것 같습니다.

+0

작업 코드를 공유 할 수 있습니까? – JohnAndrews

0

다음 코드를 사용해야합니다.

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1});

이 작동합니다.

1

아래 코드는 저에게 효과적입니다.

나는 이유는 모르겠지만,이 같은 if 문 앞에 onReady 방법 내부 데이터베이스 생성 코드를 추가해야한다 당신이 사용하기 전에 발생합니다

.run(function($ionicPlatform,$cordovaSQLite) { 
    $ionicPlatform.ready(function() { 

    db = $cordovaSQLite.openDB({name:"my.db", location:1});    
      $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS testTable(id integer primary key, col1 text,col2 tex)"); 

    if(window.cordova && window.cordova.plugins.Keyboard) { 
     // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
     // for form inputs) 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 

     // Don't remove this line unless you know what you are doing. It stops the viewport 
     // from snapping when text inputs are focused. Ionic handles this internally for 
     // a much nicer keyboard experience. 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if(window.StatusBar) { 
     StatusBar.styleDefault(); 
    }       
    }); 
0

$ ionicPlatform.ready() 코도 바.

0

이 실제로 일을, 감사 musakkhiranuj

db = $cordovaSQLite.openDB({name: "dbname.db", location: 1}); 

location:1 중요하다.

관련 문제