0

CREATED 列と MODIFIED 列を含むテーブルがあります。CREATED 値を一度だけ挿入し、それ以降は不変にしたい。私はこれを退屈な方法で行う方法を知っています (「DoesRecordExist()」メソッドを作成し、それに基づいてクエリとクエリ パラメータの数を変更します) が、これを達成するためのよりスマートな方法があることは確かです。結局のところ、これは一般的な要件 (「データベース パターン」) でなければなりません。

私のコードはこれです:

public void InsertUserSiteRecord(UserSite us)
{
    using (SQLiteConnection conn = new SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            cmd.CommandText =
                String.Format(
                    @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Created, Modified) 
                                VALUES (@SiteNum, @SerialNum, @UserName, @Created, @Modified)");
            cmd.Parameters.Add(new SQLiteParameter("SiteNum", us.SiteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum", us.SerialNum));
            cmd.Parameters.Add(new SQLiteParameter("UserName", us.UserName));
            cmd.Parameters.Add(new SQLiteParameter("Created", us.Created));
            cmd.Parameters.Add(new SQLiteParameter("Modified", us.Modified));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

...そして、私はこのようなことをしなければならないことを避けたい:

public void InsertUserSiteRecord(UserSite us)
{
    using (SQLiteConnection conn = new SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            if (!RecordExists(us.SiteNum, us.SerialNum, us.UserName))
            {
                cmd.CommandText =
                    String.Format(
                        @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Created, Modified) 
                                    VALUES (@SiteNum, @SerialNum, @UserName, @Created, @Modified)");
            else
            {
                cmd.CommandText =
                    String.Format(
                        @"INSERT INTO UserSite (SiteNum, SerialNum, UserName, Modified) 
                                    VALUES (@SiteNum, @SerialNum, @UserName, @Modified)");
            }
            cmd.Parameters.Add(new SQLiteParameter("SiteNum", us.SiteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum", us.SerialNum));
            cmd.Parameters.Add(new SQLiteParameter("UserName", us.UserName));
            if (!RecordExists(us.SiteNum, us.SerialNum, us.UserName))
            {
                cmd.Parameters.Add(new SQLiteParameter("Created", us.Created));
            }
            cmd.Parameters.Add(new SQLiteParameter("Modified", us.Modified));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

private bool RecordExists(String SiteNum, String SerialNum, String UserId)
{
    // query the table to see if those three values exist in any record
}

次のような SQL[ite] コンストラクトはありますか。

cmd.Parameters.AddOnlyIfColumnIsEmpty(new SQLiteParameter("Created", us.Created));

? または、これにどのように取り組むのが最善でしょうか?

アップデート

dub stylee の答えは独創的ですが、私である私は、退屈ではあるがより簡単に理解できる道をたどり、次の 3 つの方法を作成しました。

public int UserSiteIdFor(String userName, String serialNum, String siteNum)
{
    int Id;
    const string qry = "SELECT Id FROM UserSite WHERE UserName =
@UserName AND SiteNum = @SiteNum AND SerialNum = @SerialNum";

    try
    {
        using (SQLiteConnection con = new
SQLiteConnection(HHSUtils.GetDBConnection()))
        {
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand(qry, con);
            cmd.Parameters.Add(new SQLiteParameter("UserName",
userName));
            cmd.Parameters.Add(new SQLiteParameter("SiteNum", siteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum",
serialNum));
            Id = Convert.ToInt32(cmd.ExecuteScalar());
        }
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format(
            "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message,
ex.InnerException, ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From
TestHHSDBUtils.UserSiteIdFor: {0}", msgInnerExAndStackTrace));
        return 0;
    }
    return Id;
}

public void InsertUserSiteRecord(UserSite us)
{
    using (SQLiteConnection conn = new
SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            cmd.CommandText =
                String.Format(
                    @"INSERT INTO UserSite (SiteNum, SerialNum,
UserName, Created, Modified) 
                                VALUES (@SiteNum, @SerialNum, @UserName,
@Created, @Modified)");
            cmd.Parameters.Add(new SQLiteParameter("SiteNum",
us.SiteNum));
            cmd.Parameters.Add(new SQLiteParameter("SerialNum",
us.SerialNum));
            cmd.Parameters.Add(new SQLiteParameter("UserName",
us.UserName));
            cmd.Parameters.Add(new SQLiteParameter("Created",
us.Created));
            cmd.Parameters.Add(new SQLiteParameter("Modified",
us.Modified));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

public void UpdateUserSiteRecord(int Id, String lastLogin)
{
    using (SQLiteConnection conn = new
SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(conn))
        {
            cmd.CommandText = String.Format(@"UPDATE UserSite SET
Modified = @Modified WHERE Id = @Id");
            cmd.Parameters.Add(new SQLiteParameter("Id", Id));
            cmd.Parameters.Add(new SQLiteParameter("Modified",
lastLogin));

            cmd.ExecuteNonQuery();
        }
        conn.Close();
    }
}

...そして、次のように呼び出します。

int userSiteId = hhsdbutils.UserSiteIdFor(us.UserName, us.SerialNum, us.SiteNum);
if (userSiteId > 0)
{
    hhsdbutils.UpdateUserSiteRecord(userSiteId, us.Modified);
}
else
{
    hhsdbutils.InsertUserSiteRecord(us);
}

できます。

4

2 に答える 2

2

を使用してこれを達成できる場合がありますtrigger。ここでトリガーに関する SQLite ドキュメントを参照してください: https://www.sqlite.org/lang_createtrigger.html

基本的に、INSTEAD OFトリガーを作成してから、それに応じてクエリを設定します。ドキュメントから:

For an example of an INSTEAD OF trigger, consider the following schema:

CREATE TABLE customer(
  cust_id INTEGER PRIMARY KEY,
  cust_name TEXT,
  cust_addr TEXT
);
CREATE VIEW customer_address AS
   SELECT cust_id, cust_addr FROM customer;
CREATE TRIGGER cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON customer_address
BEGIN
  UPDATE customer SET cust_addr=NEW.cust_addr
   WHERE cust_id=NEW.cust_id;
END;
With the schema above, a statement of the form:

UPDATE customer_address SET cust_addr=$new_address WHERE cust_id=$cust_id;
Causes the customer.cust_addr field to be updated for a specific customer entry that has customer.cust_id equal to the $cust_id parameter. Note how the values assigned to the view are made available as field in the special "NEW" table within the trigger body.

やりたいことは、元のクエリがその列を更新するために渡したとしても、その列を更新しないようにトリガーを設定することです。

于 2015-02-10T22:02:49.740 に答える