node對資料庫的操作

2021-10-08 15:15:28 字數 1335 閱讀 2066

想要運算元據庫,首先就要建立且連線它

1.匯入mysql模組

var mysql = require('mysql');
2建立資料庫連線物件,配置相關資訊

var connection = mysql.createconnection();
3建立資料庫連線,可以省略

connection.connect();
4修改資料

connection.query('update students set name = "奈奈子",score = 100 where id = 1', (error, results, fields) => );

module.exports = (sql, callback) => );

}

關閉資料庫:可以省略,但如果寫的話,不可以直接寫在最後面,因為暴露出去的介面方法是非同步的,不能先關閉資料庫

connection.end();
1.查詢資料庫

語法: select * from 表名 --》查詢所有

select * from 表名 where 條件語句 --》條件查詢

connection.query('select * from students',(error,results,fields) => );
插入資料

語法:insert into 表名(要插入資訊的欄位名) value(字段對應的資訊)

connection.query('insert into students (name,age,score) values("張三",19,59)',(error,results,fields) =>);
刪除資料

語法:delete from 表名 where 要刪除的條件

delete from 表名 --》刪除表

connection.query('delete from students where id in(2,3,4) ',(error,results,fields) =>);
修改資料

語法:update 表名 set 欄位名=修改後的名字(這個可以有多個) where 要修改的欄位名id或者其他條件

connection.query('update students set name="奈奈子",score=100 where id = 1 ',(error,results,fields) =>);

對資料庫的操作

資料庫的檔案 資料檔案 包含資料和物件,主資料檔案建議副檔名.mdf,其餘.ndf 事務日誌檔案 包含恢復資料庫中的所有事物所需的資訊,建議副檔名.ldf 至少包含乙個資料檔案和乙個日誌檔案 對資料庫的操作 選其中乙個,可以選 選其中乙個,必選 分割,表示多個選項 表示說明 建立create dat...

常規對資料庫庫的操作

執行如下命令,檢視系統庫 show databases information schema 虛擬庫,不占用磁碟空間,儲存的是資料庫啟動後的一些引數,如使用者表資訊 列資訊 許可權資訊 字元資訊等 performance schema mysql 5.5開始新增乙個資料庫 主要用於收集資料庫伺服器效...

Spring 對資料庫操作

1.spring的資料庫訪問方式 提供了對jdbc,mybatis,hibernate等的支援。主要通過dao提供了如下的支援 a.提供了與平台無關的資料庫訪問異常體系 b.提供了資料訪問的模板,比如 jbdctemplate,jdbctemplate,hibernatetemplate c.提供了...