does Astyanax support cql 3 prepared statements? - astyanax

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();

Related

django.db.utils.OperationalError: near "DUPLICATE": syntax error

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()

Using WHERE NOT EXISTS in Sqlite returns syntax error

(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")`

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 5 supplied [duplicate]

This question already has answers here:
Programming Error: Incorrect number of bindings supplied
(2 answers)
Closed 6 years ago.
I really do not understand what am I doing wrong. My table is:
conn.execute('''CREATE TABLE USERS
(EMAIL TEXT NOT NULL,
PASSWORD TEXT NOT NULL,
FIRST_NAME TEXT NOT NULL,
LAST_NAME TEXT NOT NULL,
DATE TEXT NOT NULL,
SEX TEXT NOT NULL);''')
and when I am trying to insert to the table:
conn.executemany("INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?)", [email, password, first_name, last_name, date, sex])
I get this error:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 5 supplied.
if I add a question the error is the same but now:
sqlite3.OperationalError: table USERS has 6 columns but 7 values were supplied
if I add only a comma without a question mark the error is different:
sqlite3.OperationalError: near ")": syntax error
the variables value are:
email=str(myapp.register_email.text())
print email
first_name=str(myapp.first_name.text())
print first_name
last_name=str(myapp.last_name.text())
print last_name
date=str(myapp.date.text())
print date
password="nht"
print password
sex=str(myapp.sex.currentText())
print sex
I am really getting insane here cant figure out the problem
As suggested by the Python doc, the executemany() method requires a tuple of values. Therefore I suggest you to insert the new entry by using the following code:
conn.executemany("INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?)", [(email, password, first_name, last_name, date, sex)]

Classic ASP / Parameterized Full Text Query

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

ASP.NET and SQL - inserting data into more than one table?

So I know similar questions have been asked before, but I couldn't find a clear answer for my specific situation. I'm using ASP.NET (in Visual Web Developer) and I need to insert data from one form into two separate tables. This is my data source:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/courseinfo.mdb"
SelectCommand="SELECT * FROM [tableCourse], [tableFaculty]"
InsertCommand="INSERT INTO [tableCourse]
([department], [name_first], [name_last], [prefix],
[course_number], [credits], [title], [description])
VALUES (?, ?, ?, ?, ?, ?, ?, ?); INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email])
VALUES (?, ?, ?, ?)">
So you see I've tried using two insert statements, and it just comes back with an error saying there are extra characters after the SQL statement. I've tried taking out the semi-colon and then it says I'm missing a semi-colon. Is it possible to insert to two tables at once using this control? And if not, how do I work around this?
UPDATE:
Okay, tried it in the codebehind, but I don't think I did it right, now it's giving me this error:
Server Error in '/CCC' Application.
Index or primary key cannot contain a Null value.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Index or primary key cannot contain a Null value.
Source Error:
Line 87:
Line 88: AccessDataSource1.InsertCommand = "INSERT INTO [tableCourse] ([department], [name_first], [name_last], [prefix], [course_number], [credits], [title], [description]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
Line 89: AccessDataSource1.Insert()
Line 90:
Line 91: AccessDataSource1.InsertCommand = "INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email]) VALUES (?, ?, ?, ?)"
Line 89 is the one that's highlighted. So I'm thinking it's attempting to insert but for some reason the values are null, it isn't taking the values from the text boxes.
I probably left something obvious out, I don't know, I'm really new at this.
Why don't you give it a try at the codebehind?
AccessDataSource1.InsertCommand = "First INSERT Statement";
AccessDataSource1.Insert();
AccessDataSource1.InsertCommand = "Second INSERT Statement";
AccessDataSource1.Insert();

Resources