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)
);