C # Sqlite sequence

sqlite can not directly create a custom function, not like sql server as easy to create and use. However, we can still create after it is created, we can still be arbitrary (such as batch updates, etc.)

Sequence is a very common database operation, created in other relational database is fairly simple, but Sqlite not very convenient, because it can not directly create custom functions

1. create a table showing the sequence:

 

CREATE TABLE SEQUENCE (  
SEQ_NAME            VARCHAR(50) NOT NULL,
MIN_VAL                DECIMAL(12,0) NOT NULL,  
CURRENT_VAL        DECIMAL(12,0) NOT NULL,  
MAX_VAL                DECIMAL(12,0) NOT NULL DEFAULT 1,
INCREMENT            INT NOT NULL DEFAULT 1,  
PRIMARY KEY (SEQ_NAME)  
);
View Code

Sequence defined minimum, maximum, step size, and the current value of the name of the sequence

2. Create Trigger

 

CREATE TRIGGER [SEQ_RESET_TRG]
AFTER UPDATE
ON [SEQUENCE]
FOR EACH ROW
begin 
        UPDATE SEQUENCE SET CURRENT_VAL=MIN_VAL WHERE CURRENT_VAL-INCREMENT>=MAX_VAL;
     end;
View Code

When the current value is greater than the maximum value, it is reset to the minimum number to achieve the purpose of recycling.

Use in C # code creates the function, SqliteHelper visiting Sqlite public libraries, are described in my "C # Sqlite help classes."


3. Get the current value of the sequence

   [SQLiteFunction(Name = "GetCurrentValue", Arguments = 1, FuncType = FunctionType.Scalar)]
    public class GetCurrentValue : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            data.Add("V_SEQ_NAME", args[0].ToString());
            string sql = "SELECT CURRENT_VAL  FROM SEQUENCE  WHERE SEQ_NAME = @V_SEQ_NAME; ";
            return SqliteHelper.ExecuteScalar(sql,data);
        }
    }
View Code

4. Get next sequence value

[SQLiteFunction(Name = "GetNextValue", Arguments = 1, FuncType = FunctionType.Scalar)]
    public class GetNextValue : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            data.Add("V_SEQ_NAME", args[0].ToString());
            string sql = "UPDATE SEQUENCE  SET CURRENT_VAL = CURRENT_VAL + INCREMENT  WHERE SEQ_NAME = @V_SEQ_NAME; ";
            SqliteHelper.ExecuteNonQuery(sql, data);
            return SqliteHelper.ExecuteScalar(string.Format("SELECT GetCurrentValue('{0}')",args[0].ToString()),null);
        }
    }
View Code

5. Set the value of the current sequence

 [SQLiteFunction(Name = "SetValue", Arguments = 2, FuncType = FunctionType.Scalar)]
    public class SetValue : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            data.Add("V_SEQ_NAME", args[0].ToString());
            data.Add("V_VALUE", args[1].ToString());
            string sql = "UPDATE SEQUENCE SET CURRENT_VAL = @V_VALUE  WHERE SEQ_NAME= @V_SEQ_NAME; ";
            SqliteHelper.ExecuteScalar(sql, data);
            return SqliteHelper.ExecuteScalar(string.Format("SELECT GetCurrentValue('{0}')", args[0].ToString()), null);
        }
    }
View Code

6. Test:

Adding a line of data in the Sequence Listing SEQUENCE

Defined sequence name as PURCHASE_IN_ORDER, the minimum value is 2000, the current value is 2000, the maximum value is 9999, a step size of 1.

Execute the statement:

 String  SQL =  String .Format ( "the Select getNextValue ( 'PURCHASE_IN_ORDER')" );
  SqliteHelper .ExecuteNonQuery (SQL, null ); 
to see whether the current value in the database increases

 

 

 

 

 

Reproduced in: https: //www.cnblogs.com/OnlyVersion/p/3746169.html

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/93293210