From f0a7fa0c5987303b5e7f59ffd4f8de49d255d5b3 Mon Sep 17 00:00:00 2001 From: Jay Wen Date: Fri, 12 Nov 2021 14:16:35 +0800 Subject: [PATCH] execute simple sql encapsulate as an function --- tests/examples/nodejs/node-example-raw.js | 44 +++++++++-------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/tests/examples/nodejs/node-example-raw.js b/tests/examples/nodejs/node-example-raw.js index 058a50c4c3..80d5cd1bde 100644 --- a/tests/examples/nodejs/node-example-raw.js +++ b/tests/examples/nodejs/node-example-raw.js @@ -23,43 +23,31 @@ var conn = taos.connect({host:"127.0.0.1", user:"root", password:"taosdata", con // Initialize our TDengineCursor, which we use to interact with TDengine var c1 = conn.cursor(); +//execute the sql +function execute(sql){ + try { + c1.execute(sql); + } + catch(err) { + conn.close(); + throw err; + } +} + // c1.execute(query) will execute the query // Let's create a database named db -try { - c1.execute('create database if not exists db;'); -} -catch(err) { - conn.close(); - throw err; -} +execute('create database if not exists db;') // Now we will use database db -try { - c1.execute('use db;'); -} -catch (err) { - conn.close(); - throw err; -} +execute('use db;') + // Let's create a table called weather // which stores some weather data like humidity, AQI (air quality index), temperature, and some notes as text -try { - c1.execute('create table if not exists weather (ts timestamp, humidity smallint, aqi int, temperature float, notes binary(30));'); -} -catch (err) { - conn.close(); - throw err; -} +execute('create table if not exists weather (ts timestamp, humidity smallint, aqi int, temperature float, notes binary(30));'); // Let's get the description of the table weather -try { - c1.execute('describe db.weather'); -} -catch (err) { - conn.close(); - throw err; -} +execute('describe db.weather'); // To get results, we run the function c1.fetchall() // It only returns the query results as an array of result rows, but also stores the latest results in c1.data -- GitLab