< 返回版块

fengyexin9 发表于 2021-06-23 17:39

rust中使用到的sqlite,传递的参数是一个或多个,多个参数如何通过变量进行处理; sqlite获取到的数据,比如有些列是null的,如何设置到对象中去

比如:

pub struct person { pub id: i32, pub name: String, pub age: i32, pub address: String, }

比如数据库中的address为null, 那么在查询中返回的 person中的address的值当如何进行判断并设置

评论区

写评论
Mike Tang 2021-06-24 23:07

学一下md的代码段语法,现在的效果乱七八糟的。

作者 fengyexin9 2021-06-24 12:07

目前的理解是使用数组进行动态添加,不过数组使用的字段的类型是一致的,代码事例如下: let mut qparams:Vec = Vec::new(); let mut sql = String::from("select * from table where 1 = 1 ") if target == 0 { sql.push_str(" and key_index = ? "); qparams.push(key_index); } else if target == 1 { if bid != -1 { sql.push_str(" and bid = ? "); qparams.push(bid); } if cid != -1 { sql.push_str(" and cid = ? "); qparams.push(cid); } if vids != "" { let ivids:Vec = splitStr(&vids); sql.push_str(" and vid in ( "); let mut index = 0; let len = ivids.len() - 1; for vid in ivids { if(index == len) { sql.push_str(" ? "); qparams.push(vid); } else { sql.push_str(" ?, "); qparams.push(vid); } index += 1; } sql.push_str(" ) "); } } else if target == 2 { if bid != -1 { sql.push_str(" and bid = ? "); qparams.push(bid); } if cids != "" { let icids:Vec = splitStr(cids); sql.push_str(" and cid in ( "); let mut index = 0; let len = icids.len() - 1; for cid in icids { if(index == len) { sql.push_str(" ? "); qparams.push(cid); } else { sql.push_str(" ?, "); qparams.push(cid); } index += 1; } sql.push_str(" ) "); } } sql.push_str(" order by vid asc");

////////////////////////////下面是一个方法:将带有逗号和下划线的字符串数据转换为i32的数组,如字符串为:1,2,3,5-7;那么该字符串转换的数组为[1,2,3,5,6,7], // 将带有,号和-的转换为i32数组 fn splitStr(ids: &str) -> Vec{ let mut list: Vec = Vec::new(); let mut arrs = ids.split(","); for arr in arrs { if arr.contains("-") { let darrs = arr.split("-"); let mut index = 0; let mut startIndex = 0; let mut endIndex = 0; for da in darrs { if index == 0 { startIndex = da.parse::().unwrap(); } else { endIndex = da.parse::().unwrap() + 1; } index += 1; } for vid in startIndex..endIndex { list.push(vid); } } else { let vid = arr.parse::().unwrap(); list.push(vid); } } list }

作者 fengyexin9 2021-06-23 18:18

返回值为null的问题已经处理了,在sql上加入coalesce(列名,'')

1 共 3 条评论, 1 页