Closed XHanL closed 2 months ago
The Rusqlite prepare() + execute() method need format like:
prepare()
execute()
INSERT INTO \"virtual_file\" (\"path\", \"name\") VALUES (?, ?)
However, if we use Query::insert() without values_panic(), the build string look like this:
Query::insert()
values_panic()
INSERT INTO \"virtual_file\" (\"path\", \"name\") /* NO VALUE PART */
If we execute this prepared stmt, the DB return error
SqliteFailure(Error { code: Unknown, extended_code: 1 }, Some("incomplete input"))
We have to add empty values manually like this, It's unfriendly and confusing():
.values_panic(["".into(), "".into(), ...])
#[derive(Iden)] pub enum VirtualFile { Table, Id, Path, Name, } fn test_insert() { let conn = Connection::open("../sqlite.db").unwrap(); let sql = [Table::create() .table(VirtualFile::Table) .if_not_exists() .col( ColumnDef::new(VirtualFile::Id) .integer() .primary_key() .auto_increment(), ) .col(ColumnDef::new(VirtualFile::Path).string().not_null()) .col(ColumnDef::new(VirtualFile::Name).string().not_null()) .build(SqliteQueryBuilder)]; conn.execute_batch(&sql.join("; ")).unwrap(); let (sql, _) = Query::insert() .into_table(VirtualFile::Table) .columns([VirtualFile::Path, VirtualFile::Name]) //.values_panic(["".into(), "".into()]) // need to add this manually for insert stmt prepare .build(SqliteQueryBuilder); println!(">>> {:?}", &sql); let mut stmt = conn.prepare(&sql).unwrap(); stmt.execute(("path", "name")).unwrap(); }
When we use Query::insert() without values_panic(), the builder will generate VALUES (?, ?, ...) based on columns,
VALUES (?, ?, ...)
When we use Query::insert() without values_panic(), the builder generate
INSERT INTO \"virtual_file\" (\"path\", \"name\")
Description
The Rusqlite
prepare()
+execute()
method need format like:However, if we use
Query::insert()
withoutvalues_panic()
, the build string look like this:If we execute this prepared stmt, the DB return error
We have to add empty values manually like this, It's unfriendly and confusing():
Steps to Reproduce
Expected Behavior
When we use
Query::insert()
withoutvalues_panic()
, the builder will generateVALUES (?, ?, ...)
based on columns,Actual Behavior
When we use
Query::insert()
withoutvalues_panic()
, the builder generateReproduces How Often
Versions