最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nodejs中關(guān)于mysql數(shù)據(jù)庫的操作

 更新時(shí)間:2022年08月04日 10:59:22   作者:燈光下的投影  
這篇文章主要介紹了nodejs中關(guān)于mysql數(shù)據(jù)庫的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

基本概念

為什么要有數(shù)據(jù)庫

沒有數(shù)據(jù)庫,我們的數(shù)據(jù)都是存儲(chǔ)在文件當(dāng)中的,那么文件存儲(chǔ)數(shù)據(jù)的缺點(diǎn)有:

  • 文件的安全性問題。
  • 文件不利于查詢和對(duì)數(shù)據(jù)的管理。
  • 文件不利于存放海量數(shù)據(jù)
  • 文件在程序中控制不方便

什么是數(shù)據(jù)庫

數(shù)據(jù)庫,簡(jiǎn)而言之可視為電子化的文件柜——存儲(chǔ)電子文件的處所,用戶可以對(duì)文件中的數(shù)據(jù)運(yùn)行增加、刪除、修改、查詢等操作。

前端程序員只需要對(duì)數(shù)據(jù)庫有一定了解即可。

瀏覽器---->服務(wù)器---->數(shù)據(jù)庫

數(shù)據(jù)庫的分類

關(guān)系型數(shù)據(jù)庫:

  • MySQL、
  • Oracle、
  • SQL Server
  • SQLite(安卓)

非關(guān)系型數(shù)據(jù)庫

  • mongodb
  • redis
  • BigTable
  • DBA

數(shù)據(jù)庫中基本術(shù)語

  • 數(shù)據(jù)庫database:存放數(shù)據(jù)的倉庫,一般一個(gè)項(xiàng)目中的數(shù)據(jù)會(huì)存儲(chǔ)到一個(gè)數(shù)據(jù)庫中
  • 表table: 一個(gè)表對(duì)應(yīng)一類數(shù)據(jù),比如學(xué)生表,老師表
  • 列columns:一張表由多列組成,也叫一個(gè)字段,比如學(xué)生的姓名,成績(jī),年齡等
  • 行rows: 一個(gè)學(xué)生信息對(duì)應(yīng)一行,一行也叫一條記錄。

數(shù)據(jù)庫的可視化操作(創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表)

數(shù)據(jù)類型(部分)

  • int: 整數(shù)類型
  • varchar: 字符類型
  • datetime: 日期類型

數(shù)據(jù)庫的常見命令

SQL: 結(jié)構(gòu)化查詢語言(Structured Query Language)簡(jiǎn)稱SQL 。用于數(shù)據(jù)庫的增刪改查以及管理等功能。

數(shù)據(jù)庫相關(guān)

--SQL中的注釋

  • SHOW DATABASES; 查看所有的數(shù)據(jù)
  • CREATE DATABASE mydb; 創(chuàng)建數(shù)據(jù)庫
  • DROP DATABASE mydb;刪除數(shù)據(jù)庫
  • USE mydb; 使用數(shù)據(jù)庫

表相關(guān)

  • SHOW TABLES;查看當(dāng)前數(shù)據(jù)庫中所有的表

創(chuàng)建表

CREATE TABLE user(
?? ?id INT auto_increment PRIMARY KEY,
?? ?name VARCHAR(255) NOT NULL,
?? ?age INT ,
?? ?gender VARCHAR(4),
?? ?content VARCHAR(255)
)
  • DROP TABLE user;刪除表

插入數(shù)據(jù)

INSERT INTO user (name, age, gender, content) VALUES ('胡聰聰', 18, '男', '哈哈哈,哈哈哈')
-- 如果省略列名,那么必須要和字段一一對(duì)應(yīng)
INSERT INTO user VALUES (null, '胡聰聰', 18, '男', '哈哈哈,哈哈哈')
?
INSERT INTO user SET name='hcc', age=18, gender='男', content='嘻嘻嘻'

修改數(shù)據(jù)

// 修改所有的數(shù)據(jù)
UPDATE USER SET name='胡西西'
// 根據(jù)條件修改
UPDATE USER set name='胡聰聰', content="這是內(nèi)容" WHERE id = 2

刪除數(shù)據(jù)

// 刪除所有的數(shù)據(jù)
DELETE FROM USER
?
// 刪除id為5的數(shù)據(jù)
DELETE FROM USER WHERE id = 5

查詢數(shù)據(jù)

-- 查詢所有數(shù)據(jù)
SELECT * FROM user
?
-- 查詢指定列
SELECT id, name,age from user

條件查詢

--- 并且
SELECT * from user where name='胡聰聰' AND age=21
?
--- 或者
SELECT * from user where name='胡聰聰' or age=21
?
-- 范圍查詢
?
-- 模糊查詢 ?%表示通配 ?_表示單個(gè)字符
SELECT * from user where name LIKE '胡%'
?
-- in語句
SELECT * from user where name in ('胡聰聰', 'hcc')
?
-- order by
-- 排序需要寫在最后面,,asc升序 ?desc:降序
SELECT * from user ORDER BY id desc
?
-- limit分頁
SELECT * from user ORDER BY id desc limit 3
SELECT * from user ORDER BY id desc limit 3,3
?
?
-- 獲取總條數(shù)
SELECT count(*) as total FROM user

導(dǎo)入和導(dǎo)出數(shù)據(jù)庫腳本

node操作mysql

基本使用

安裝

npm install mysql

基本使用

// 導(dǎo)入第三方包
const mysql = require('mysql')
// 創(chuàng)建連接
var connection = mysql.createConnection({
? // 本地
? host: 'localhost',
? user: 'root',
? password: 'root',
? // 數(shù)據(jù)庫名稱
? database: 'mydb',
? port: 3306
})
?
// 連接數(shù)據(jù)庫
connection.connect()
?
// 執(zhí)行sql語句
connection.query('select * from user where id = 8', (err, result) => {
? if (err) return console.log('查詢失敗', err)
? // result返回的是數(shù)組, 數(shù)組中是一個(gè)對(duì)象
? console.log(result)
})
?
// 關(guān)閉連接
connection.end()

查詢語句

var name = '胡聰聰'
// 使用?表示占位,可以防止sql注入
connect.query(`select * from user where name=?`, name, (err, result) => {
? if (err) return console.log('錯(cuò)誤了', err)
? console.log(result)
})

插入語句

connect.query(
? 'insert into user (name, age, gender, content) values (?, ?, ?, ?)',
? ['胡嘻嘻', 18, '男', '哈哈哈哈'],
? err => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了')
? }
)
?
// 方式2
connect.query(
? 'insert into user set ?',
? {
? ? name: '胡洗洗',
? ? age: 30,
? ? gender: '男',
? ? content: '哈哈哈'
? },
? (err, result) => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了', result)
? }
)

修改語句

connect.query(
? 'update user set ? where id = ?',
? [
? ? {
? ? ? name: '胡洗洗',
? ? ? age: 30,
? ? ? gender: '男',
? ? ? content: '哈哈哈'
? ? },
? ? 10
? ],
? (err, result) => {
? ? if (err) return console.log('錯(cuò)誤', err)
? ? console.log('添加成功了', result)
? }
)

刪除語句

connect.query('delete from user where id = ?', [10], (err, result) => {
? if (err) return console.log('失敗', err)
? console.log(result)
})

db模塊封裝

// 導(dǎo)入mysql
const mysql = require('mysql')
?
// 創(chuàng)建連接對(duì)象
const connect = mysql.createConnection({
? host: 'localhost',
? port: 3306,
? user: 'root',
? password: 'root',
? database: 'mydb'
})
?
exports.query = function(sql, params, callback) {
? connect.connect()
? connect.query(sql, params, (err, data) => {
? ? callback && ?callback(err, data)
? })
? connect.end()
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評(píng)論

海南省| 雷波县| 泾阳县| 临邑县| 惠东县| 崇阳县| 桂平市| 张家界市| 怀柔区| 渭南市| 修武县| 湘阴县| 永福县| 吉安市| 那坡县| 北碚区| 安康市| 崇礼县| 吉安市| 武平县| 玉山县| 正宁县| 泗水县| 太康县| 集贤县| 宜宾市| 从江县| 大埔县| 白河县| 康马县| 常宁市| 广昌县| 墨玉县| 榆中县| 洛浦县| 津市市| 云和县| 齐齐哈尔市| 鸡西市| 涟源市| 开平市|