Can a SQLite user-defined function take a row argument?
They are described as scalar, but I think that refers to the return type rather than the arguments.
I'm trying to define one in rust that will provide a TEXT
value derived from other columns in the row, for convenience/readability at point of use, I'd like to call it as select myfunc(mytable) from mytable
rather than explicitly the columns that it derives.
The rusqlite
example simply gets an argument as f64
, so it's not that clear to me how it might be possible to interpret it as a row and retrieve columnar values from within it. Nor have I been able to find examples in other languages.
Is this possible?
1 answer
-
answered 2021-02-27 12:22
OJFord
This doesn't seem possible.
func(tablename)
syntax that I'm familiar with seems to be PostgreSQL-specific; SQLite supportsfunc(*)
but whenfunc
is user-defined it receives zero arguments, not one (structured) orN
(all columns separately) as I expected.
See also questions close to this topic
-
I have problem with significant sqlite3 syntax
This is my function
def separator(data,id): first_part = data.split(':')[0] second_part = data.split(':')[1] conn = sqlite3.connect('db.db') q = conn.cursor() q.execute(f"SELECT {second_part} FROM users WHERE id IS "+str(id)) q_new = [*q][0][0] x = ast.literal_eval(q_new) x.append(first_part) print(second_part) print(id) print(type(str(x))) q.execute("UPDATE users SET %s = '%s' WHERE id = '%s'"%(second_part,x,id)) conn.close()
And the output is like this:
q.execute("UPDATE users SET %s = '%s' WHERE id = '%s'"%(second_part,x,id)) sqlite3.OperationalError: near "1": syntax error bang_switch_duo_2000 1746292802 <class 'str'>
What should I do? I tried a lot of things such as concatenating, f-strings and etc.
-
Updating table SQlite
def update_employee_table(self,ids,username, password, first_name, last_name,age, phone_number, department, city, address): self.connect() print(self) self.c.execute("UPDATE employee SET (username,password,first_name,last_name,age,phone_number,department,city,address) VALUES (?,?,?,?,?,?,?,?,?) WHERE id = ?",(username, password, first_name, last_name,age, phone_number, department, city, address),(ids)) self.commit()
i am trying to update a table by the id and insert the values that i have passed to the function , the syntax of the update confuses me a little and i get this error : TypeError: function takes at most 2 arguments (3 given)
-
Is it okay to use SQLite to store translations instead of XML resources like strings.xml
I wanted to find a way to give a user to chose language of app instead of changing it from device settings. App is meant for people who use rare language, that isn't supported on most of devices. I was searching for answers of "How to change app language programmatically?", but my head ached. So i just want to use SQLite not only for storing all app datas, but also to store translations of captions, labels, etc that are usually stored in "strings.xml". So is it okay? Is anyone gonna think that it's a bad practice?
-
How to read from Azure Blob Storage with Python delta-rs
I'd like to use the Python bindings to the delta-rs to read from my blob storage. https://github.com/delta-io/delta-rs/tree/main/python
Currently I am kind of lost, since I cannot figure out how to configure the filesystem on my local machine. Where do i have to put my credentials?
Can I use adlfs for this?
from adlfs import AzureBlobFileSystem fs = AzureBlobFileSystem( account_name="...", account_key='...' )
and then use the fs object?
Would be really helpful if someone could provide me with some headstart here.
Best
-
I got a "panicked at 'attempt to subtract with overflow'" while trying to loop over a vector
With this function, I am trying to detect whether there are wrong bid or ask messages (based on their ascending/descending order). Here is the hierarchy of the variables: -Vec_record: a vector of type where Record is :
pub struct Record { pub glid: String, pub instrument_id: String, pub asks: Vec<Limit>, pub bids: Vec<Limit>, }
asks and bids are vectors of type where is :
pub struct Limit { pub quantity: u32, pub implied_quantity: u32, pub number_of_orders: u32, pub price: f32, }
the detection of wrong bids or asks is based on the ascending/descending order.
I get a "panicked at 'attempt to subtract with overflow'" error in for z in 0..(record.asks.len()-1) and for j in 0..(record.bids.len()-1).
pub fn detect_errors_from_limits (vec_record : &Vec<structs::Record>) { for (i, record) in vec_record.iter().enumerate() { for z in 0..(record.asks.len()-1) { if record.asks.len() > 0 { if record.asks[z].price > record.asks[z+1].price{ println!("Ask number {} is wrong in record {}", z, i); } } } for j in 0..(record.bids.len()-1) { if record.bids.len() > 0 { if record.bids[j].price < record.bids[j+1].price{ println!("Bid number {} is wrong in record {}", j, i); } } } } }
-
Extracting LLVM bitcode embedded using `-lto-embed-bitcode`
Goal: Extract full-program (merged) post-LTO bitcode from an ELF binary.
The program happens to be written in Rust, but I don't think that's a crucial detail.
I'm able to compile a Rust program into an ELF binary with a
.llvmbc
section using the following Cargo invocation:RUSTFLAGS="-C linker_plugin_lto -C linker=clang -C link-arg=-fuse-ld=lld -C link-arg=-Wl,--plugin-opt=-lto-embed-bitcode=optimized" \ cargo build --release
I'm then able to use
readelf -S | grep llvmbc
to verify that the section exists. It does. Superb!I'd now like to extract the full-program post-LTO bitcode and disassemble it:
$ objcopy target/release/world --dump-section .llvmbc=llvm.bc $ llvm-dis llvm.bc LLVM ERROR: Invalid encoding PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace. Stack dump: 0. Program arguments: llvm-dis llvm.bc #0 0x000055c06f7ae78c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x1b578c) #1 0x000055c06f7ac6e4 llvm::sys::RunSignalHandlers() (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x1b36e4) #2 0x000055c06f7ac843 SignalHandler(int) (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x1b3843) #3 0x00007f6c62eaf730 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12730) #4 0x00007f6c627957bb raise /build/glibc-vjB4T1/glibc-2.28/signal/../sysdeps/unix/sysv/linux/raise.c:51:1 #5 0x00007f6c62780535 abort /build/glibc-vjB4T1/glibc-2.28/stdlib/abort.c:81:7 #6 0x000055c06f783753 llvm::report_fatal_error(llvm::Twine const&, bool) (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x18a753) #7 0x000055c06f783868 (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x18a868) #8 0x000055c06f7bb703 llvm::BitstreamCursor::ReadAbbrevRecord() (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x1c2703) #9 0x000055c06f64a49d llvm::BitstreamCursor::advance(unsigned int) (.constprop.1679) (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x5149d) #10 0x000055c06f658abd llvm::getBitcodeFileContents(llvm::MemoryBufferRef) (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x5fabd) #11 0x000055c06f63e159 main (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x45159) #12 0x00007f6c6278209b __libc_start_main /build/glibc-vjB4T1/glibc-2.28/csu/../csu/libc-start.c:342:3 #13 0x000055c06f6436ea _start (/home/vext01/research/yorick/llvm-project/inst/bin/llvm-dis+0x4a6ea) Aborted
If I search the binary for the LLVM header's magic bytes,
0x4243c0de
, there are multiple hits. Furthermore, if I tellrustc
to use a single codegen unit (-C codegen-units=1
), then there are then fewer hits for the magic bytes (exactly two).What I think is happening is the linker is concatenating the
.llvmbc
sections of the intermediate objects with the post-LTO bitcode, and this confusesllvm-dis
.Assuming that's the case, how can I unambiguously extract only the post-LTO bitcode? I don't feel comfortable with trying to separate the different modules based on the magic bytes. This seems error prone, as that byte sequence could appear elsewhere by coincidence (i.e. not marking the start of a bitcode object at all).
Is there perhaps a way to make libLTO put the post-LTO bitcode into a dedicated section of a different name? Having read the source code, I don't think it's possible without modifications.
Thanks
EDIT
Repeating the experiment using clang instead of rustc actually seems to work, so I'm starting to wonder if this is in fact a rust bug. Perhaps rustc is passing the old pre-merged bitcode through when it shouldn't?
$ clang -fuse-ld=lld -flto -Wl,--plugin-opt=-lto-embed-bitcode=optimized world.c $ objcopy a.out --dump-section .llvmbc=llvm.bc $ llvm-dis llvm.bc $ head -5 llvm.ll ; ModuleID = 'llvm.bc' source_filename = "ld-temp.o" target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"
-
How do we use SELECT query with an external WHERE parameter in rusqlite?
I need to fetch a row from a database using SELECT and WHERE, I need to fetch a row depending on the age, I tried this way using some other tutorial.
use rusqlite::{params, Connection, Result}; #[derive(Debug)] struct Person { id: i32, name: String, age: u8, data: String, } fn main() -> Result<()> { let conn = Connection::open_in_memory()?; conn.execute( "CREATE TABLE person ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER, data TEXT )", [], )?; let me = Person { id: 0, name: "Steven".to_string(), age:1, data: "data1".to_string(), }; conn.execute( "INSERT INTO person (name, age, data) VALUES (?1, ?2, ?3)", params![me.name, me.age, me.data], )?; conn.execute( "INSERT INTO person (name, age, data) VALUES (?1, ?2, ?3)", params!["john".to_string(), 2, "data2".to_string()], )?; let age:u8 = 1; let mut stmt = conn.prepare("SELECT id, name, age, data FROM person WHERE age=:age;")?; let person_iter = stmt.query_map([], |row| { Ok(Person { id: row.get(0)?, name: row.get(1)?, age: row.get(2)?, data: row.get(3)?, }) })?; for person in person_iter { println!("Found person {:?}", person); } Ok(()) }
How do we use SELECT query with an external WHERE parameter?
-
How to insert and fetch date in a sqlite database using rusqlite?
I have a struct with NaiveDate field and I want to insert this field to a table. This is my whole code
use chrono::naive::NaiveDate; use rusqlite::{params, Connection, Result}; #[derive(Debug)] struct Person { id: i32, name: String, date: NaiveDate, } fn main()->Result<()>{ let date_str = "2020-04-12"; let naive_date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap(); let me = Person { id: 0, name: "Steven".to_string(), date: naive_date, }; println!("{:?}",me); let conn = Connection::open_in_memory()?; conn.execute( "CREATE TABLE person ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, date TEXT )", [], )?; conn.execute( "INSERT INTO person (name, date) VALUES (?1, ?2)", params![me.name, me.date], )?; let mut stmt = conn.prepare("SELECT id, name, date FROM person")?; let person_iter = stmt.query_map([], |row| { Ok(Person { id: row.get(0)?, name: row.get(1)?, date: row.get(2)?, }) })?; for person in person_iter { println!("Found person {:?}", person.unwrap()); } Ok(()) }
But it gives these two errors
| the trait `FromSql` is not implemented for `NaiveDate` | the trait `ToSql` is not implemented for `NaiveDate`
cargo.toml
chrono = "0.4.19" rusqlite = "0.25.0"