sql = '''INSERT INTO EmployeeData_markAttendance(Name,inTime,outtime,InDate)
VALUES(%s, %s, %s,%s) ON DUPLICATE KEYS UPDATE VALUES
outtime=%s'''
val=(Name,inTime,outtime,InDate,outtime)
cursor.execute(sql,val)
connection.commit()
SQLite does not support ON DUPLICATE KEY....
It supports UPSERT with the ON CONFLICT clause which works only if there is a unique constraint in the table, which I assume is defined for Name.
Also use ? placeholders instead of %s:
sql = """
INSERT INTO EmployeeData_markAttendance(Name, inTime, outtime, InDate) VALUES(?, ?, ?, ?)
ON CONFLICT(Name) DO UPDATE SET outtime = EXCLUDED.outtime
"""
val = (Name, inTime, outtime, InDate)
cursor.execute(sql, val)
connection.commit()
Related
(This relates to a Discord bot using Discord.js and sqlite v3, NOT sqlite3)
I'm currently trying to use WHERE NOT EXISTS to add a row to my Sqlite table, but only if there isn't a row where "serverid" is the ID of the current server and the type spam already.
I tried it with this:
sql.run(`INSERT INTO filters WHERE NOT EXISTS(SELECT 1 FROM filters WHERE serverid = "${msg.guild.id}" AND type = "Spam") (serverid, type, active, action, time) VALUES (?, ?, ?, ?, ?)`, msg.guild.id, `Spam`, 0, `delete`, 60)
This works if there ISN'T a row with that ID and type yet, but as soon as it does exist, I get a "syntax error at: WHERE".
It doesn't tell me which WHERE the problem is, and I double checked the syntax multiple times and it should be fine.
Does this not work in sqlite? Or did I get the syntax wrong?
The syntax of the query is wrong.
You should use INSERT ... SELECT instead of INSERT ... VALUES:
INSERT INTO filters (serverid, type, active, action, time)
SELECT ?, ?, ?, ?, ?
WHERE NOT EXISTS(SELECT 1 FROM filters WHERE serverid = "${msg.guild.id}" AND type = "Spam")`
I create multiple tables (at least two):
CREATE TABLE prices0
(
dt NOT NULL PRIMARY KEY,
buy REAL NOT NULL,
sell REAL NOT NULL
) WITHOUT ROWID;
CREATE TABLE prices1
(
dt NOT NULL PRIMARY KEY,
buy REAL NOT NULL,
sell REAL NOT NULL
) WITHOUT ROWID;
and insert statements:
INSERT INTO prices0 (dt, buy, sell) VALUES (?, ?, ?);
INSERT INTO prices0 (dt, buy, sell) VALUES (?, ?, ?);
In my C++ code I open and execute the statements as follows:
for (size_t i = 0; i < 2; ++i)
{
sqlite3_prepare(...);
}
for (size_t i = 0; i < 2; ++i)
{
sqlite3_bind_int64(...);
sqlite3_bind_double(...);
sqlite3_bind_double(...);
sqlite3_step(...);
sqlite3_reset(...);
}
The first call of sqlite3_step function fails with error code '1' and the message 'SQL logic error'.
The same code does not produce any errors with the single table.
it is because when I create the second table the database schema is changed, see
p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
in SQLite sources.
I'm getting an error from an sqlite3 query for which I can't find any reference material. Googling the string takes me deep in the SQLite code itself, and that's so opaque I can't make heads or tails of it.
The table schema:
CREATE TABLE quote (
seqnum INTEGER,
session STRING,
timestamp_sip INTEGER,
timestamp_1 INTEGER,
market_center STRING,
symbol STRING,
bid_price INTEGER,
bid_lots INTEGER,
offer_price INTEGER,
offer_lots INTEGER,
flags INTEGER,
PRIMARY KEY (symbol, seqnum) );
The query:
select (seqnum, session, timestamp_sip, timestamp_1, market_center, symbol)
from quote
where symbol = 'QQQ';
The error:
Error: row value misused
I have no idea how to proceed here. There is plenty of data in the table that would match the query:
sqlite> select count(*) from quote where symbol = 'QQQ';
2675931
Can anyone offer any guidance here? Sqlite version is 3.16.2.
Nevermind. Those parentheses around the select columns (left over from a copy/paste) are the problem. Poor error message, maybe. But my fault.
I had a similar when working with a Rails 5.2 Application.
For my case I was trying to write a search query for a model in application:
def self.search(params)
applications = all.order('created_at DESC') # for not existing params args
applications = applications.where("cast(id as text) like ?, email like ?, first_name like ?, last_name like ?, appref like ?", "#{params[:search]}", "%#{params[:search]}%", "#{params[:search]}", "#{params[:search]}", "#{params[:search]}",) if params[:search]
applications
end
The issue was that I was using a comma (,) to separate the search parameters, I simply corrected by using an OR instead:
def self.search(params)
applications = all.order('created_at DESC') # for not existing params args
applications = applications.where("cast(id as text) like ? OR email like ? OR first_name like ? OR last_name like ? OR appref like ?", "#{params[:search]}", "%#{params[:search]}%", "#{params[:search]}", "#{params[:search]}", "#{params[:search]}",) if params[:search]
applications
end
I deleted brackets from query and it work for me: from SELECT (column1, column2, ...) FROM table to SELECT column1, column2, ... FROM table
JUST
select seqnum, session, timestamp_sip, timestamp_1, market_center, symbol
from quote
where symbol = 'QQQ';
then it works.
Yeah, this bug is happing in my code, and I found this questions, but none of the answers helped me.
I fixed this problem with remove the same column in my SELECT command.
(It's stupid, because I can't select the column if the column is already in the condition subcommand.)
This is the problem SQL command (DO NOT USE THIS):
SELECT (`id`, `username`) FROM `users` WHERE `id` = 'someone_s id'
This is the fixed SQL command (PLEASE USE THIS):
SELECT (`username`) FROM `users` WHERE `id` = 'someone_s id'
The same error occurs when putting the elements of a GROUP BY clause inside brackets, as in
SELECT 1 as a, 2 as b FROM (SELECT 1) GROUP BY (a, b);
The correct syntax is, of course
SELECT 1 as a, 2 as b FROM (SELECT 1) GROUP BY a, b;
I know that cassandra does but what about Astyanax? And does it escape single quotes in values? Thx
Yes it does,
You can see an example here https://github.com/Netflix/astyanax/wiki/Cql-and-cql3
final String INSERT_STATEMENT = "INSERT INTO employees (empID, deptID, first_name, last_name) VALUES (?, ?, ?, ?);";
result = keyspace
.prepareQuery(CQL3_CF)
.withCql(INSERT_STATEMENT)
.asPreparedStatement()
.withIntegerValue(222)
.withIntegerValue(333)
.withStringValue("Eric")
.withStringValue("Cartman")
.execute();
When writing parameterized queries in Classic ASP, I have been using the following syntax:
Set cmdConn = Server.CreateObject("ADODB.Command")
Set cmdConn.ActiveConnection = Conn
cmdConn.Prepared = True
SQL = " INSERT INTO myTable (column1, column2, column3) VALUES (?, ?, ?); "
Set newParameter = cmdConn.CreateParameter("#column1", ad_Integer, ad_ParamInput, Len(input1), input1)
cmdConn.Parameters.Append newParameter
Set newParameter = cmdConn.CreateParameter("#column2", ad_Integer, ad_ParamInput, Len(input2), input2)
cmdConn.Parameters.Append newParameter
Set newParameter = cmdConn.CreateParameter("#column3", ad_Integer, ad_ParamInput, Len(input3), input3)
cmdConn.Parameters.Append newParameter
cmdConn.CommandText = SQL
cmdConn.Execute
And I'm of the understanding (self-taught), that where I use #column1 when appending a new parameter, this allocates the parameter to that particular column. I could be wrong tho'. So this has totally confused me when trying to write a full-text query.
This is my original full-text query:
SQL = "SELECT * FROM myTable WHERE MATCH (column1, column2, column3) AGAINST ('"&input&"' IN BOOLEAN MODE)"
Could somebody show me what syntax to use for this query?
I would imagine the SQL would look like this:
SQL = "SELECT * FROM myTable WHERE MATCH (column1, column2, column3) AGAINST (? IN BOOLEAN MODE)"
But not sure what to put for the newParameter string. Any help and explanations gratefully received...
"#columnN" is the name of that parameter, and isn't related to the column columnN. This field is optional, so it could be unspecified for all of your parameters if you are never going to use the name when referring to it.
It can be used for retrieving the value of output and input/output parameters from the Command object, instead of referring to the parameter by the order in which it was appended to the Parameters collection. I believe that some DBMSs will also support using named parameters in the query string instead of ? (easier to read, presumably).
To answer your specific question,
Set newParameter = cmdConn.CreateParameter(, adInteger, adParamInput, Len(input), input)
cmdConn.Parameters.Append newParameter