0

I'm trying to create a table using the "sqlite-net-extensions". But the problem is always returning with error.

The error that returns is "Read only".

I created two models to create the tables.

Model --> Login

[PrimaryKey, AutoIncrement, Column("login_id")]
    [Indexed(Name = "LoginId", Order = 2, Unique = true)]
    public int Id { get; set; }

    [NotNull, Column("login_user")]
    [Indexed(Name = "LoginId", Order = 1, Unique = true)]
    public string UserName { get; set; }

    [NotNull, Column("login_password")] public string Password { get; set; }

    [OneToOne("login_id", "Login")]
    public User User { get; set; }

Model --> User

[PrimaryKey, AutoIncrement, Column("user_id")]
    public int Id { get; set; }

    [NotNull, Column("user_name")]
    [Indexed(Name = "UserId", Order = 3, Unique = true)]
    public string Name { get; set; }

    [NotNull, Column("user_email")]
    [Indexed(Name = "UserId", Order = 2, Unique = true)]
    public string Email { get; set; }

    [ForeignKey(typeof(Login)), NotNull, Column("login_id")]
    [Indexed(Name = "UserId", Order = 1, Unique = true)]
    public int LoginId { get; set; }

    [OneToOne("login_id", "User", CascadeOperations = CascadeOperation.All, ReadOnly = false)]
    public Login Login { get; set; }

Insert method

public async Task InsertWithChildren(object o)
    {
        var connection = _sqliteWrapper.OpenDatabase();
        await connection.InsertWithChildrenAsync(o, recursive: true);
    }
  • 2
    Where is your database located? If it is included in your app bundle then it will not be writable. You will need to copy it to a user folder first. – Jason Sep 26 '18 at 01:12
  • I agree with Jason's assumption. If the db was deployed with the app, it will be in a read only location. take a look at [Deploying a database file with Xamarin.Forms app](https://robgibbens.com/deploying-a-database-file-with-a-xamarin-forms-app/) – Neil Sep 26 '18 at 14:51

1 Answers1

0

Follow the next steps

1.- Create a SQLite connection.

2.- Create your tables

3.- Insert into database

 var connection = new SQLiteAsyncConnection(YourDBPath);
 connection.CreateTableAsync<Login>().Wait();
 connection.CreateTableAsync<User>().Wait();
 connection.InsertWithChildrenAsync(YourItem);
Ricardo Romo
  • 1,588
  • 12
  • 25