Why am I getting "SQLITE_ERROR: incomplete input" in this code? - node.js

I'm attempting to run some NodeJS code where I have the following:
db.run(insertQuery, newValuesArray, function(err) {
if (err) {
throw new Error(err);
}
})
When I run this code I get the following error: "SQLITE_ERROR: incomplete input". Checking the debugger I see the following:
The variable db is a valid SQLITE3 Database object
The currently empty test db has 3 columns: id (int), booya (text), and gta (text)
The value of insert query is "INSERT INTO test (id, booya, gta) VALUES (?, ?, ?)"
The values of newValuesArray is [0, 'cyborg', 'la']
I have been staring at this and fiddling with it for quite awhile now. What is wrong with this code?

Related

retrieve value from a select query

I am trying to retrieve a value from a mysql table to use it in another query but I keep getting an error because on the second query the value is not passed.
var id_customer='';
con1.query("SELECT id FROM utenze WHERE email='"+data.object.customer_email+"'", function (err, result, fields) {
if(err) throw err;
id_customer= result[0].id;
console.log(id_customer); //here id_customer has the correct value
});
console.log(id_customer); //here id_customer is empty
con2.query("SELECT ruolo FROM ruolo WHERE id_user="+id_customer, function (err, result, fields) {
if (err) throw err;
The second query will fail because id_customer remains empty.
data.object.email is correctly populated and the first query runs fine and returns a value.
What am i doing wrong? Is it related to how I retrieve the value from the first query?
EDIT: by further debugging tests result[0].id is returning the correct value
The first query will only return the id of utenze because you wrote select id from utenze , and not select * . so maybe replace id_customer= result[0].id by id_customer= result[0]

Sqlite bind or column index out of range but i've got the right number of parameters

My code is this:
db.run(`begin transaction;
replace into players (user_id, role) values ($id, $role);
update signed_up_users set finalised = 1 where user_id = $id;
commit;`, { $id: id, $role: role}, err=>{
if (err) {throw err;}
});
And I get the following error:
Error: SQLITE_RANGE: bind or column index out of range
--> in Database#run('begin transaction;\n replace into players (user_id, role) values (?, ?);\n update signed_up_users set finalised = 1 where user_id = ?;\n commit;', [ '123', 'INNOCENT', '123' ], [Function])
at Object.exports.finalise_user (C:\whatever\file.js)
at user.resolve_to_id.then.id (C:\whatever\file.js:52:14)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
If I understand the documentation correctly, I have the right amount of parameters and placeholders in my statement. So what am I doing wrong? Thanks.
The documentation actually says:
Database#run(sql, [param, ...], [callback])
Runs the SQL query […]
Database#exec(sql, [callback])
Runs all SQL queries in the supplied string.

PostgreSQL ERROR: relation does not exist on INSERT Statement

I am trying to have my code INSERT a row into my table called thoughtentries. It is in the public schema. I am able to run ths command while connected to my database using psql:
INSERT INTO thoughtentries VALUES('12/17/2016 14:10', 'hi');
The first column is of character type with length 17. The second column is of type text.
When I have my code attempt to INSERT using the same command above I get the error in my log:
ERROR: relation "thoughtentries" does not exist at character 13
STATEMENT: INSERT INTO thoughtentries VALUES('12/17/2016 14:11', 'hi');
I am using pg and pg-format to format the command. Here is my code to do this:
client.connect(function (err) {
if (err) throw err
app.listen(3000, function () {
console.log('listening on 3000')
})
var textToDB = format('INSERT INTO thoughtentries VALUES(%s, %s);', timestamp, "'hi'")
client.query(textToDB, function (err, result) {
if (err) {
console.log(err)
}
console.log(result)
client.end(function (err) {
if (err) throw err
})
})
})
How do I go about fixing this?
Have you verified that the table was, in fact, created in the public schema?
SELECT *
FROM information_schema.tables
WHERE table_name = 'thoughtentries';
Once you have verified that, I see two possible explanations remaining:
You are connecting to a different database by mistake. Verify, in the same session, with:
select current_database();
Your search_path setting does not include the public schema. If that's the case, you can schema-qualify the table to fix: public.thoughtentries
How does the search_path influence identifier resolution and the "current schema"
Aside: Save timestamps as data type timestamp, not character(17).
Actually, don't use character(n) at all:
Any downsides of using data type "text" for storing strings?

Inserting timestamp into Cassandra

I have a table created as follows:
CREATE TABLE my_table (
date text,
id text,
time timestamp,
value text,
PRIMARY KEY (id));
CREATE INDEX recordings_date_ci ON recordings (date);
I'm able to simply add a new row to the table using the following Node code:
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['localhost'], keyspace: 'my_keyspace'});
const query = 'INSERT INTO my_table (date, id, time, url) VALUES (?, ?, ?, ?)';
client.execute(query, ['20160901', '0000000000', '2016-09-01 00:00:00+0000', 'random url'], function(err, result) {
if (err){
console.log(err);
}
console.log('Insert row ended:' + result);
});
However, I get the following error:
'Error: Expected 8 or 0 byte long for date (24)
When I change the timestamp to epoc time:
client.execute(query, ['20160901', '0000000000', 1472688000, 'random url']
I get:
d
OverflowError: normalized days too large to fit in a C int
I'm able to insert new rows via cqlsh so I'm probably missing something with the node.js driver.
Any idea?
Thanks
Where you have a string 2016-09-01 00:00:00+0000, instead use new Date('2016-09-01 00:00:00+0000').

Node.js and sqlite, SQLITE_RANGE: bind or column index out of range

See my answer below for a MWE!
I know it sound stupid and the answer is probably right in front of me, but I can't figure out why I get this SQLITE_RANGE error, since my object looks like it has every properties needed.
console.log "values " , values
# Recording in db
console.assert values.login?
console.assert values.password_sha?
console.assert values.email?
console.assert values.token?
values.password = null
#db.run "INSERT INTO user VALUES (NULL, $login, $password_sha, $email, $token)", values, (err) ->
console.error err if err?
Here is the output of my server
values { login: 'ostream',
email: 'ijw',
password: 'justine',
token: 'acppn99ngiafw29',
password_sha: 'b1820c2ec34175954bdaa42038b48886b4c83f8d53f88b5315c415898855e4f8' }
{ [Error: SQLITE_RANGE: bind or column index out of range] errno: 25, code: 'SQLITE_RANGE' }
Thanks in advance!
Apparently what you need to do is make sure the keys in your user object are prefixed with $. Otherwise sqlite3 won't recognise them as values for placeholders. Try:
sqlite3 = require 'sqlite3'
db = new sqlite3.Database('testfile.db');
user =
$name: 'hello'
$password: 'jambon'
db.run 'insert into "user" (name, password) VALUES ($name, $password)', user, (err) ->
console.log 'request executed : ', err
It's not very well documented, but looking at the test cases you can see that there are other options, like prefixing them with # or :. In any case, your object has to match the placeholders including the prefix.
You should always specify column list when you INSERT:
INSERT INTO "user"(login, password_sha, email, token)
VALUES ('ostream',
'b1820c2ec34175954bdaa42038b48886b4c83f8d53f88b5315c415898855e4f8',
'ijw', 'acppn99ngiafw29');
SELECT *
FROM "user"
SqlFiddleDemo
Keep in mind that user is keyword and should be quoted with " or you can rename table to users.
Make sure the number of parameters matches.
For all SQL commands make sure the name of parameters matches.
Notice that $myValues != $myValue in my example:
let sqlRequest = "INSERT into myTable (myValue) VALUES ($myValue)";
let sqlParams = { $myValues: "Wrong parameter name" };
this.common.run(sqlRequest, sqlParams);

Resources