I'm inserting JSON data into MariaDB using NodeJs. Getting below error while inserting data. Please advise what cause to get error. Actually Column data1 no empty or null values.Why am i getting below error ?
{ [Error: Column 'data1' cannot be null] code: 1048 }
Table Structure
CREATE TABLE `from_excel` (
`ID` INT(11) NOT NULL AUTO_INCREMENT,
`data1` VARCHAR(50) NULL DEFAULT NULL,
`data2` VARCHAR(100) NULL DEFAULT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
Code which i'm using to insert data.
var Client = require('mariasql');
var c = new Client({
host : 'localhost',
user : 'xxxx',
password : 'xxxx',
db : 'Metrics'
});
const workbook = xlsx.readFile(__dirname + '/test.xlsx');
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
var json=xlsx.utils.sheet_to_json(worksheet);
console.log(json.length);
for(var i=0;i<json.length;i++)
{
var post = {data1: json[i].data1, data2: json[i].data2};
var sql = c.query('INSERT INTO elements_from_excel (data1,data2) VALUES (?,?)', post, function(err, result) {
console.log(sql);
if(err){console.log(err);}
else {console.log("success");}
});
}
c.end();
What could be happening is that the resulting insert statement being run is as follows:
INSERT into from_excel (data1, data2) VALUES (`data1` = \'data1value\', `data2` = \'value\', ?)
Try replacing the query string with the following instead:
var post = {data1: json[i].data1, data2: json[i].data2};
var sql = c.query('INSERT INTO from_excel SET ?', post, function(err, result) {
console.log(sql);
if(err){console.log(err);}
else {console.log("success");}
It should be INSERT INTO from_excel VALUES (?), although it's quite possible that you'll encounter other errors when you fix this one.
Make sure the function you are calling receive the exact type of data they expect.
Related
I have a requirement to receive JSON data in a Stored Proc parameter and insert the same in the snowflake target table (user_json_feedback). JSON Data has three key elements(User, EntityID, and Entity Type), whereas the target table has five columns (User, ID, Entity Type, Region, and Date). The region will have a default value of "NA," and the date will be the current date.
If the inserts are successful, it returns true; otherwise, it returns false.
I am struggling with the syntax and parsing issues here, as I am very new to writing procedures.
Here is what I have been trying to do, which is giving me errors obviously but serves the algorithm of my intent.
CREATE OR REPLACE SP_UPDATE_JSON_DATA (JSON_DATA VARIANT)
RETURNS BOOLEAN
LANGUAGE JAVASCRIPT
EXECUTE AS OWNER
AS
$$
//Declare variables
var REGION = 'NA'
var V_DATE = `select current_date;`;
var DATE_STMT= snowflake.createStatement({sqlText: V_DATE });
var curr_date = DATE_STMT.execute();
var src_json = JSON.parse(JSON_DATA);
var sql_command =
`INSERT INTO user_json_feedback (user,id,etype,region ,date)//
select src_json:USER,src_json:ENTITY_ID,src_json:ENTITY_TYPE,REGION,curr_date;`;
try {
snowflake.execute (
{sqlText: sql_command}
);
return "Succeeded."; // Return a success/error indicator.
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
The function call with parameters will be like
call SP_UPDATE_JSON_DATA ('[{"USER":"XYZ","ENTITY_ID":"BMT0001","ENTITY_TYPE":"BMT"},{"USER":"ABC","ENTITY_ID":"BMT0002","ENTITY_TYPE":"BMT"}]');
Thanks in advance for the help!
theres a few things here.
Firstly the step to get current date. curr_date is a result set object. to extract the value and use it later, you need to read the first row with .next() then GetColumnValue to read the column content. to pass it later as a well formatted string you'll wanna convert with .toISOString().
Secondly the parsed json returns an array in this case so you'll need to iterate over the array to insert the individual records. As it's not known ahead of time if the variant will contain an array you're best checking if the parsed json is an array and handle it accordingly
Last tweak was altering the return type so you get the verbose feedback you're expecting from your return calls.
Updated code:
CREATE OR REPLACE TEMPORARY TABLE user_json_feedback
(
user VARCHAR(100)
,id VARCHAR(100)
,etype VARCHAR(100)
,region VARCHAR(100)
,date TIMESTAMP_NTZ
);
CREATE OR REPLACE TEMPORARY PROCEDURE SP_UPDATE_JSON_DATA(JSON_DATA VARIANT)
RETURNS STRING
LANGUAGE JAVASCRIPT
EXECUTE AS OWNER
AS
$$
//Declare variables
var REGION = 'NA'
var V_DATE = `select current_date;`;
var DATE_STMT= snowflake.createStatement({sqlText: V_DATE });
var DATE_STMT_RES = DATE_STMT.execute();
DATE_STMT_RES.next()
var curr_date = DATE_STMT_RES.getColumnValue(1).toISOString();
var src_json = JSON.parse(JSON_DATA);
try {
if (Array.isArray(src_json)){
for (key in src_json){
var sql_command =
`INSERT INTO user_json_feedback (user,id,etype,region,date)//
VALUES(:1,:2,:3,:4,:5)`;
snowflake.execute (
{
sqlText: sql_command,
binds: [src_json[key].USER,src_json[key].ENTITY_ID,src_json[key].ENTITY_TYPE,REGION,curr_date]
}
);
}
}
else {
var sql_command =
`INSERT INTO user_json_feedback (user,id,etype,region,date)//
VALUES(:1,:2,:3,:4,:5)`;
snowflake.execute (
{
sqlText: sql_command,
binds: [src_json.USER,src_json.ENTITY_ID,src_json.ENTITY_TYPE,REGION,curr_date]
}
);
}
return "Succeeded."; // Return a success/error indicator.
}
catch (err) {
return "Failed: " + err; // Return a success/error indicator.
}
$$;
--Need to cast variable string as variant.
--ARRAY example
call SP_UPDATE_JSON_DATA ('[{"USER":"XYZ","ENTITY_ID":"BMT0001","ENTITY_TYPE":"BMT"},{"USER":"ABC","ENTITY_ID":"BMT0002","ENTITY_TYPE":"BMT"}]'::VARIANT);
--Single object example
call SP_UPDATE_JSON_DATA ('{"USER":"JST","ENTITY_ID":"BMT0003","ENTITY_TYPE":"BMT"}'::VARIANT);
SELECT *
FROM user_json_feedback;
Result set:
While all this works, you may well be better served just inserting the whole variant into a table and relying on snowflake's significant semi-structured data querying capabilities. Certainly for large payloads you'll find much better performance from bulk loading to a variant column in a table then parsing in a view.
I'm writing migration script from PostgreSQL to Oracle.
I wanna retrieve CREATE TABLE script without any other data.
It should look like this:
CREATE TABLE public.actor (
actor_id integer DEFAULT nextval('public.actor_actor_id_seq'::regclass) NOT NULL,
first_name character varying(45) NOT NULL,
last_name character varying(45) NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL
);
But when i'm getting data by using:
let script = execSync('pg_dump -t \'actor\' --schema-only dvdrental', {encoding: 'utf-8'});
I receive the data that look like this:
--
-- PostgreSQL database dump
--
-- Dumped from database version 13.1
-- Dumped by pg_dump version 13.1
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: actor; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.actor (
actor_id integer DEFAULT nextval('public.actor_actor_id_seq'::regclass) NOT NULL,
first_name character varying(45) NOT NULL,
last_name character varying(45) NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.actor OWNER TO postgres;
--
-- Name: actor actor_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.actor
ADD CONSTRAINT actor_pkey PRIMARY KEY (actor_id);
--
-- Name: actor last_updated; Type: TRIGGER; Schema: public; Owner: postgres
--
CREATE TRIGGER last_updated BEFORE UPDATE ON public.actor FOR EACH ROW EXECUTE FUNCTION public.last_updated();
--
-- PostgreSQL database dump complete
--
Can you advice me how to get particular string by using split method or maybe some regexp.
Or maybe some command for getting script from database for CREATE TABLE to the ); range.
Assuming the format is always like this (e.g. it's always pgdump) you can iterate the text line by line and extract everything between CREATE TABLE and the closing paren:
function *createTableStatements(sql) {
let buf;
for (let line of sql.split('\n')) {
if (line.trim().startsWith('CREATE TABLE'))
buf = [line]
else if (buf) {
buf.push(line);
if (line.startsWith(');')) {
yield buf.join('\n');
buf = null;
}
}
}
}
//
sql = ....
for (let stmt of createTableStatements(sql)) {
// do something with stmt
}
To parse arbitrarily formatted SQL you need a specialized parser library, don't try it with string functions or regular expressions.
Ola!
I'm doing a node.js application. I have this table structure in my MySql DB:
My task is to get the table_4 names and the related table_7 names. I know the Table_1 username and password. Is it possible to create one query - using subselect - not calling all the time callback with a new select after getting values? - like below -
con.connect(function(err) {
if (err)
{throw err }
con.query("SELECT id FROM Table_1 WHERE username = xyz AND password = aaa",
function (err, result) {
if (err) {throw err};
con.query("SELECT table_3_id FROM table2 WHERE Table_1_id = "+(result)+")",
function(/*bla-bla*/){
};
});
}
);
Thanks!
Here it is how you can achieve that with one query :
SELECT Table_4.*
FROM Table_1 , Table_2 , Table_3 , Table_4
WHERE Table_1.username and Table_1.password and
Table_2.Table_1_id = Table_1.id and
Table_2.Table_3_id = Table_3.id and
Table_3.Table_4_id = Table_4.id
I couldn't found proper relations for Table_7 names. But I think you will get idea how to do it further from this.
I just talked to my lead dev, hwho gave me a correct solution - similar to #Vivek Doshi answer - :
SELECT Table_4.names, Table_7.names
FROM Table_1 , Table_2 , Table_3 , Table_4, Table_5, Table_6, Table_7
WHERE Table_1.username and Table_1.password and
Table_2.Table_1_id = Table_1.id and
Table_2.Table_3_id = Table_3.id and
Table_3.Table_4_id = table_4.id and
Table_3.table_5_id = table_5.id and
table_6.table_5_id = Table_5.id and
table_6.table_7_id = table_7.id;
i need how to get values from sqlite Database for particular columns records in Javascript, i successfully inserted values in DB but do no how to fetch the values from db
Here my sample code how i inserted :
var newPath1 = __dirname + path.sep+'schedule.sqlite'
var bfr = fs.readFileSync(newPath1)
var db = new sql.Database(bfr)
db.run("INSERT into
profile_id(SNo,visit_id,date,time,profile_url,First_Name,Last_Name) VALUES
(?,?,?,?,?,?,?)",
[result,result1,date1,time1,sampleUrl,first_Name,last_Name])
var data = db.export();
var buffer = new Buffer(data);
fs.writeFileSync(newPath1, buffer);
i need a solution from this DB i need to fetch all values from First_Name
Try like this,
var stmt = db.prepare("INSERT into
profile_id(SNo,visit_id,date,time,profile_url,First_Name,Last_Name) VALUES
(?,?,?,?,?,?,?)");
stmt.run(result,result1,date1,time1,sampleUrl,first_Name,last_Name);
stmt.finalize();
db.each("SELECT First_Name FROM profile_id", function(err, row) {
console.log("First Name : "+row.First_Name);
});
finally i found the solution
var fName = db.exec("SELECT First_Name FROM profile_id")
this code works
I have a Postgresql stored function defined as following:
CREATE OR REPLACE FUNCTION SessionGet(
sid varchar)
RETURNS "SESSION" AS $$
SELECT * FROM "SESSION" WHERE "SESSION_ID" = sid;
$$ LANGUAGE sql;
I call it from node using the PG module with:
SELECT SessionGet('SID-1'::varchar);
The session table is defined as:
CREATE TABLE "SESSION"
(
"SESSION_ID" character varying NOT NULL DEFAULT ''::character varying,
"SESSION" json NOT NULL DEFAULT '{}'::json,
"LAST_UPDATE" bigint DEFAULT 0,
CONSTRAINT "SESSION_PK" PRIMARY KEY ("SESSION_ID")
)
WITH (
OIDS=FALSE
);
I am trying to retrieve the returned result as following:
client.query(sql, function(err, result) {
done(); // Releasing connection to the pool
if ( err ) {
callback(err);
} else if ( result.rows.length > 0 ) {
var ttmp = result.rows[0];
var tmp1 = ttmp[0];
console.log("Res[0]: " + tmp1);
callback(err,result.rows[0]);
} else {
callback(err);
}
});
Although result.rows.length is > 0, result.rows[0][0] is undefined. How can I retrieve the field values from the returned row?
I have solved my issue by sending a SQL statement (SELECT * FROM "SESSION" WHERE "SESSION_ID" = '...';) rather than relying on a stored function.
I have also changed the name of the SESSION column to SESSION_JS, as giving the name of a table to a column too seems to be an issue, though no error message is displayed.