I have a problem when I try to insert into my sqlite db. I have two tables looking like this :
CREATE TABLE user(UId INTEGER PRIMARY KEY ASC AUTOINCREMENT ,name VARCHAR(100) UNIQUE,phoneNumber INTEGER UNIQUE);
and
CREATE TABLE 'transaction'(TId INTEGER PRIMARY KEY ASC AUTOINCREMENT , loan REAL, trackUser INTEGER,FOREIGN KEY(trackUser) REFERENCES user(UId));
I can insert into the user table without any problems like this:
INSERT INTO user (name, phoneNumber) VALUES(@name,@phoneNumber)
But when I try to add a transaction my database gets locked. I do that like this:
public void Add(Transaction trans,User user)
{
_conn.Open();
string SQL = "INSERT INTO 'transaction' (loan, trackUser) VALUES(@loan,@userID);";
string checkUserSQL = "SELECT EXISTS(SELECT 1 FROM user WHERE UId = @UId) LIMIT 1;";
SQLiteCommand checkUserCommand = new SQLiteCommand(checkUserSQL,_conn);
checkUserCommand.Parameters.Add(new SQLiteParameter("@UId", user.UId));
var reader = checkUserCommand.ExecuteReader();
reader.Read();
var check = reader.GetBoolean(0);
if (!check)
{
throw new UserDoesNotExcistException();
}
SQLiteCommand transCommand = new SQLiteCommand(SQL, _conn);
transCommand.Parameters.Add(new SQLiteParameter("@loan",trans.Loan));
transCommand.Parameters.Add(new SQLiteParameter("@userID",user.UId));
transCommand.ExecuteNonQuery();
_conn.Close();
}
the select statement that checks if the user exicst runs without a problem but when it comes to transCommand.ExecuteNonQuery(); the db gets locked. Am I binding the forign key in a wrong way or why is my db getting locked?
Aucun commentaire:
Enregistrer un commentaire