程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了通过 Vec<Row> 从 MySQL 读取数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决通过 Vec<Row> 从 MySQL 读取数据?

开发过程中遇到通过 Vec<Row> 从 MySQL 读取数据的问题如何解决?下面主要结合日常开发的经验,给出你关于通过 Vec<Row> 从 MySQL 读取数据的解决方法建议,希望对你解决通过 Vec<Row> 从 MySQL 读取数据有所启发或帮助;

通过 Vec<Row> 从 MysqL 读取数据的最佳(简单、安全和高效)方式是什么?

这是一个使用(特定)元组向量的示例函数:

fn read_recs(tx: &mut transaction) -> Result<HashMap<i64,String>> {
    let q = format!("SELECT ID,name FROM some_table");
    let rows: Vec<(i64,String)> = tx.query(q)?;
    let mut res = HashMap::new();
    for r in rows {
        res.insert(r.0,r.1);
    }
    Ok(res)
}

对于简单的查询,我发现这种方法已经足够好了(以及 query_map()),但是对于返回更多列的查询,我更愿意直接读入 Vec<Row>,然后以某种方式读取像这样的简单(不太冗长)方式的值:

let rows: Vec<Row> = tx.query(q)?;
for r in rows {
    let ID: i64 = extract(r,0)?;
    let name: String = extract(r,1)?;
}

或者甚至这个:

let rows: Vec<Row> = tx.query(q)?;
for r in rows {
    let ID: i64 = extract(r,"ID")?;
    let name: String = extract(r,"name")?;
}

问题是,extract 会是什么样子才能成功读取数据或返回错误?我希望它恐慌,例如,在我只期望非空值的情况下,查询返回 NulL。显然,我希望不要每次从字段中读取数据时都必须处理 Option<...> 值,因为那样会使我的代码过于冗长。

便说一句,如果这很重要,我会使用 Anyhow

解决方法

我不知道我是否完全理解这个问题,但这行不通吗?

public class Polygon
{
    private ArrayList<Point> shape; //instance variable
    
    public Polygon() //constructs a new ArrayList of points called shape
    {
        shape = new ArrayList<Point>(); 
    }
    
    public void add(Point point) //takes in a Point and adds it to the ArrayList "shape"
    {
        shape.add(point); 
    }
    
    public double perimeter()
    {   
        double @R_171_10586@l = 0;
        if (shape.size() > 2) //a polygon must have more than 2 sides
        {
            for (int i = 0; i < shape.size() - 1; i++) //for every point in the list,except for the last
            {
                double x1 = shape.get(i).getX();
                double y1 = shape.get(i).getY();
                double x2 = shape.get(i + 1).getX();
                double y2 = shape.get(i + 1).getY();
                double dist = Math.sqrt(Math.abs(Math.pow((x2 - x1),2) //get the distance between the point
                                        + Math.pow((y2 - y1),2)));     //and the following point
                @R_171_10586@l = @R_171_10586@l + dist; //add this distance to the @R_171_10586@l
            }
            double firstX = shape.get(0).getX();
            double firstY = shape.get(0).getY();
            double lastX = shape.get(shape.size() - 1).getX();
            double lastY = shape.get(shape.size() - 1).getY();
            double finalDist = Math.sqrt(Math.abs(Math.pow((lastX - firstX),2) //get the distance between 
                                        + Math.pow((lastY - firstY),2)));      //the first & last points
            @R_171_10586@l = @R_171_10586@l + finalDist; //add it to the @R_171_10586@l
        }
        return @R_171_10586@l;
    }
    
    public void draw()
    {
        for (int i = 0; i < shape.size() - 1; i++) //for every point in the list,except for the last
        {
            double x1 = shape.get(i).getX();
            double y1 = shape.get(i).getY();
            double x2 = shape.get(i + 1).getX();
            double y2 = shape.get(i + 1).getY();
            Line line = new Line(x1,y1,x2,y2); //create a new Line "line"
            line.draw(); //draw the line
        }
        double firstX = shape.get(0).getX();
        double firstY = shape.get(0).getY();
        double lastX = shape.get(shape.size() - 1).getX();
        double lastY = shape.get(shape.size() - 1).getY();
        Line lastLine = new Line(lastX,lastY,firstX,firstY); //get the first & last points & create a line
        lastLine.draw(); //draw this line too
    }
}

大佬总结

以上是大佬教程为你收集整理的通过 Vec<Row> 从 MySQL 读取数据全部内容,希望文章能够帮你解决通过 Vec<Row> 从 MySQL 读取数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。