请教

faiger 5月前 375

 import win.ui;
import sqlite;
import console;
/*DSG{{*/
var winform = win.form(text="数据录入";right=700;bottom=378)
winform.add(
btnDelete={cls="button";text="删除";left=576;top=21;right=667;bottom=50;z=2};
btn_adddb={cls="button";text="添加";left=471;top=22;right=562;bottom=50;z=11};
ed_age={cls="edit";left=253;top=24;right=345;bottom=43;edge=1;z=6};
ed_kg={cls="edit";left=253;top=59;right=345;bottom=82;edge=1;z=5};
ed_love={cls="edit";left=83;top=59;right=175;bottom=82;edge=1;z=4};
ed_name={cls="edit";left=83;top=24;right=175;bottom=47;edge=1;z=3};
listview={cls="listview";left=13;top=91;right=686;bottom=357;bgcolor=16777215;edge=1;fullRow=1;gridLines=1;z=1};
static={cls="static";text="姓名:";left=43;top=27;right=85;bottom=47;transparent=1;z=7};
static2={cls="static";text="年龄:";left=212;top=29;right=254;bottom=49;transparent=1;z=8};
static3={cls="static";text="性别:";left=213;top=63;right=255;bottom=83;transparent=1;z=9};
static4={cls="static";text="住址:";left=42;top=61;right=84;bottom=76;transparent=1;z=10}
)
/*}}*/

winform.listview.setExtended(0x4/*_LVS_EX_CHECKBOXES*/);//勾选框
winform.listview.insertColumn("序号",50,1)
winform.listview.insertColumn("姓名",100,2)
winform.listview.insertColumn("年龄",100,3)
winform.listview.insertColumn("性别",100,4)
winform.listview.insertColumn("住址",100,5)

var sqlConnection = sqlite("\db.db") //连接数据库
if(not sqlConnection.existsTable("filea")){        //如果不存在表,则创建数据表
            sqlConnection.exec("CREATE TABLE filea(
                     id INTEGER PRIMARY KEY AUTOINCREMENT,  
                     name,
                     age,
                     kg,
                     love
                    );"
            )
            win.msgbox("创建表成功","提示")
            //创建索引利于查询
            //sqlConnection.exec("create index 索引名字 on 表名字(索引字段名字)")            
}else {

                var db=sqlConnection.getTable("SELECT * FROM filea");//查询全表数据
                    //将查询到的数据显示于listview
                 for(i=1;#db;1){        
                 winform.listview.addItem();
                 winform.listview.setItemText(tostring(db[ i ].id),i,1)
                 winform.listview.setItemText(tostring(db[ i ].name),i,2)
                 winform.listview.setItemText(tostring(db[ i ].age),i,3)
                 winform.listview.setItemText(tostring(db[ i ].kg),i,4)
                 winform.listview.setItemText(tostring(db[ i ].love),i,5)
                 }
}

//刷新listview
Refresh_main=function(){

        winform.listview.clear();//先清空,再显示,实现刷新
        var db=sqlConnection.getTable("SELECT * FROM filea");
        
                 for(i=1;#db;1){        
                 winform.listview.addItem();
                 winform.listview.setItemText(tostring(db[ i ].id),i,1)
                 winform.listview.setItemText(tostring(db[ i ].name),i,2)
                 winform.listview.setItemText(tostring(db[ i ].age),i,3)
                 winform.listview.setItemText(tostring(db[ i ].kg),i,4)
                 winform.listview.setItemText(tostring(db[ i ].love),i,5)
        }
}

//删除所选数据
delete_db = function(){
        
for(i=1;winform.listview.count;1){        

                if(winform.listview.getChecked(i)){
               
                        var id=winform.listview.getItemText(i,1)
                        sqlConnection.exec("DELETE FROM filea WHERE id="+id+"")
                }
}
win.msgbox("删除成功","系统提示")
Refresh_main() 
}
//查询表中相同的并删除
delete_thesame = function(name){
sqlConnection.exec("DELETE FROM filea where name not in(select max(id) from filea group by name)")
}
//查询表中相同姓名
select_db = function(name){
return sqlConnection.getTable("select name from filea where name='"+name+"'");       
}
//查询表中有相同数据的便覆盖掉
the_same = function(name,age,kg,love){
        var num = sqlConnection.exec("insert into filea(name,age,kg,love) select '"+name+"','"+age+"','"+kg+"','"+love+"' where not exists(select * from filea where name='"+name+"');")
        win.msgbox("添加成功","系统提示")
        Refresh_main()   
}

//添加
add_db =function(name,age,kg,love){
      var add=sqlConnection.exec("insert into filea (name,age,kg,love) values('"+name+"','"+age+"','"+kg+"','"+love+"')")      
      win.msgbox("添加成功","系统提示")   
      Refresh_main();  
}

winform.listview.onnotify = function(id,code,ptr){
    if(code==0xFFFFFF4B){// 滚动条
        if(!winform.listview.editlable) return ;
        edit = winform.listview.editlable;
        rc=winform.listview.getItemRect(edit.listViewNotifyMessage.iItem,edit.listViewNotifyMessage.iSubItem,,2/*_LVIR_LABEL*/);
        if(rc.top<rc.bottom-rc.top){//选中行向上滚动到了列标题及其以上的位置,不处理的话编辑框会覆盖掉列标题。
            rc.top-=math.abs(rc.bottom);rc.bottom=rc.top;
        }else {
            rc.right += 5; rc.bottom += 5;
        }
        edit.setRect(rc);
        winform.listview.redraw();//经测试,这行会引起滚动条位置变化时listview控件闪烁,如果在意这个问题,请主动给listview控件增加_WS_CLIPCHILDREN样式,可以省略掉这行。
    }
    if(code=0xFFFFFFFD/*_NM_DBLCLK*/ ){ //code=0xFFFFFFFE/*_NM_CLICK鼠标单击*/
        var notifyMessage = winform.listview.getNotifyMessage(code,ptr);
        if( ! notifyMessage.iItem && notifyMessage.iSubItem ) return ;
        
        var edit = winform.listview.editlable
        if( ! edit ){
            winform.listview.addCtrl(
                editlable = {
                    cls="edit";font = LOGFONT( h = 11 );left = 0;top = 0;
                    right = 50;bottom = 50;autoResize = false ;hide = 1;edge = 1;                    
                    wndproc = function( hwnd, message, wParam, lParam ){
                        if( ( message = 0x8/*_WM_KILLFOCUS*/)
                            || message == 0x101/*_WM_KEYUP*/ && wParam == 0xD/*_VK_RETURN*/){
                            // 按下了回车键或失去焦点,更新listview数据
                            owner.parent.setItemText( owner.text, //格子内容
                                                      owner.listViewNotifyMessage.iItem, //行号
                                                      owner.listViewNotifyMessage.iSubItem );//列号
                            //console.log(owner.text, owner.listViewNotifyMessage.iItem, owner.listViewNotifyMessage.iSubItem) //测试显示数据
                            var focus=owner.listViewNotifyMessage.iItem
                            var id=winform.listview.getItemText(focus,1);//focus=第i行,第1列
                            var name=winform.listview.getItemText(focus,2);
                            var age=winform.listview.getItemText(focus,3);
                            var kg=winform.listview.getItemText(focus,4);
                            var love=winform.listview.getItemText(focus,5);
                            console.log(id,name,age,kg,love)
                            //更新语句update
                            update_db(id,name,age,kg,love);
                            //调用刷新
                            Refresh_main();
                            owner.show(false);
                        }
                    }  
                }
            )  
            edit = winform.listview.editlable;
        }
        edit.listViewNotifyMessage = notifyMessage;
        edit.text=winform.listview.getItemText(notifyMessage.iItem,notifyMessage.iSubItem);
        var rc=winform.listview.getItemRect( notifyMessage.iItem,notifyMessage.iSubItem,,2/*_LVIR_LABEL*/ )
        rc.right += 5; rc.bottom += 5;
        edit.setRect(rc);
        edit.show();
        edit.setFocus();// 获取焦点
    }
}

//修改表中数据
update_db = function(id,name,age,kg,love){
        var doubt=win.msgboxTest("确定修改该条数据吗?","系统提示");
        if(doubt){
        sqlConnection.exec("UPDATE filea SET name='"+name+"',age='"+age+"',kg='"+kg+"',love='"+love+"' WHERE id ='"+id+"'");
        }
}

//删除所选数据
winform.btnDelete.oncommand = function(){
     var bool=win.msgboxTest("确定删除该条数据吗?","系统提示")
     
     if(bool){
             delete_db();
     }
}

//添加数据到db
winform.btn_adddb.oncommand = function(){
        
        var name=winform.ed_name.text;
        var age =winform.ed_age.text;
        var kg  =winform.ed_kg.text;
        var love=winform.ed_love.text;
        var name_01;
        var num=select_db(name);
        for(i=1;#num;1){
               
                name_01=num[ i ].name;

            }
    if(name_01!=null){
   
       win.msgbox("该名称数据库已经存在","系统提示")
               
    }else {
            
                 if(name!="" ? age!=null ? kg!=null ? love!=""){
                        add_db(name,age,kg,love)
                }        
    }
}

winform.show()  
win.loopMessage();

本人刚接触aardio,请哪位老师帮改进一下以上代码,不胜感激!

实现功能如下:

  1. 能否改成access数据库;

  2. 添加分页和打印功能。

最新回复 (2)
  • 光庆 5月前
    0 2
    你这不是具体的问题啊
  • 小肥羊 5月前
    0 3
    这是个vip问题了,哈哈。这是个需求
返回
发新帖
作者最近主题: