如何使用sqlite计算平方根?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (40)

我需要在sqite数据库中计算欧几里德距离。

除了编写和加载数学函数的动态库之外,还有人知道如何在sqite中计算平方根吗?

提问于
用户回答回答于

嗯,我有个半个答案。

是的,这涉及到一个第三的聚会,但你不必自己写:你检查了最后一个分机吗?

它包括几个数学函数,其中包括sqrt()。

用户回答回答于

用户定义的SQLite函数对我来说很难实现。最后,经过很长一段时间的搜索,我能够在我的C#代码中实现它。主要功能如下:

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

登记:

SQLiteFunction.RegisterFunction(typeof(Sqrt));

并在SELECT中使用:

SQLiteCommand com = new SQLiteCommand("select sqrt(10.42)", connection);

或者,如果只想查看代码(或者通过我的代码的所有部分),我将粘贴在SQLite数据库中计算平方根的完整工作示例代码下面。要创建和运行此示例,请执行以下6个步骤:

  1. 创建新项目(我的名字是sqrt)
  2. 包括对项目的SQLite引用:解决方案资源管理器->引用(右击:添加引用)->程序集-扩展-System.Data.SQLite(检查)->OK
  3. 开着App.config并替换为这个(如果没有这个步骤,可能会得到混合模式装配错误):<?xml version="1.0" encoding="utf-8" ?><configuration><startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0"/></startup></configuration>
  4. 替换你的Form1.Designer.cs使用此代码: <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup> </configuration>
  5. 使用Form1.Designer.cs namespace Sqrt { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.txb_Input = new System.Windows.Forms.TextBox(); this.txb_Output = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.btn_Calcualte = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txb_Input // this.txb_Input.Location = new System.Drawing.Point(131, 12); this.txb_Input.Name = "txb_Input"; this.txb_Input.Size = new System.Drawing.Size(201, 20); this.txb_Input.TabIndex = 0; // // txb_Output // this.txb_Output.BackColor = System.Drawing.Color.WhiteSmoke; this.txb_Output.Location = new System.Drawing.Point(131, 38); this.txb_Output.Name = "txb_Output"; this.txb_Output.ReadOnly = true; this.txb_Output.Size = new System.Drawing.Size(201, 20); this.txb_Output.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(31, 13); this.label1.TabIndex = 1; this.label1.Text = "Input"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 41); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(39, 13); this.label2.TabIndex = 1; this.label2.Text = "Output"; // // btn_Calcualte // this.btn_Calcualte.Location = new System.Drawing.Point(257, 64); this.btn_Calcualte.Name = "btn_Calcualte"; this.btn_Calcualte.Size = new System.Drawing.Size(75, 23); this.btn_Calcualte.TabIndex = 2; this.btn_Calcualte.Text = "Calculate"; this.btn_Calcualte.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(344, 98); this.Controls.Add(this.btn_Calcualte); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txb_Output); this.Controls.Add(this.txb_Input); this.Name = "Form1"; this.Text = "Root square example"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txb_Input; private System.Windows.Forms.TextBox txb_Output; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btn_Calcualte; } }

  1. 使用using System; using System.Data.SQLite; using System.Windows.Forms; namespace Sqrt { // definition of custom sqlite function [SQLiteFunction(Arguments = 1, FuncType = FunctionType.Scalar, Name = "Sqrt")] class Sqrt : SQLiteFunction { public override object Invoke(object[] args) { return Math.Sqrt(Double.Parse(args[0].ToString())); // return result of math sqrt function } } public partial class Form1 : Form { public Form1() { InitializeComponent(); this.btn_Calcualte.Click += new System.EventHandler(this.btn_Calcualte_Click); } private void btn_Calcualte_Click(object sender, EventArgs e) { if (txb_Input.Text.Length == 0) return; try { SQLiteConnection.CreateFile(AppDomain.CurrentDomain.BaseDirectory + "test.s3db"); } catch { } SQLiteConnection con = new SQLiteConnection("Data Source=test.s3db"); SQLiteFunction.RegisterFunction(typeof(Sqrt)); // register custom function con.Open(); SQLiteCommand com = new SQLiteCommand("select sqrt(" + txb_Input.Text.Replace(',', '.') + ")", con); // select result string res = com.ExecuteScalar().ToString(); txb_Output.Text = res; } } }

扫码关注云+社区