这是我的调用
let sqlLocalSelect = new SqlLocalSelect();
var variable = sqlLocalSelect;
console.log("variable " + JSON.stringify(variable));
这是我称的类
import { SQLite } from 'ionic-native';
export class SqlLocalSelect {
database: SQLite;
private li_sitios: Array<Object> = [];
constructor() {}
get get_li_sitios(): Array<Object> {
this.database = new SQLite();
this.database.openDatabase({name: "data.db", location: "default"}).then(() => {
this.database.executeSql('SELECT * FROM Markers;', [])
.then((data) => {
if(data.rows.length > 0){
this.li_sitios = [];
for(var i = 0; i < data.rows.length; i++) {
var value:string = "no";
if (data.rows.item(i).synchronised) {
value = "si";
} else {
value = "no";
}
this.li_sitios.unshift({
id: data.rows.item(i).id,
id_externa: data.rows.item(i).id_externa,
id_owner: data.rows.item(i).id_owner,
name: data.rows.item(i).name,
lat: data.rows.item(i).lat,
lng: data.rows.item(i).lng,
draggable: false,
synchronised: value,
fecha: data.rows.item(i).fecha,
hora: data.rows.item(i).hora
});
}
} else {
console.log("No se encontraron datos");
}
console.log("Consulta realizada correctamente");
console.log("li_sitios "+ JSON.stringify(this.li_sitios));
}, (error) => {
console.log("Error ejecutando la consulta: " + JSON.stringify(error));
});
}, (error) => {
console.log("Error al conectar con la base de datos local: " + JSON.stringify(error));
});
return this.li_sitios;
}
}
基本上,它是对SQLite数据库的查询,但是数据输出不会以正确的顺序运行
这是我的输出
06-07 18:23:51.030 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60258)] "variable []", source: file:///android_asset/www/build/main.js (60258)
06-07 18:23:51.120 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(179)] "OPEN database: data.db - OK", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (179)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: java.sql.SQLException: sqlite3_step failure: cannot rollback - no transaction is active
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.liteglue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.lang.Thread.run(Thread.java:831)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: java.sql.SQLException: sqlite3_step failure: cannot rollback - no transaction is active
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.liteglue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-07 18:23:51.240 21450-21555/io.ionic.starter W/System.err: at java.lang.Thread.run(Thread.java:831)
06-07 18:23:51.290 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60834)] "Consulta realizada correctamente", source: file:///android_asset/www/build/main.js (60834)
06-07 18:23:51.290 21450-21450/io.ionic.starter I/chromium: [INFO:CONSOLE(60835)] "li_sitios [{"id":4,"id_externa":0,"id_owner":1,"name":"Nombre 4","lat":22.834722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"si","fecha":"2017-6-5","hora":"15:36"},{"id":3,"id_externa":0,"id_owner":1,"name":"Nombre 2","lat":22.534722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"si","fecha":"2017-6-5","hora":"15:29"},{"id":2,"id_externa":15,"id_owner":1,"name":"Nombre 2","lat":22.534722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"no","fecha":"2017-6-5","hora":"15:24"},{"id":1,"id_externa":16,"id_owner":1,"name":"Nombre 1","lat":22.034722222222,"lng":-103.91194444444,"draggable":false,"synchronised":"no","fecha":"2017-6-5","hora":"15:22"}]", source: file:///android_asset/www/build/main.js (60835)
我首先提到的是,它似乎是空白的结果,然后查询的结果扔向控制台。
我首先提到的是,它似乎是get never的结果,然后查询的结果将其扔向控制台。
这是异步编程的本质。与简化的示例非常相似:
const arr = [];
setTimeout(()=>{
arr.push(1);
console.log(arr); // [1]
}, 1000);
console.log(arr); // []
按行号编码订单不一定是执行顺序。这听起来很简单(确实如此(,但是我们看到很多初学者都缺少它值得一提的。
非常感谢您,我在您的帮助下解决了它
this.sqlLocalSelect = new SqlLocalSelect();
setTimeout(()=>{
var variable = this.sqlLocalSelect.get_li_sitios;
console.log("variable " + JSON.stringify(variable));
}, 1000);