Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //assuming i have database:
- Person_table(ID int A_I, NAME varchar, AGE int)
- //code:
- var p = new Person("John", 22);
- p.writeToDatabase();
- //the object is now written in database
- var mysql = require('mysql');
- var conn = mysql.createPool({
- host : 'localhost',
- database: 'db_name',
- user: 'user_name',
- password : 'pass',
- multipleStatement: true,
- connectionLimit : 10
- });
- conn.getConnection(function(err){
- if(err) throw err;
- });
- module.exports = conn;
- var db = require('./db.js');
- function Person(n, a) {
- this.name = n;
- this.age = a;
- }
- Person.prototype.writeToDatabase = function (callback) {
- db.query("INSERT INTO Person_table(NAME, AGE) VALUES(?,?)", [this.name, this.age], function (err, rows) {
- if (err) return callback(err);
- else return callback(null, rows);
- });
- }
- module.exports = Person;
- var Person = require('./person.js')
- var p = new Person("John", 22);
- p.writeToDatabase(function(err, rows){
- if(err) console.log(err);
- else console.log("written to DB");
- });
Add Comment
Please, Sign In to add comment