简体   繁体   中英

How do I append a new id to record

I have a problem, basically I want the id in database to autoincrement and add more users, however when i try to add a 2nd record, it drops an error such as:

constraint failed
Unique constraint failed: DANE.id

this is my inserting method:

 private void LoadToDb_Click(object sender, EventArgs e)
        {
            var modelData = new Data();
            var db = new SqlDb();
            var sqLiteConnection = db.Connect();
            var sqLiteCommand = sqLiteConnection.CreateCommand();

            try
            {
                modelData.Name = firstName.Text;
                modelData.Age = Convert.ToInt32(DisplayAge.Text);
                modelData.LastName = LastName.Text;

                sqLiteCommand.CommandText =
                    $"insert into DANE values('{modelData.Name}', '{modelData.LastName}', '{modelData.Age}', {Interlocked.Increment(ref modelData.Id)})";
                sqLiteCommand.ExecuteNonQuery();

                db.Disconnect();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

This is my Data Model

    public class Data
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public int id = 0;
    }

and this is sqLite database pattern (I use db browser for that)

CREATE TABLE "DANE" (
    "FirstName" TEXT NOT NULL,
    "LastName"  TEXT NOT NULL,
    "Age"   INTEGER NOT NULL,
    "id"    INTEGER NOT NULL,
    PRIMARY KEY("id" AUTOINCREMENT)
);

You are using Data.Id as primary key, but you create a new instance every time. So your primary key will always be 1 (0 as soon as you create the new instance, then 1 when you call Interlocked.Increment on it, which btw is probably not needed).

Also you are generating the primary key both in your code and in the database. Choose the one you prefer, no need to do it twince.

your table defined with "id" INTEGER NOT NULL, PRIMARY KEY("id" AUTOINCREMENT)

So, your insert needs to be like

$"insert into DANE (FirstName, LastName, Age) 
  values('{modelData.Name}', '{modelData.LastName}', '{modelData.Age}')";

Basically, you don't list auto increment column in INSERT . But you need to retrieve inserted record value based on SqlLite-specific DB syntax and provider capabilities. Each DB engine and its .NET provider has its own way of doing it.

This should help you

TIP: SQLiteConnection.LastInsertRowId

DEFINE TABLE LIKE THIS:

CREATE TABLE "DANE" (`enter code here`
     "id"    INTEGER NOT NULL AUTO_INCREMENT,
    "FirstName" TEXT NOT NULL,
    "LastName"  TEXT NOT NULL,
    "Age"   INTEGER NOT NULL,
);

OR

var NewID =  DB.DANE.Max(x => x.id) + 1;

and than add NewID as id in your insert query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM