Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Cascade Delete

actually i'm developping windows metro app using SQLite DataBase. i use sqlite manager (mozilla) for administration. i tried delete cascade but it work only in sqlite manager not in C# code :

My function


public async Task<string> DeleteSurvey(int SurveyID)
{
    string result = string.Empty;
    var db = new SQLite.SQLiteAsyncConnection(App.DBPath);
    var survey = await GetSurvey(SurveyID);
    var res = await db.DeleteAsync(survey);

    if (res > 0)
        result = "Success";
    else
        result = "Echec";

    return result;
}

db.CreateTable<Survey>();

SQLiteCommand command1 = new SQLiteCommand(db);
command1.CommandText = "create table if not exists SurveyItemGroup";
command1.CommandText += "(ID integer primary key autoincrement not null, IDSurvey integer,";
command1.CommandText += "Number integer, Name varchar(50), FOREIGN KEY(IDSurvey) REFERENCES Survey(ID) ON DELETE CASCADE ON UPDATE CASCADE)";
command1.ExecuteNonQuery();

In C# code it only delete survey table not both (Survey and SurveyItemGroup)

PS: I have the same problem with pragma (pragma foreign_keys=ON;) it works only if i do it sqlite manager.

like image 898
Bedine Avatar asked May 14 '26 15:05

Bedine


1 Answers

(I'm adding this answer because the OP noted this as his solution, but only in a comment.)

Currently, for ON DELETE CASCADE to work, pragma foreign_keys=on; must be issued in each new connection. It's not currently a persistent setting.

like image 57
Peter Hansen Avatar answered May 17 '26 06:05

Peter Hansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!