I'm trying to seed an sqlite DB using angular taking data from a json file.
My procedure is simple: I have the DB schema in a angular constant and I init the DB creating tables and fields. After the creation I load my json file and I loop betweek objects inserting elements in my DB.
I try with one object and It works: exploring my sqlite db with Chrome I have my tables filled with data. The problems is when I loop between my 1500 objects: after 200 objects the table disappear and in the console I have "could not prepare statement (1 no such table: Questions)"
I write my code in coffeescript and so this is the compiled version:
(function() {
var DatabaseFactory;
DatabaseFactory = (function() {
function DatabaseFactory($log, $q, DBCONFIG, CATEGORY_SEED, $http) {
var category_init, db, fetch, fetchAll, fix_correct, fix_image, import_category, import_questions, import_single_answer, import_single_question, init, query, removeQuotes, seed;
db = null;
init = function() {
db = window.openDatabase(DBCONFIG.name, '1.0', 'database', -1);
return angular.forEach(DBCONFIG.tables, function(table) {
var columns, q;
columns = [];
angular.forEach(table.columns, function(column) {
return columns.push("" + column.name + " " + column.type);
});
q = "DROP TABLE " + table.name;
query(q);
$log.info("Table " + table.name + " deleted");
q = "CREATE TABLE IF NOT EXISTS " + table.name + " (" + (columns.join(',')) + ")";
query(q);
return $log.info("Table " + table.name + " initialized");
});
};
query = function(query, bindings) {
var deferred;
bindings = (typeof bindings !== "undefined" ? bindings : []);
deferred = $q.defer();
db.transaction(function(transaction) {
transaction.executeSql(query, bindings, (function(transaction, result) {
deferred.resolve(result);
}), function(transaction, error) {
deferred.reject(error);
$log.error(error.message);
});
});
return deferred.promise;
};
seed = function() {
category_init();
return $http.get('data/question.json').then(function(result) {
return import_questions(result.data);
});
};
category_init = function() {
return angular.forEach(CATEGORY_SEED, function(category) {
return import_category(category);
});
};
import_category = function(category) {
var q;
q = "INSERT INTO Sections (id, label, image) VALUES (" + category['id'] + ",'" + (removeQuotes(category['category'])) + "','" + category['image'] + "')";
return query(q);
};
import_questions = function(data) {
var question, _i, _len, _results;
_results = [];
for (_i = 0, _len = data.length; _i < _len; _i++) {
question = data[_i];
$log.info(question['text']);
_results.push(import_single_question(question));
}
return _results;
};
import_single_question = function(question) {
var answer, image, q, question_section, _i, _len, _ref, _results;
question_section = question['section']['id'];
image = question['image']['image']['url'];
q = "INSERT INTO Questions (id, text, exam_type, errors_count, done_count, section_id, image) VALUES (" + question['id'] + ",'" + (removeQuotes(question['text'])) + "','" + question['quiz_type'] + "',0,0, " + question_section + ",'" + (fix_image(image)) + "')";
query(q);
_ref = question['answers'];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
answer = _ref[_i];
_results.push(import_single_answer(question['id'], answer));
}
return _results;
};
import_single_answer = function(question_id, answer) {
var q;
q = "INSERT INTO Answers (id, text, correct, question_id) VALUES (" + answer['id'] + ",'" + (removeQuotes(answer['text'])) + "','" + (fix_correct(answer['correct'])) + "'," + question_id + ")";
return query(q);
};
removeQuotes = function(str) {
return str.replace(/'/g, "'").replace(/"/g, """);
};
fix_correct = function(correct) {
if (correct === true) {
return 1;
} else {
return 0;
}
};
fix_image = function(image) {
if (image === '/images/fallback/default.png') {
return "";
}
};
fetchAll = function(result) {
var i, output;
output = [];
i = 0;
while (i < result.rows.length) {
output.push(result.rows.item(i));
i++;
}
return output;
};
fetch = function(result) {
return result.rows.item(0);
};
return {
fetch: fetch,
fetchAll: fetchAll,
init: init,
query: query,
seed: seed
};
}
return DatabaseFactory;
})();
angular.module('app').factory('DatabaseFactory', ['$log', '$q', 'DBCONFIG', 'CATEGORY_SEED', '$http', DatabaseFactory]);
}).call(this);
DBCONFIG
and CATEGORY_SEED
are two constant that I use to create tables. I add DROP TABLE
to delete and recreate the table everytime. I will remove it in the final version.
The problem is in the 'import_question' function. If I comment the queries and I leave just the log statement I can see in my console all the questions. With the query after 200 question I receive the error.
Is this a memory problem?
Aucun commentaire:
Enregistrer un commentaire