Sqlite3 && orm node.js && $.get ()

  To connect the server and the client, we should know what information to get or to send in server and client. 

1. Server

  I started a server by 'express', a dependency of node. And I'll use dependency 'orm' to connect with sqlite profile to get the statics in it. Sqlite is a software to help us create and manage databases. Also, we have to create a dependency.

(1)create and manage databases and tables in sqlite3

  Open the terminal ,and input 'sqlite3' to open sqlite. If you want to create a '.db' profile , you can input 'sqlite3 [ database name of yours ].db',like:

$ sqlite3 mydatabase.db

This will help you create a database in the current folder. To create a new table in the database you can :

sqlite> create table [ your table's name] (

     ...> [ row name ]  [ data type ]([ the length])  [ if null ] ) ;

';' can help u to back to 'sqlite >'.It's an example to create a table :

sqlite> create table students (

     ...> id int primary key not null,

     ...>name char not null);

  To see all the tables you can:

sqlite> .table

And it's easy to delete a table:

sqlite> drop table [ your table's name ]

  After we create database and tables, we can use sqlite to insert data into it, but we can use 'orm' to create tables or insert data as well. To insert data in sqlite ,for example:

sqlite> insert table students ( id, name)

     ...> values 1, Tom ;


(2)use orm to manage databases in node.js && create API

  Before we use orm , we should 'require' it into our js profile . And then we are able to connect with our database in our project.

let orm = require("orm");

orm.connect('sqlite:/[ GetFullPath of your database ]',function(err, db) {

    if(err)returnconsole.error('Connection error: '+ err);

    elseconsole.log('success!');

});

If you use express and orm at the same time , you can use this to connect with database in sqlite3 : ( this is in wiki of node-orm2)

app.use(orm.express("sqlite://username:password@host/database", {

define: function (db, models, next) {

models.person = db.define("person", { ... });

next();

}

}));

app.listen(80);

app.get("/", function (req, res) {

// req.models is a reference to models used above in define()

req.models.person.find(...);

});

As I use 'express' to start my server , so I connect orm and use orm in this way. Now we can easily get requests , find what we need in our databases , and send the response to client.

This is my example to connect with sqlite by orm models:

app.use(orm.express('[ GetFullPath of your database ]',{

    define:function(db, models, next) {

// 'db' is your database profile , 'models'  is a map to your database , it can help you to get data in your database to your js profile.'Next' is what you'll do next.

models.students = db.define("students", {

id:Number,

name:String

});

next();

}

}));

And then we can create an API which can help to find data of students in our database.

app.get('/student',function(req,res) {

   req.models.students.find({id:1},function(err,student) {

   console.log(student);

   res.send(student);

   })

});

There are other methods of orm models to manage the database ( when we use 'express' as well ) :

req.models.students.create( { ... },function (err, ...) { ... };

//to create data in table 'students'

And following is an example to change data of one student in table:

req.models.students.find({id:id},function(err,student){

    if(req.body.crtname!==""&&id!==''){

        student[0].name=req.body.crtname;

//To use 'body'  u need dependency 'body-parser'

        student[0].save(function(err) {

            if(err) {

            console.log('err'+ err);

            }else{

            res.send(student)

            }

       });

}else{

res.status(400).send(' please input in correct way ')

}

});

Now we can get the request and send our data . Next I'll share how to show our data to client by jquery.

2.Client

For example , if you want to add your student (id:1) information to the table of your website , you can set an 'id' to your button's tag , and use API we wrote to get data after the tag of this 'id' has been clicked.

$(document).ready ( function () {

$( '#submit' ).click (  function () {

  $( '#tablebody' ).empty();

//clear what there was in the table before clicked

      $.get('/student',function(ans) {

      let student = ans;

 //'ans' is what returns by '/student', and it's an array of the student's information of 'id:1'.

//the array is: [ { id:1 , name : Tom } ] ,as we input to our database.

      let str = ` <tr> <td> student[0].id </td>

                            <td> student [0].name </td> </tr>`;

      $( '#tablebody' ).append( str );

     });

}  )

} )

'$.get()' can help us get what entering the API will returns .  After we get data we can 'apend' our data into our website. And the client will get the data in the website .

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,312评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,578评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,337评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,134评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,161评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,303评论 1 280
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,761评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,421评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,609评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,450评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,504评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,194评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,760评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,836评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,066评论 1 257
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,612评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,178评论 2 341

推荐阅读更多精彩内容

  • 大莽阅读 287评论 0 0
  • “长老,B3429是否可以评为低等生物星球?” “恩,差不多可以了,毕竟用他们的时间计算,那首诗已经过去那么久了,...
    彭杨森阅读 787评论 2 1
  • 混编的时候导入的pod文件 因为用swift的必须用use_frameworks!会把静态库编译成framewor...
    开心一刻_阅读 292评论 0 0
  • 也许是真的停得太久了吧。 最近和朋友在一起聊天的时候,总有“时间过得真快,几年前如何如何”之类的感叹,感觉除了年龄...
    manbanpaiing阅读 225评论 2 2
  • 1 啊可以大家一起养鱼 出去卖 2 大家一去去卖草药 3 创业卖苞米 1.创办农家乐的相关程序 各个地方创办农家乐...
    精神分裂_5d2b阅读 251评论 0 0