善小而为_ 发表于 2024-2-20 17:05:17

通用代码:sqlite用户登录

本帖最后由 善小而为_ 于 2024-2-20 17:08 编辑

//sqlite用户登录
import win.ui;
/*DSG{{*/
var winform = win.form(text="用户登录";right=530;bottom=275)
winform.add(
button={cls="button";text="登录";left=206;top=201;right=294;bottom=236;db=1;dl=1;z=5};
editPass={cls="edit";left=206;top=125;right=345;bottom=155;db=1;dl=1;dr=1;dt=1;edge=1;password=1;z=4};
editUser={cls="edit";left=206;top=50;right=345;bottom=79;dl=1;dr=1;dt=1;edge=1;multiline=1;z=2};
static={cls="static";text="用户名";left=82;top=50;right=162;bottom=79;align="center";center=1;dl=1;dt=1;transparent=1;z=1};
static2={cls="static";text="密码";left=82;top=125;right=173;bottom=155;align="center";center=1;db=1;dl=1;dt=1;transparent=1;z=3}
)
/*}}*/

import sqlite
import console

var dbName = io.joinpath(io._exedir,"mydb.db");
var db = sqlite(dbName)

//初始化
var userTbl = "user"
if !db.existsTable(userTbl){
        db.exec("create table ??(id,name,pass) ",{userTbl})

        var dataTab = {
                {1, 'admin', '123456'},
                {2, 'user', '123456'},
        }
        db.beginTrans(); //事务开始
        for(i=1;#dataTab;1){
                db.exec("INSERT INTO ?? VALUES (?) ",{userTbl,dataTab});
        }
        db.commitTrans(); //事务结束
       
        //var dt = db.getTable("select * from ?? ",{userTbl});
        //console.dump(dt)
}

winform.button.oncommand = function(id,event){
        name = winform.editUser.text;
        pass = winform.editPass.text;
        //winform.msgbox("用户名:"+name+",密码:"+pass)
        var dt = db.getTable("select count(*) as count from ?? where name=@name and pass=@pass ",
                        {"user", name=name, pass=pass});
        //console.dump(dt)
        if !dt.count {
                winform.msgbox("用户登录失败!");
                return ;
        } else {
                winform.msgbox("用户登录成功!")
        }
}

winform.onClose = function(hwnd,message,wParam,lParam){
    if db db.close()
}

winform.show();
win.loopMessage();

jelin 发表于 2024-2-21 07:18:44

感谢分享

Squall8965 发表于 2024-3-10 15:19:42

谢谢分享。
页: [1]
查看完整版本: 通用代码:sqlite用户登录