I'm trying to send null
to my MySQL database using Node.JS:
con.query("INSERT INTO Routes (routeTrigger) VALUES ( " + null + " )", {title: 'test'}, function(err, result) {
if (err) throw err;
});
But when looking in the database, the updated value reads 'null'
. The database's NULL
is supposed to read NULL
. If I send capital NULL
instead, that doesn't even work (in Node.JS). If I send 'NULL'
it sends the string "NULL", and not the database's NULL
.
JavaScript's null
is not the same as SQL's Null
. You want (in your case, using raw SQL strings) a string:
con.query("INSERT INTO Routes (routeTrigger) VALUES ( null )", {title: 'test'}, function(err, result) {
if (err) throw err;
});
But note that that creates a row that's entirely null
. Which is possible, but it's unlikely what you want.
What are you actually trying to do?
If you want to assign the NULL value In the SQL tables, you can think differently.
Instead of assigning NULL, you can tweak the field (column) so that the default value (when no value is assigned) is : NULL.
Now, when you insert your new row, just don't assign any value to that field.
Example :
Let's say the table is structured like so :
MyTable
id: auto increment; ...
name: text
url: text; default = NULL
Then you could do:
INSERT INTO Mytable ( name ) VALUES ( "Joe" );
As you omit the url information, it gets set to NULL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With