3

I'm importing an Android code to windows rt and I need the data to be compatible on both apps. When creating my sqlite database I need to set a default value to a column, doing something like

CREATE TABLE [blabla] ( [id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [bleh] INTEGER NOT NULL DEFAULT -1 )

I wrote my C# code, using SQLite-net this way:

[Table("blabla")]
public class Blabla {
    [PrimaryKey, AutoIncrement, NotNull]
    public int id { get; set; }

    [NotNull]
    public int bleh { get; set; }
}

But I can't set the default value to 'bleh'. I need both database to be the same =/ Can anyone help me with this?

Thanks =)

3 Answers 3

3

Why don't you use:

    private int _bleh = 1;
    [NotNull]
    public int Bleh
    {
        get { return _bleh; }
        set { _bleh = value; }
    }

Then bleh will always have a default of 1 unless changed

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately the database generated by the windows app still incompatible with my android app that assumes that this column has a default value. Changing my android code is not viable, since I cant be sure that every user who has my app installed will update it.
Can't add a field to an existing table - no default value specified
2

As long as didn't get an answer, I just created the whole database using the SQL statements, so the database won't need to be created by my C# code. It's working ok!

Comments

1

I know it's late but maybe this will help someone like me. If Default is supported by your SQLite, then use Default attribute

[Table("blabla")]
public class Blabla {
    [PrimaryKey, AutoIncrement, NotNull]
    public int id { get; set; }

    [NotNull,Default(value:1)]
    public int bleh { get; set; }
}

If Default is not supported by your SQLite,you can simply assign default value like this:

 [Table("blabla")]
    public class Blabla {
        [PrimaryKey, AutoIncrement, NotNull]
        public int id { get; set; }

        [NotNull]
        public int bleh { get; set; } = 1;
    }

I prefer second way always.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.