GithubHelp home page GithubHelp logo

Insert null values about rust-oracle HOT 3 CLOSED

franksl avatar franksl commented on July 22, 2024
Insert null values

from rust-oracle.

Comments (3)

kubo avatar kubo commented on July 22, 2024

Use Option<ToSql>.

let mut stmt = conn
    .statement("insert into employees values (:name, :salary)")
    .build()?;

stmt.execute_named(&[("name", &"Allen"), ("salary", &None::<i32>)])?;

let employees: [(&str, Option<i32>); 3] = [
    ("Jones", Some(7566)),
    ("Martin", Some(7654)),
    ("Blake", None),
];
for emp in &employees {
    stmt.execute_named(&[("name", &emp.0), ("salary", &emp.1)])?;
}

from rust-oracle.

franksl avatar franksl commented on July 22, 2024

Ok, thanks, should the type always match the column type when setting null values?
Suppose I have a table like this:
CREATE TABLE test (id INT PRIMARY KEY, ival INT, sval VARCHAR(100));

If I need to set ival and sval as null do I need to set the Option type as i32 and str like this?

let mut stmt = conn
    .statement("insert into test (id, ival, sval) values (:id, :ival, :sval)")
    .build()?;

stmt.execute_named(&[("id", &1), ("ival", &None::<i32>), ("sval", &None::<str>)])?;

Are columns types checked?
Thanks,
Frank

from rust-oracle.

kubo avatar kubo commented on July 22, 2024

Ok, thanks, should the type always match the column type when setting null values?

Not always. For column types (number, date, timestamp, interval, ...) supporting implicit type conversion from string, &None::<&str> is available. For other column types (object, collection), the type must match the column type.

from rust-oracle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.