I would like to update a text field in an sqlite database created using the peewee python library. Specifically I would like to use peewee's atomic updates something like:
query = Table.update(textfield = Table.textfield + 'string').where(some condition)
query.execute()
This type of update works fine for numeric fields but not for text fields. I'm guessing there may be a way to do this with the sqlite || operator, but as sql in general is somewhat new to me I am unable to figure it out.
You can use the concat operator:
query = Table.update(textfield=Table.textfield.concat('string')).where(whatever)
query.execute()
The concat operator will use || under-the-hood.
Related
I have a doubt about the structure of a SQLite query. I'm trying to update a user-selected value in the table referencing the row by the username.
The table is called Data and has these columns: USERNAME,PASSWORD,ADDRESS,NOTES.
I'm using SQL drivers for GO (_ "github.com/mattn/go-sqlite3"), here's my query:
...
stmt, err := db.Prepare("UPDATE Data SET ?=? WHERE USERNAME=?")
check(err)
res, err := stmt.Exec(splittedQuery[0], splittedQuery[1],splittedQuery[2])
...
From this sequence I can only get a syntax error: near "?": syntax error.
How should I manage this? If it's a trivial question I'm sorry, I'm just new to GO and trying to learn something out of it.
Thanks
You cannot do that in SQL. It's not specific to sqlite either. Parameterized placeholder are only for value, you cannot change the structure of the query with that. Here are some documentation links for your reference:
https://jmoiron.github.io/sqlx/#bindvars
https://use-the-index-luke.com/sql/where-clause/bind-parameters
What you are trying to do is building a dynamic query. You can do that by building your query string yourself:
query := "UPDATE Data SET " + col_name + "=? WHERE USERNAME=?"
But depending from the source of your data for the column_name you need to be cautious of sql injection (this is a whole other topic, for fun you can look at that https://imgs.xkcd.com/comics/exploits_of_a_mom.png).
There are also a few library available to help you with that. For example to name one, you can check this one https://github.com/Masterminds/squirrel
I am trying to create a query in python3 using sqlobject mapper. I have a requirement of filtering a custom field based on the output of bitise operation.
Query looks like this:
query = sqlobject.AND(query,dl.<table_object>.q.anomalyType & alert_type == alert_type)
But when the actual SQL transformation happens, this bitwise & gets converted into "SQL AND", which changes the whole meaning of the query.
Can someone advise the right way to add bitwise operator in the query sqlobject in python3?
SQLConstant does the trick. Working sql query looks like this:
query = sqlobject.AND(query, sqlbuilder.SQLConstant(f'anomalyType & {alert_type}') == alert_type)
Thanks for the help #phd.
I need to create trigger dynamically and don't need drop them in future.
So, I need a code to do this.
Likely
CREATE TRIGGER random() BEFORE INSERT... (with random name)
Or
CREATE TRIGGER BEFORE INSERT... (without name)
Can I do this in sqlite shell?
I know, it's bad practise, but it's experiment.
Thanks.
I'm afraid that's not possible. According to the documentation, trigger-name is an atomic syntactical unit (it can be seen from it from being lowercase, in a rounded-cornered rectangle), in the sense it cannot be constructed by evaluating complex expressions. You are only allowed to enter a literal there (same with table-name and index-name by the way, see here and here).
What you can do instead is dynamically constructing the whole query string, before passing it to SQLite. E.g. if you are interacting with SQLite through Python, then you can write something like:
tableName = "someRandomString"
db.execute("CREATE TABLE " + tableName + " (A INT, B TEXT)")
Or if you are using the Windows command prompt:
set tableName=someRandomString
sqlite3.exe test.sqlite "CREATE TABLE %tableName% (A INT, B TEXT)"
I know you were asking about triggers, not tables but it's basically the same thing from your question's perspective, and the table creation syntax is shorter.
For example if in the sqlite database have entries Resume, Résumé ,RESUME and if we enter
select Name from NAME_TABLE where Name like 'Re%%'
It should return all
But like does not support collation , any-other way to do pattern matching using collation ?
In SQLite, LIKE always uses the NOCASE or BINARY collations.
You could redefine LIKE by install a user-defined like() function, but you could just as well use your own function with a different name.
Is it possible to find index of an character in SQLite without using extension functions?
I need to substring texts like below from the beginning until ( character in a SELECT statement.
TT 15 (Something...)
TT 5 (blabla...)
I cannot use instr in our version of SQLite (i think it is 3.6) and it's not possible to update SQLite either.
If it were possible to do something like instr() without actually calling instr(), the authors of SQLite would not have felt the need to add instr().
If you cannot update SQLite or create an extension function, you have to read the entire string from the database and search it in your own code.