如何使用Javascript项目在nativescript-sidekick中使用预填充的sqlite数据库

时间:2018-10-30 12:15:33

标签: javascript sqlite nativescript

我目前正在尝试使用 JavaScript 项目连接到NativeScript-Sidekick中预先填充的Sqlite数据库。我已经看过很多教程和视频,但是都包括 Angular 。是否有任何代码允许 JavaScript 项目与数据库进行交互?例如允许 TextField 由基于它们所在列的 SELECT 中拉出的内容填充?

到目前为止,这是我的代码,与教程等类似,但是它们确实使用了 Telerik

这是home-items-page.xml

<Page
navigatingTo="onNavigatingTo"  
xmlns="http://schemas.nativescript.org/tns.xsd">

<StackLayout>
    <TextField id="firstname" hint="Fist Name" text="{{ firstname }}"/>
    <TextField id="lastname" hint="Last Name" text="{{ lastname }}" />
    <StackLayout orientation="horizontal">
        <Button text="Insert" tap="{{ insert }}" />
        <Button text="Select" tap="{{ select }}" />
    </StackLayout>
</StackLayout>
</Page>

home-items-page.js

var createViewModel = require("./home-items-view-model").createViewModel;
var Sqlite = require("nativescript-sqlite");

function onNavigatingTo(args) {
var page = args.object;
if (!Sqlite.exists("eatforlife.db")) {
Sqlite.copyDatabase("eatforlife.db");
}

var db_name = "eatforlife.db";

new Sqlite(db_name).then(db => {
    page.bindingContext = createViewModel(db);
});

}

exports.onNavigatingTo = onNavigatingTo;

home-items-view-model

const observableModule = require("tns-core-modules/data/observable");
var Sqlite = require("nativescript-sqlite");

function createViewModel(db) {
var viewModel = Observable();

viewModel.firstname = "";
viewModel.lastname = "";

    viewModel.select = function(){
    db.all("SELECT * FROM client WHERE Email = ? AND Password = ?",["example@gmail.com","qwerty123"]).then(rows => {
        for(var row in rows){
            console.log("RESULT ", rows[row]);

        }
    }, error =>{
        console.log("SELECT ERROR",error);
    })
    }

return viewModel;
}

exports.createViewModel = createViewModel;

2 个答案:

答案 0 :(得分:0)

您应该在Observable上使用getter and setter方法,例如

viewModel.set("firstname", YourFirstNameFromDB);
viewModel.set("lastname", YourLastNameFromDB);

假设您要从数据库中获取一行,则可以使用get方法而不是全部方法。

答案 1 :(得分:0)

我使用 Observables

文本字段更改为点击 SQLite 数据

更新后的home-items-page.js

var createViewModel = require("./home-items-view-model").createViewModel;
var Sqlite = require("nativescript-sqlite");

function onNavigatingTo(args) {
var page = args.object;

if (!Sqlite.exists("eatforlife.sqlite")) {
 Sqlite.copyDatabase("eatforlife.sqlite");
}

var db_name = "eatforlife.sqlite";

new Sqlite(db_name).then(db => {
    page.bindingContext = createViewModel(db);
 });

}

exports.onNavigatingTo = onNavigatingTo;

更新了home-items-view-model.js

const Observable = require("tns-core-modules/data/observable").Observable;
const fromObject = require("tns-core-modules/data/observable").fromObject;

const fromObjectRecursive = require("tns-core- 
modules/data/observable").fromObjectRecursive;

var dialogs = require("tns-core-modules/ui/dialogs");
var Sqlite = require("nativescript-sqlite");

function createViewModel(db) {
 var viewModel = new Observable();


viewModel.select = function () {
    db.get("SELECT * FROM client").then(rows => {

        viewModel.set("setname", rows[10]);
        viewModel.set("setsurname",rows[11]);

        viewModel.set("name",this.get("setname"));
        viewModel.set("surname", this.get("setsurname"));

    }, error => {
        console.log("SELECT ERROR", error);
    });
  }

 return viewModel;
}

exports.createViewModel = createViewModel;

更新后的home-items-page.xml

<Page
 navigatingTo="onNavigatingTo"  
 xmlns="http://schemas.nativescript.org/tns.xsd">

<StackLayout>
    <Label id="name" text="{{ name }}"/>
    <Label id="surname" text="{{ surname }}" />
    <StackLayout orientation="horizontal">
        <Button text="Insert" tap="{{ insert }}" />
        <Button text="Select" tap="{{ select }}" />
     </StackLayout>
 </StackLayout>
</Page>

我希望这对其他许多人有用,这要感谢 Manoj