Curs Javascript

- Delete cu Node.js in MySQL

Metoda query() a modulului "mysql" poate efectua interogari SQL care actualizeaza (Update) si sterg (Delete) inregistrari in tabelul mysql.
Pentru a evita "SQL injection", este indicat sa filtrati datele furnizate de utilizator inainte de a le folosi in interiorul unei interogari SQL. Puteti face acest lucru folosind metoda mysql.escape().
Alternativ, puteti folosi caractere '?' ca inlocuitori pentru valorile pe care doriti sa le filtrati, apoi adaugati valorile intr-un array ca al doilea argument a metodei query() (in aceeasi ordine ca si inlocuitorii '?' din interogarea sql).

Update cu Node.js in MySQL

Puteti actualiza inregistrari existente intr-un tabel MySQL folosind instructiunea "UPDATE".
Pentru a obtine numarul de randuri afectate/actualizate, se foloseste proprietatea affectedRows a obiectului rezultat.

- In acest exemplu vom actualiza datele din coloana "address" din randul cu id-ul 5, folosind metoda mysql.escape() pentru a filtra datele in interogarea SQL:
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

//update with escape()
var adr ='Happy 12';
var id =5;
let sql ='UPDATE friends SET address ='+ mysql.escape(adr) +' WHERE id ='+ mysql.escape(id);

pool.getConnection((err, con)=>{
  if(err) throw err;

  con.query(sql, (err, res)=>{
    if(err) throw err;
    con.release(); //release the connection
    console.log('Record(s) updated: '+ res.affectedRows);
  });
});
- In urmatorul cod se folosesc inlocuitori '?' in instructiunea Update:
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

//update using placeholders
let sql ='UPDATE friends SET address =? WHERE id =?';
var placeh =['Happy 12', 5]; //values for placeholders

pool.getConnection((err, con)=>{
  if(err) throw err;

  con.query(sql, placeh, (err, res)=>{
    if(err) throw err;
    con.release(); //release the connection
    console.log('Record(s) updated: '+ res.affectedRows);
  });
});
- O alta metoda cu inlocuitori '?', adaugarea numelor coloanelor si a valorilor intr-un obiect:
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

//update using placeholders
let sql ='UPDATE friends SET ? WHERE ?';
var adr ={address:'Happy 12'};
var id ={id:5};

pool.getConnection((err, con)=>{
  if(err) throw err;

  con.query(sql, [adr, id], (err, res)=>{
    if(err) throw err;
    con.release(); //release the connection
    console.log('Record(s) updated: '+ res.affectedRows);
  });
});
Resultat:
Record(s) updated: 1

Delete cu Node.js in MySQL

Puteti sterge inregistrari dintr-un tabel MySQL folosind instructiunea "DELETE FROM".
Pentru a obtine numarul randurilor afectate/sterse, se foloseste proprietatea affectedRows a obiectului resultat.
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

//deleted with placeholders
let sql ='DELETE FROM friends WHERE id =? OR name =?';
var id =2;
var name='Olpram';

pool.getConnection((err, con)=>{
  if(err) throw err;

  con.query(sql, [id, name], (err, res)=>{
    if(err) throw err;
    con.release(); //release the connection
    console.log('Rows deleted: '+ res.affectedRows);
  });
});
Resultat:
Rows deleted: 2

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Ce tag adauga imagine in pagina web?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Care din aceste coduri CSS afiseaza textul inclinat?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Clic pe functia jQuery care ascunde animat un element HTML.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Clic pe functia definita corect in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Indicati forma pentru Prezent Perfect Continuu a verbului "to walk" (a merge).
have walked have been walking be walking
I have been walking for 5 hours.
- Merg pe jos de 5 ore.
Indicati Trecutul Nedefinit pentru verbul "ser" (a fi) la forma Yo.
será sería fui
Yo fui entrenador.
- Am fost antrenor.
Node.js - Actualizare si Stergere date in tabel MySQL

Last accessed pages

  1. Ser - Estar 2 (3296)
  2. Gramatica limbii spaniole. Indrumator si prezentare generala (66985)
  3. Adverbele in limba engleza - Adverbs (32510)
  4. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (268696)
  5. Accentul si Pronuntia (27427)

Popular pages this month

  1. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (1359)
  2. Exercitii engleza - English Tests and exercises - Grammar (1085)
  3. Prezentul simplu si continuu - Present Tense Simple and Continuous (1079)
  4. Gramatica limbii engleze - Prezentare Generala (1030)
  5. Prezentul simplu - Exercitii si teste incepatori (887)