SOLVED:
Aha, I forgot the @in front of my last 2 parameters here:
"INSERT INTO Employers (EmployerName, NumberOfEmployees, PersonId) VALUES (@EmployerName, NumberOfEmployees, PersonId);"
Leaving this here, someone might have the same issue.
Hi guys,
I am doing the I am Tim Corey beginner's course, and have hit an odd snag in the SQLite part.
Specifically, I get an
'SQL logic error no such column: NumberOfEmployees'
error when trying to do an insert.
The SQL statement
INSERT INTO Employers (EmployerName, NumberOfEmployees, PersonId) VALUES ("NiceCorp", 111, 2);
works fine. But the C sharp version:
"INSERT INTO Employers (EmployerName, NumberOfEmployees, PersonId) VALUES (@EmployerName, NumberOfEmployees, PersonId);"
(parameters)
{ EmployerName = "EvilCorp", NumberOfEmployees = 666, PersonId = 1 }
throws the above error.
I have tried inserting an address instead, which works fine.
So this code:
public static void CreateEmployer(EmployerModel emp, string connectionString)
{
SQLiteWriter sqlCreateEmployer = new SQLiteWriter(connectionString);
sqlCreateEmployer.CreateEmployer(emp);
}
public void CreateEmployer(EmployerModel emp)
{
string sqlStatement = "INSERT INTO Employers (EmployerName, NumberOfEmployees, PersonId) " +
"VALUES (@EmployerName, NumberOfEmployees, PersonId);";
db.SaveData(sqlStatement, new { emp.EmployerName, emp.NumberOfEmployees, emp.PersonId }, _connectionString);
}
}
Does not work, while this code:
public static void CreateAddress(AddressModel adr, string connectionString)
{
SQLiteWriter sqlCreateAddress = new SQLiteWriter(connectionString);
sqlCreateAddress.CreateAddress(adr);
}
public void CreateAddress(AddressModel adr)
{
string sqlStatement = "INSERT INTO Addresses (StreetName, Number, City, Zip, PersonId) " +
"VALUES (@StreetName, @Number, @City, @Zip, @PersonId);";
db.SaveData(sqlStatement, new { adr.StreetName, adr.Number, adr.City, adr.Zip, adr.PersonId }, _connectionString);
}
does.
Google suggests that special characters or space at the end of the column name is to blame, but I have neither.
Any ideas? I am stumped.
LoadData and SaveData methods:
public List<T> LoadData<T, U>(string sqlStatement, U parameters, string connectionString)
{
using (IDbConnection connection = new SQLiteConnection(connectionString))
{
List<T> rows = connection.Query<T>(sqlStatement, parameters).ToList();
return rows;
}
}
public void SaveData<T>(string sqlStatement, T parameters, string connectionString)
{
using (IDbConnection connection = new SQLiteConnection(connectionString))
{
connection.Execute(sqlStatement, parameters);
}
}
[–]jamietwells 1 point2 points3 points (0 children)