Ich habe ein Problem mit einer SQLite-Abfrage. Meine Datenbank hat einen Tabellennamen "tipo", sie enthält 3 Spalten: code, categorie und sousCategorie.Phonegap: SQLite-Problem
Ich fülle diese Tabelle eine CSV-Datei unter Verwendung von (CODE_LIT = Code, All = sousCategorie, General = Kategorie): CSVFile
Mein Problem ist, wenn ich die sousCategorie von "Coraux" für exemple erhalten möchten. Ich benutze diese Abfrage:
this.getSousCategorie = function(categorie){
this.db.transaction(function(tx){
tx.executeSql("SELECT distinct sousCategorie,categorie FROM tipo where categorie = 'Coraux' ", [], function(tx,results){
var element = document.getElementById('sousCategorie');
var s = "";
for (var i = 0; i < results.rows.length; i++) {
s+="<option>"+results.rows.item(i).sousCategorie+";;;"+results.rows.item(i).categorie+"</option>";
}
element.innerHTML = s;
}, this.errorCB);
},this.errorCB);
}
Ich habe keine Ergebnisse haben. Aber wenn ich die WHERE diese Abfrage von Code = ‚AA‘ zu ändern, es gibt mir das richtige Ergebnis:
SELECT distinct sousCategorie,categorie FROM tipo where code = 'AA'
Ich verstehe nicht, warum diese Abfrage mit allen Spalten arbeitet außer Kategorie. Ich hoffe, jemand mir helfen, ich danke Ihnen sehr
EDIT: Hier werden die Funktionen ist, dass ich die CSV-Datei in meiner Datenbank
function importCategories(){
ReadFileFromWithInProject("test.csv");
}
function ReadFileFromWithInProject(filename){
var path = "file:///var/mobile/Containers/Data/Application/D0D4318C-7843-420A-9289-B34FA10F0DA8/Library/NoCloud/phonegapdevapp/"
window.resolveLocalFileSystemURL(path, function(fs){
fs.getFile("www/"+filename,null,function(fileEntry){
readFile(fileEntry);
},function(e){
alert(e);
});
},function(e){
alert(e);
});
}
function readFile(fileEntry){
fileEntry.file(function(file){
var reader = new FileReader();
reader.onloadend = function(){
PaintValues(this.result);
};
reader.readAsText(file);
});
}
function PaintValues(strData){
var splitArray = strData.split("\n");
var strPaintData = "";
var BdDonnees = new BdD();
alert(splitArray.length-1);
for (var i = 1; i < splitArray.length-1; i++) {
var rowSplit = splitArray[i].split(",");
BdDonnees.newTipo(rowSplit); //add to the Database
}
BdDonnees.getCategories();
}
Und hier ist zu importieren verwendet, um meine Datenbank:
function BdD(){
\t this.db= window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
this.setupTable = function(){
\t \t this.db.transaction(function(tx){
\t \t \t tx.executeSql('DROP TABLE IF EXISTS entrees');
\t \t \t tx.executeSql("CREATE TABLE IF NOT EXISTS entrees(id INTEGER PRIMARY KEY,photo,latitude,longitude,code,date)");
\t \t \t tx.executeSql('DROP TABLE IF EXISTS tipo');
\t \t \t tx.executeSql("CREATE TABLE IF NOT EXISTS tipo(id INTEGER PRIMARY KEY,code ,categorie ,sousCategorie)");
\t \t \t alert("Reset réalisé")
\t \t },this.errorCB);
\t \t
\t }
this.newTipo = function(tipo){
\t \t this.db.transaction(function(tx){
\t \t \t \t tx.executeSql('INSERT INTO tipo (code,categorie,sousCategorie) VALUES (?,?,?)',[tipo[0],tipo[2],tipo[1]]);
\t \t \t },this.errorCB);
\t }
this.getCategories = function(){
\t \t this.db.transaction(function(tx){
\t \t \t tx.executeSql('SELECT distinct categorie FROM tipo ORDER BY categorie', [], function(tx,results){
\t \t \t \t var element = document.getElementById('categorie');
\t \t \t \t var s = '<option value="" selected disabled hidden>Choisissez une catégorie</option>';
\t \t \t \t for (var i = 0; i < results.rows.length; i++) {
\t \t \t \t \t s+="<option>"+results.rows.item(i).categorie+"</option>";
\t \t \t \t }
\t \t \t \t element.innerHTML = s;
\t \t \t }, this.errorCB);
\t \t },this.errorCB);
\t }
Ich würde vermuten, dass Ihre CSV-Dateien falsche Zeilenendezeichen verwenden, die in der Datenbank enden. –
Hallo, du hast recht, das ist das Problem, aber ich weiß nicht, wie man das Ende entfernt (ich denke, es ist das \ n) – Couldosh
Wie wird die Datei generiert und importiert? –