SQLite 和 C# - 使用 CASE 检查插入和更新

SQLite and C# - INSERT and UPDATE whit CASE check

我正在尝试使用 C# 和 sqlite 编辑此 table 中的插入内容。

SQLite Table:

CREATE TABLE "utenti" (
    "id"    INTEGER NOT NULL,
    "grado" TEXT,
    "cognome"   TEXT NOT NULL,
    "nome"  TEXT NOT NULL,
    "codice_fiscale"    TEXT
)

CREATE TABLE "entrate" (
    "id"    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "id_utente" INTEGER NOT NULL,
    "nome"  TEXT NOT NULL,
    "entrata"   TEXT NOT NULL,
    "uscita"    TEXT
);

我想确定一下,首先我们去检查文本框中是否有插入 "id_utente" 的记录。 如果它存在并且 "uscita" 列的值为空,则更新该行并在 "uscita" 列中添加值。 如果文本框中没有插入带有 "id_utente" 的行,或者它们存在但已输入 "entrata" 和 "uscita" 列值,则插入一条新记录。


测试代码:

public static bool SavePerson(int idUtente, string nome, string orario)
        {
            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                var output = cnn.Query($"select * from utenti where id = {idUtente}").FirstOrDefault();


                if (output != null) {
                    nome = FindCognome(idUtente);
                    string query;


                    query = $"IF NOT EXISTS(SELECT (id_utente, nome, entrata) FROM entrate WHERE id_utente = '\"{idUtente}\"')" +
                    $"BEGIN" 
                    $"INSERT INTO entrate (id_utente, nome, entrata) VALUES (\"{idUtente}\",\"{nome}\",\"{orario}\")" +
                    $"END" +
                    $"ELSE" +
                    $"BEGIN" +
                    $"UPDATE entrate (id_utente, nome, entrata) VALUES (\"{idUtente}\",\"{nome}\",\"{orario}\") WHERE id_utente = '\"{idUtente}\"')" +
                    $"END";

                    //cnn.Execute($"INSERT INTO entrate (id_utente, nome, entrata) VALUES (\"{idUtente}\",\"{nome}\",\"{orario}\")");


                    cnn.Execute(query);
                    cnn.Close();
                    return true;
                } 
                else return false;

            }
        }

我做了各种测试和尝试,但我发现自己很困难,对于table中行的控制变量。


其他错误测试

query = $"UPDATE entrate SET uscita = \"{orario}\"," +
                        $"CASE WHEN id_utente = {idUtente}" +
                        $"ELSE INSERT INTO entrate (id_utente, nome, entrata) VALUES (\"{idUtente}\",\"{nome}\",\"{orario}\")";

我通过更改一些表及其读取方式解决了问题

SQLite Table:

CREATE TABLE "utenti" (
    "id"    INTEGER NOT NULL,
    "grado" TEXT,
    "cognome"   TEXT NOT NULL,
    "nome"  TEXT NOT NULL,
    "codice_fiscale"    TEXT,
    "presente"  INTEGER
)

CREATE TABLE "entrate" (
    "id"    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "id_utente" INTEGER NOT NULL,
    "nome"  TEXT NOT NULL,
    "entrata"   TEXT NOT NULL,
    "uscita"    TEXT
)

代码:

public static bool SavePerson(int idUtente, string nome, string orario)
{
    using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
    {
        var output = cnn.Query($"select * from utenti where id = {idUtente}").FirstOrDefault();

        if (output != null) {
            nome = FindCognome(idUtente);
            var checkpresenza = cnn.QuerySingle<int>($"select presente from utenti where id = \"{idUtente}\" limit 1");
            Console.WriteLine(checkpresenza);

            if (checkpresenza == 0)
            {
                cnn.Execute($"INSERT INTO entrate (id_utente, nome, entrata) VALUES (\"{idUtente}\",\"{nome}\",\"{orario}\")");
                cnn.Execute($"UPDATE utenti SET presente = \"1\"  WHERE id = {idUtente}");
                presenza = 0;

            }
            else if (checkpresenza == 1)
            {
                cnn.Execute($"UPDATE entrate SET uscita = \"{orario}\" where id_utente = {idUtente}");
                cnn.Execute($"UPDATE utenti SET presente = \"0\"  WHERE id = {idUtente}");
                presenza = 1;
            }

            cnn.Close();
            return true;
        } 
        else return false;

        }

}