今天想修改sqlite数据库中密码,总是报错,排查多次没有解决,请高手指点
import win.ui;
import sqlite;
var winform = win.form(text="修改密码";right=320;bottom=220;center=1)
winform.add(
editOld={cls="edit";left=100;top=30;right=220;bottom=55;edge=1;password=1;z=1};
editNew={cls="edit";left=100;top=70;right=220;bottom=95;edge=1;password=1;z=2};
editConfirm={cls="edit";left=100;top=110;right=220;bottom=135;edge=1;password=1;z=3};
btnOk={cls="button";text="确定";left=60;top=170;right=130;bottom=200;z=4};
btnCancel={cls="button";text="取消";left=180;top=170;right=250;bottom=200;z=5};
staticOld={cls="static";text="原密码:";left=40;top=35;right=95;bottom=55;z=6};
staticNew={cls="static";text="新密码:";left=40;top=75;right=95;bottom=95;z=7};
staticConfirm={cls="static";text="确认密码:";left=25;top=115;right=95;bottom=135;z=8}
)
// 密码强度检查
var checkStrength = function(pwd) {
if(#pwd < 6) return false;
var hasLetter = false, hasNum = false;
for(i=1; #pwd; 1) {
var c = string.sub(pwd,i,i);
if(string.match(c,"[a-zA-Z]")) hasLetter = true;
if(string.match(c,"[0-9]")) hasNum = true;
}
return hasLetter && hasNum;
}
// 验证原密码
var validateOld = function(pwd) {
win.msgbox("你输入的原密码: [" + pwd + "]", "调试");
win.msgbox("数据库绝对路径: " + io.fullpath("db.db"), "调试");
var db = sqlite("db.db");
var t = db.getTable("SELECT password FROM users WHERE username='admin'");
if(t && #t > 0){
win.msgbox("数据库admin密码: [" + t[1].password + "] 类型: " + type(t[1].password) + " 长度: " + #t[1].password, "调试");
}else{
win.msgbox("数据库没有admin用户", "调试");
}
var check = db.getTable("SELECT id FROM users WHERE username=? AND password=?","admin",pwd);
win.msgbox("check内容: " + table.tostring(check), "调试");
db.close();
if(type(check) == "table" && #check > 0){
return true;
}else{
return false;
}
}
// 更新密码
var updatePwd = function(newPwd) {
var db = sqlite("db.db");
var n = db.exec("UPDATE users SET password=? WHERE username='admin'", newPwd);
db.close();
return n > 0;
}
winform.btnOk.oncommand = function(id,event){
var oldPwd = string.trim(winform.editOld.text);
var newPwd = string.trim(winform.editNew.text);
var confirmPwd = string.trim(winform.editConfirm.text);
if(!oldPwd || !newPwd || !confirmPwd) {
win.msgbox("所有字段都不能为空!"); return;
}
if(!validateOld(oldPwd)) {
win.msgbox("原密码错误!"); return;
}
if(newPwd != confirmPwd) {
win.msgbox("两次新密码不一致!"); return;
}
if(oldPwd == newPwd) {
win.msgbox("新密码不能与原密码相同!"); return;
}
if(!checkStrength(newPwd)) {
win.msgbox("新密码需6位以上且包含字母和数字!"); return;
}
if(updatePwd(newPwd)) {
win.msgbox("密码修改成功!"); winform.close();
} else {
win.msgbox("密码修改失败!");
}
}
winform.btnCancel.oncommand = function(id,event){
winform.close();
}
winform.show();
win.loopMessage();