C# 注册并使用sqlite 自定义函数

本文详细介绍了如何在SQLite中实现自定义函数,包括ToUpper和GetChinesePYChar两个示例,展示了如何通过创建类来扩展SQLite的功能,并提供了实际应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,sqlite 没有 ToUpper 的自带函数。 没关系,我们可以创建它;

2,sqlite 不能直接创建自定义函数,不能像 sql server中那样方便创建并使用。没关系,我们照样可以创建它,创建成功后,我们照样可以随心所欲(比如 批量更新等)。


效果:

            var ss = JonseTest.SqlLiteHelper.GetSingle(out sError, "select ToUpper('ABCcdsf')");   // ABCCDSF
            var ss2 = JonseTest.SqlLiteHelper.GetSingle(out sError, "select GetChinesePYChar('我a*%爱你中国')");   // Wa*%ANZG

             

方案:

step1: 创建类 ToUpper   和 GetChinesePYChar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace MetroGuide
{
    [SQLiteFunction(Name = "ToUpper", Arguments = 1, FuncType = FunctionType.Scalar)]
    public class ToUpper : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            return args[0].ToString().ToUpper();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace MetroGuide
{
    [SQLiteFunction(Name = "GetChinesePYChar", Arguments = 1, FuncType = FunctionType.Scalar)]
    public class GetChinesePYChar : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            string sOrigion = args[0].ToString();
            return Common.GetPYString(sOrigion);
        }
    }
}

step2:

            var ss = JonseTest.SqlLiteHelper.GetSingle(out sError, "select ToUpper('ABCcdsf')");   // ABCCDSF
            var ss2 = JonseTest.SqlLiteHelper.GetSingle(out sError, "select GetChinesePYChar('我a*%爱你中国')");   // Wa*%ANZG


step3:

           public static string ResetAllPointNameChinesePYChar()
          {
                 return "update tblLinePoint set ChinesePYChar=GetChinesePYChar(Name)";
          }


结束。


参考 :

http://blog.youkuaiyun.com/huanshanv20008/article/details/7635092

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值