I am asking this , because sometimes(rarely) my query doing multiple additions.
I am opening db every page at the top.
var db = SQLite.openDatabase({name:'appdb.db',createFromLocation: '~appdb.db'})
my query is
db.transaction((tx) => {
tx.executeSql('INSERT INTO messages (chatID,messageID,senderID,message,uri,type,date)'+
'values(?,?,?,?,?,?,?)',[this.state.chatID,data.messageID,data.senderID,data.message,data.uri,data.type,data.date], (tx, results) => {
});
});
I think about convert to this for every query
import db from '../Classes/db';
db.open();
db.transaction((tx) => {
tx.executeSql('INSERT INTO messages (chatID,messageID,senderID,message,uri,type,date)'+
'values(?,?,?,?,?,?,?)',[this.state.chatID,data.messageID,data.senderID,data.message,data.uri,data.type,data.date], (tx, results) => {
});
});
db.close();
Related
I am working with the database in ionic, I call one API that returns me a number of records, I have to insert those records into the database and when to insert operations are completed then I want to call select records from the database. problem is asynchronous behavior, the select records from the database called before the insert operations are completed. can anyone help me to resolve this? my code is below...
DbProvider:
export class DbProvider {
public addData(dId: string, sId: string, subId: string, subName: string,
dDate: string, cId: string, cName: string, stdId: string, stdName: string,
) {
return new Promise((resolve, reject) => {
this.db.executeSql("INSERT INTO data (dId , sId , subId , subName ," +
" dDate , cId , cName , stdId , stdName ) VALUES (?, ?,?, ?,?, ?,?, ?,?)",
[dId, sId, subId, subName, dDate, cId, cName, stdId, stdName]).then((data) => {
resolve(data);
}, (error) => {
reject(error.tostring());
});
});
}
}
database insert and call
for (let temp of ApiData) {
this.DbHandler.IsAvailable(temp.dId).then(data => {
if (data) {
console.log("did Available editing " + data);
this.DbHandler.editData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName);
} else {
console.log("did not Available inserting " + data);
this.DbHandler.addData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName);
}
});
}
this.getDataFromDb();
I have multiple INSERTs I'd like to be done before starting SELECT requests. My problem is that the INSERT is not yet finished when the SELECT fires.
Call read from DB in then method:
let promises = [];
for (let temp of ApiData) {
let promise = new Promise((resolve, reject) => {
this.DbHandler.IsAvailable(temp.dId).then(data => {
if (data) {
console.log("did Available editing " + data);
this.DbHandler.editData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName).then(() => resolve());
} else {
console.log("did not Available inserting " + data);
this.DbHandler.addData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName).then(() => resolve());
}
});
};
promises.push(promise);
}
Promise.all(promises).then(() => this.getDataFromDb());
I have a NodeJS application which is my server and I created a Database class to help me handle querying my SQL DB. If I send requests a second between each other, everything runs fine.. no problems.. But if I start spamming requests to my server it crashes due to Error: Cannot enqueue Quit after invoking quit.
Here's my query function inside my Database class
static query(query: string): Promise<any> {
console.log('Query: ' + query);
return new Promise((resolve, reject) => {
this.connect().then(success => {
sqlConn.query(query, (err, results) => {
if (err) { return reject(err);
} else {
return resolve(results);
}
});
}).catch(err => {
return reject(err);
}).then( () => {
if (sqlConn.state !== 'disconnected') {
sqlConn.end();
}
});
});
};
and here's the this.connect() function
static connect(): Promise<any> {
return new Promise((resolve, reject) => {
sqlConn = mysql.createConnection(this.connectionData);
sqlConn.connect(err => {
if (err) { return reject(err); } else {
return resolve('SQL connection established');
}
});
});
};
I'm pretty sure the problem appears sometimes, it would still be
processing one query, and then another query comes before the first
one finishes, so it would call sqlConn.end() twice, even when it's
already disconnected? Any help is greatly appreciated...
> Main goal is for the query to wait till it's 100% done before it runs
the next one..
You can simplify your code by using the npm module mysql and use it's built-in connection pool.
From the documentation:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
You can, of course, create your own function that promisifies that call like this:
function query (sql) {
return new Promise((resolve, reject) => {
pool.query(sql, (error, results, fields) =>
error ? reject(error) : resolve({ results, fields });
};
}
If you really wants to use this approach then please use eachSeries function of async library.
var chunkedArray= [];
async.eachSeries(chunkedArray, startUpload, endUpload);
funtion startUpload(data,cb){
//iterate over every single item in array 1 at a time
}
function endUplaod(err){
//finally call this
}
This might help:-
https://caolan.github.io/async/docs.html#eachSeries
But i rather suggest you to use pooling of connection which make less overhead on your db and you can use your mysql more efficiently then making multiple connection.
// Load module
var mysql = require('mysql');
// Initialize pool
var pool = mysql.createPool({
connectionLimit : 10,
host : '127.0.0.1',
user : 'root',
password : 'root',
database : 'db_name',
debug : false
});
module.exports = pool;
I have a lot of problem using the mssql npm library.
Here is my db class :
"use strict"
const mssql = require('mssql');
const moment = require("moment");
let pool_historian = null;
let connexion_historian = null;
function mapBarChart(array) {
return new Promise((resolve, reject) => {
let result = {}
result.old = []
result.this = []
let currentYear = null;
for (let i = 0; i < array.length; i++) {
if (parseInt(moment().format("YYYY")) !== array[i].Annees) {
result.old.push(array[i]);
} else {
result.this.push(array[i]);
}
}
resolve(result);
})
};
class Historian {
constructor(data) {
pool_historian = new mssql.ConnectionPool({
server: data.host,
user: data.username,
password: data.password,
database: data.historian_db,
pool: {
max: 50,
min: 1
}
});
}
getBarChart(sensor, from, to) {
return pool_historian.connect().then(connector => {
return connector.query`SELECT Annees=YEAR(DateTime),Mois=MONTH(DateTime), Valeur=ROUND(sum(AnalogHistory.Value),2) FROM AnalogHistory WHERE AnalogHistory.TagName IN (${sensor}) AND Quality = 0 AND wwVersion = 'Latest' AND wwRetrievalMode = 'Full' AND DateTime >= ${from} AND DateTime <= ${to} AND AnalogHistory.Value > 0 GROUP BY YEAR(AnalogHistory.DateTime),MONTH(AnalogHistory.DateTime) ORDER BY Annees, Mois`.then(result => {
connector.close();
return mapBarChart(result.recordset).then(result => { return result });
//return result.recordset;
}).catch(err => {
return err;
})
})
}
getLineChart() {
return pool_historian.connect().then(connector => {
let variable = "A_000000000000000000000000000045.PV";
return connector.query`SELECT Annees=YEAR(DateTime),Mois=MONTH(DateTime),day=DAY(DateTime), Valeur=ROUND(sum(AnalogHistory.Value),2) FROM AnalogHistory WHERE AnalogHistory.TagName IN (${variable}) AND Quality = 0 AND wwVersion = 'Latest' AND wwRetrievalMode = 'Cyclic' AND DateTime >= '20160101 00:00:00.000' AND DateTime <= '20170809 00:00:00.000' AND AnalogHistory.Value > 0 GROUP BY YEAR(AnalogHistory.DateTime),MONTH(AnalogHistory.DateTime), Day(AnalogHistory.DateTime)ORDER BY Annees, Mois`.then(result => {
connector.close();
return result.recordset;
}).catch(err => {
return err;
})
})
}
close() {
pool_historian.close()
}
}
This class is used in this "business class" :
const Historian = require(`${__dirname}/historian-query`)
const Fdedb = require(`${__dirname}/fdedb-query`)
const moment = require('moment');
moment.locale("fr-FR");
class Graph_Tasks {
constructor() {
this.historian = new Historian({ host: "192.168.1.16", username: "******", password: "w***", historian_db: "R******e" })
this.fdedb = new Fdedb({ host: "192.168.1.16", username: "*****", password: "*****", fde_db: "S*****" })
}
createGraphForBuilding(code) {
return new Promise((resolve, reject) => {
this.fdedb.getList(code).then(list => {
console.log(list)
let datas = [];
//Foreach item on the list perform these 2 queryes
Promise.all([this.historian.getLineChart("A_000000000000000000000000000045.PV", moment().subtract(1, "years").startOf("year").format(), moment().format()), this.historian.getBarChart("A_000000000000000000000000000045.PV", moment().subtract(1, "years").startOf("year").format(), moment().format())]).then(results => {
let datas = []
datas = { "lineChart": null, "barChart": results[0] };
console.log(datas)
res.render('index', { title: 'WebGraph', message: 'Yo Yo', datas });
})
console.log(datas)
resolve(datas)
}).catch(console.log);
});
}
}
module.exports = Graph_Tasks;
As you can see, what I'm trying to do is performing a simultaneous database request. As I read in the documentation, the connection pool must let me do this properly. So when the program arrives at Promise.all, I expected that the 2 requests will be launched simultaneously.
But I get an error :
Une exception s'est produite : Error
Promise Rejection (ConnectionError: Already connecting to database! Call close before connecting to different database.)
ConnectionError: Already connecting to database! Call close before connecting to different database.
at ConnectionError (d:\repositories\fde\node_modules\mssql\lib\base.js:1428:7)
at ConnectionPool._connect (d:\repositories\fde\node_modules\mssql\lib\base.js:235:23)
at EventEmitter.connect.PromiseLibrary (d:\repositories\fde\node_modules\mssql\lib\base.js:217:19)
at ConnectionPool.connect (d:\repositories\fde\node_modules\mssql\lib\base.js:216:12)
at Historian.getBarChart (d:\repositories\fde\class\historian-query.js:39:31)
at __dirname.createGraphForBuilding.Promise.fdedb.getList.then.list (d:\repositories\fde\class\graph_tasks.js:21:188)
at process._tickCallback (internal/process/next_tick.js:109:7)
So my question is: how to adapt the code to let me perform several queries at the same time (the promise.all for each of my list item)?
The issue is that you can't open multiple connection pools to the same server (I assume Fdedb is opening another connection because you didn't include code for it). If, say, you were pulling that data from two different servers then opening two connection pools would be appropriate- I've run into that use case before. But it looks like your two databases are on the same server (localhost), so it's better to open just the one connection and pass it to your objects to make the query. You can touch multiple databases on the same host using plain old SQL, see: How do I query tables located in different database?
I have connected MSSQl with node.js using below code
var sql = require('mssql')
var config ={
server:'xxxx',
database:'xxxx',
user:'xxx',
password:'xxx',
port:'xx'
};
sql.connect(dbconfig, function (err) {
if (err) console.log(err);
var sqlquery='';
const request = new sql.Request();
if(condition)
{
//query to the database and get the repo value
sqlquery='select * from verylargetable';
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var repo=result.recordset[0].Repo;
//query to the database and get the comm value
sqlquery="select commit from verylargetable where Repo='"+repo+"'";
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var comm=result.recordset[0].Comm;
if (result.recordset.length > 0)
{
//query to the database and update the table
sqlquery="UPDATE verylargetable set Repo='"+repo+"', WHERE Comm='"+comm+"'";
request.query(sqlquery, function (err,result){
if (err) console.log(err)
console.log("record(s) updated");
});
}
});
});
}
else
{
//query to the database and get the repo value
sqlquery='select * from verylargetable';
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
var repo=result.recordset[0].Repo;
//query to the databaseto insert new record
sqlquery ="INSERT INTO verylargetable VALUES("+repo+"','"+comm+"',1)";
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
});
});
}
});
Based on some conditions have to execute queries. while executing, these queries are updated properly.but sometimes facing below issue
global connection already exists. call sql.close() first.
when i used sql.close() at the end i couldn't initialize connection next time.
Facing connection is closed issue.
I have separate method for database changes.I need to establish the connection at the start of the method and have to close at the end.In between have to execute all the queries.
please let me know how to open and close the connection properly?
sql.connect returns connection, in order to close the connection, just call close on the result.
var pool = sql.connect(dbconfig, function (err) {
if (err) console.log(err);
const request = new sql.Request()
var sqlquery='select * from verylargetable';
request.query(sqlquery, function (err, result) {
if (err) console.log(err)
});
});
pool.close();
I was facing the same issue and solved it with below implementation,
For handling global connection already exists. call sql.close() first we need to create a connection pool for mssql npm version 4.0.4
const config = {
user: '**',
password: '**',
server: '** or localhost',
database: '**',
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}}
router.get('/SQLConnection1', function (req, res, next) {
const pool1 = new sql.ConnectionPool(config, err => {
// ... error checks
// Query
pool1.request() // or: new sql.Request(pool1)
.query('SELECT ID,Name from Avatar', (err, result) => {
// ... error checks
console.dir(result)
res.json({'Result': result})
})
})
pool1.on('error', err => {
// ... error handler
console.log('Error ' + err);
})
});
So I am looking to model our existing redis data into aerospike. One requirement that we have is to be able to get all the keys for a given user. For eg., say we have keys such as <id>:<timestamp>. Now, at some point in time, I need to get all keys for the given id, where I would require something like a prefix search across all keys in the aerospike namespace (which are indexed) to get the values for all <id>:<timestamp> keys. Would like to know if this is possible, and if yes, how.
You cannot do a query on key prefix directly. The server only stores the key digest, so the key value (<id>:<timestamp> in your case) doesn't get indexed.
The way to model this would be to add the <id> part of your key as a separate record bin. Then you can index that bin and run a query on it.
Here is a simple example - it's using the Aerospike Node.js client but the concept is the same no matter what client you prefer:
const Aerospike = require('aerospike')
const ns = 'test'
const set = 'demo'
// connect to cluster and create index on 'id' bin
var client = Aerospike.client()
client.connect((err) => {
assertOk(err, 'connecting to cluster')
createIndex('id', 'id_idx', Aerospike.indexDataType.STRING, () => {
// create a new sample record
var userId = 'user1'
var ts = new Date().getTime()
var key = new Aerospike.Key(ns, set, `${userId}:${ts}`)
var record = { id: userId, value: Math.random() }
client.put(key, record, (err) => {
assertOk(err, 'write record')
// query for records with matching 'id'
var query = client.query(ns, set)
query.where(Aerospike.filter.equal('id', userId))
var stream = query.foreach()
stream.on('error', (error) => assertOk(error, 'executing query'))
stream.on('end', () => client.close())
stream.on('data', (record, meta, key) => {
console.log(record)
})
})
})
})
function assertOk (err, message) {
if (err) {
console.error('ERROR: %s - %s', message, err)
process.quit()
}
}
function createIndex (bin, name, datatype, callback) {
var index = {
ns: ns,
set: set,
bin: bin,
index: name,
datatype: datatype
}
client.createIndex(index, (err, job) => {
assertOk(err, 'creating index')
job.waitUntilDone(100, (err) => {
assertOk(err, 'creating index')
callback()
})
})
}
Hope this helps!