SELECT 요청을 시작하기 전에 여러 개의 INSERT를 수행하고 싶습니다. 내 문제는 SELECT가 발생하면 INSERT가 아직 끝나지 않았다는 것입니다. 코르도바 SQLite는 삽입이 완료 될 때까지 대기합니다.
데이터베이스 처리를 위해 공장을 만들어 :databaseFactory.js
factory.insertIntoTable= function (database, data) {
var sqlCommand = 'INSERT INTO blablabla';
database.transaction(function(tx) {
database.executeSql(sqlCommand, [data.thingsToInsert], function (resultSet) {
console.log('success: insertIntoTable');
}, function (error) {
console.log('SELECT error: ' + error.message);
});
}, function(error) {
console.log('transaction error: ' + error.message);
database.close();
}, function() {
console.log('transaction ok: insertIntoTable');
});
};
, 삽입 어쨌든 잘 작동
ionic.Platform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
};
if(window.StatusBar) {
StatusBar.styleDefault();
}
db = window.sqlitePlugin.openDatabase({name: 'myDbName.db', location: 'default'});
if (window.Connection) {
if (navigator.connection.type !== Connection.NONE) {
databaseFactory.createTables(db);
MyService.getStuffFromAPI().then(function(result) {
for (var index = 0; index < result[0].campaigns.length; index++) {
databaseFactory.insertIntoTable(db, result[0].campaigns[index]);
}
var selectResult = databaseFactory.selectFromCampaigns();
console.log(selectResult); //This log comes earlier than the above inserts could finish.
}, function(result) {
console.log(result);
});
}
}
});
app.js를, 나는 그것을 확인했다.
나는 datebase.transaction이 비동기 적이기 때문에 단일 db.executeSQL 명령으로 시도했지만 $ q resolve를 추가해도 동일한 문제가 있음을 알 수 있습니다. 덕분에 도움을받을 수있었습니다!
'$의 q.all (약속)도'반환 약속 (고정)
app.js. 그래서 당신은 다음과 같은 것을 할 필요가 있습니다 :'$ q.all (promise) .then (function() {// 성공하면 코드를 선택 하시겠습니까?}, function (err) {// 오류시 코드})' . –
그래,이 방법을 수정하고 여전히 작동하지 않습니다. API 요청이 $ q가 아니기 때문에 $ q가 완료되었는지 여부를 알 수 없습니다. SQLite 플러그인을 사용해 보셨습니까? – MagicDragon
나는 결국 해결책을 찾은 것 같아. – MagicDragon