SHARE
TWEET

NodeJS : Simpan Data Query MYSQL ke File TXT Berformat JSON

stevennathaniel Jun 15th, 2016 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by StevenNathaniel on 16-Jun-16.
  3.  */
  4.  
  5. var mysql = require('mysql');
  6.  
  7. const fs = require('fs');
  8.  
  9. var koneksi = mysql.createConnection({host: 'localhost', database: 'latihan', user: 'root', password: ''});
  10.  
  11. koneksi.connect(function(error) {
  12.  
  13.     if (error) {
  14.  
  15.         console.error('gagal tersambung: ' + error.stack);
  16.  
  17.         return;
  18.     }
  19.  
  20.     koneksi.query('SELECT * FROM pegawai', function (error, rows, field){
  21.  
  22.         if (!error)
  23.  
  24.             //console.log(JSON.stringify(rows));
  25.  
  26.             fs.writeFile("D:/StevenNathaniel/ProyekNodeJS/Webstorm1/test1.txt", JSON.stringify(rows));
  27.  
  28.  
  29.         else
  30.  
  31.             console.log('Terjadi error: ', error.stack)
  32.  
  33.         return;
  34.     })
  35.  
  36.     koneksi.end(function(error) {
  37.         if(error) {
  38.  
  39.             console.error('gagal memutus koneksi: ' + error.stack)
  40.         }
  41.     })
  42. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top