DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Insert new data fails

Open gnuhel opened this issue 11 years ago • 6 comments

How can I insert new data to a model?

gnuhel avatar Feb 09 '15 21:02 gnuhel

@gnuhel can you be more specific. I don't understand what you mean.

agrosner avatar Feb 09 '15 21:02 agrosner

@agrosner I create a model User

@Table(databaseName = PosDatabase.NAME)
public class User extends BaseModel {
    @Column(columnType = Column.PRIMARY_KEY)
    public int id;

    @Column
    public String name;
}

and want to insert new data to that model with

        new Insert<User>(User.class)
                .columns(User$Table.NAME)
                .values("Name")
                .query();

Is it the right code? It seems not to insert data into User. When I try to output the data

        List <User> users = new Select().from(User.class).queryList();

users is empty.

Can you help me? Thanks

gnuhel avatar Feb 09 '15 21:02 gnuhel

I would:


User user = new User();
user.name = "Name";

// determines if exists to update or insert
user.save(false);

// if you know it will not exist at all (or have an ```@Table(insertConflict())``` specified) you can run
user.insert(false);

there are many ways to interact with the DB in this library, which makes it flexible.

agrosner avatar Feb 09 '15 21:02 agrosner

@gnuhel actually it looks like you found a nice bug!! good find! its running the wrong method.

agrosner avatar Feb 09 '15 21:02 agrosner

This is still open, pending a 1.4.3 release.

agrosner avatar Feb 09 '15 21:02 agrosner

What should I do if I want to enter the multiple users at the single time....

KKS161994 avatar Aug 26 '15 23:08 KKS161994