Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQLite和C-插入并更新whit CASE检查_C#_Sqlite_Insert - Fatal编程技术网

C# SQLite和C-插入并更新whit CASE检查

C# SQLite和C-插入并更新whit CASE检查,c#,sqlite,insert,C#,Sqlite,Insert,我正在尝试使用C和sqlite编辑此表中的插入 SQLite表: CREATE TABLE "utenti" ( "id" INTEGER NOT NULL, "grado" TEXT, "cognome" TEXT NOT NULL, "nome" TEXT NOT NULL, "codice_fiscale" TEXT ) CREATE TABLE "utenti" ( "id" INTEGER NOT NULL,

我正在尝试使用C和sqlite编辑此表中的插入

SQLite表:

CREATE TABLE "utenti" (
    "id"    INTEGER NOT NULL,
    "grado" TEXT,
    "cognome"   TEXT NOT NULL,
    "nome"  TEXT NOT NULL,
    "codice_fiscale"    TEXT
)
CREATE TABLE "utenti" (
    "id"    INTEGER NOT NULL,
    "grado" TEXT,
    "cognome"   TEXT NOT NULL,
    "nome"  TEXT NOT NULL,
    "codice_fiscale"    TEXT,
    "presente"  INTEGER
)
我想确保我们首先检查文本框中是否有id为的记录。 如果它存在并且uscita列的值为空,则更新该行并在uscita列中添加值。 而如果文本框中没有插入id为_utete的行,或者如果它们存在但输入了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;

            }
        }
我做了各种测试和尝试,但我发现自己很困难,因为表中行的控制变量

其他错误测试

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

我通过改变一些表格和它们的读取方式来解决这个问题

SQLite表:

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


如果您使用的是SQLite,那么请使用SQLite所期望的SQL语法。
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;

        }

}