Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » UltraLite - M-Business Anywhere 编程 » 教程:M-Business Anywhere 示例应用程序

 

第 5 课:添加数据操作和导航

本课介绍了如何用数据操作和导航逻辑填充应用程序。

 ♦  初始化表
  1. 通过将以下代码添加到 tutorial.js 中的 Connect 函数的末尾来初始化数据库中表示 Customer 表的 CustomerTable:

    try {
      CustomerTable = Connection.getTable( "customer", null );
      CustomerTable.open();
    } catch( ex3 ) {
      alert("Error: " + ex3.getMessage() );
    }
  2. 添加变量以在数据库和 Web 窗体之间移动数据。

    为客户数据将以下变量声明添加到 tutorial.js 的顶端:

    var GivenName = "";
    var Surname = "";
    var Street = "";
    var City = "";
    var Phone = "";
    var ID = "";
    
  3. 创建函数以读取和显示客户数据。

    将以下函数添加到 tutorial.js。此函数读取客户数据的当前行,还确保 NULL 列显示为空字符串:



    function Fetch() 
    {
        if( CustomerTable.getRowCount() == 0 ) {
     GivenName = "";
     Surname = "";
     Street = "";
     City = "";
     Phone = "";
     ID = "";
     return;
        } 
        ID = CustomerTable.getString( CustomerTable.schema.getColumnID( "ID" ) );
        GivenName = CustomerTable.getString( CustomerTable.schema.getColumnID( "GivenName" ) );
        Surname = CustomerTable.getString( CustomerTable.schema.getColumnID( "Surname" ) );
        Street = CustomerTable.getString( CustomerTable.schema.getColumnID( "Street" ) );
        if( CustomerTable.isNull( CustomerTable.schema.getColumnID( "City" ) ) ) {
     City = "";
        } else {
     City = CustomerTable.getString( CustomerTable.schema.getColumnID( "City" ) );
        }
        if( CustomerTable.isNull( CustomerTable.schema.getColumnID( "Phone" ) ) ) {
     Phone = "";
        } else {
     Phone = CustomerTable.getString( CustomerTable.schema.getColumnID( "Phone" ) );
        }
    }
    

    将以下内容添加到 main.htm 中紧靠 <script> 标记之后。DisplayRow 从数据库获取值,然后在 Web 窗体中显示这些值。FetchForm 获取 Web 窗体中的当前值,然后提供给数据库代码使用。



    <script>
    function DisplayRow() {
        Fetch();
        document.form.ID.value = ID;
        document.form.GivenName.value = GivenName;
        document.form.Surname.value = Surname;
        document.form.Street.value = Street;
        document.form.City.value = City;
        document.form.Phone.value = Phone;
    }
    
    function FetchForm() {
        GivenName = document.form.GivenName.value;
        Surname = document.form.Surname.value;
        Street = document.form.Street.value;
        City = document.form.City.value;
        Phone = document.form.Phone.value;
    }
    </script>
  4. 当应用程序已装载时,调用 DisplayRow 以显示当前行。按如下方法修改 main.htm 顶端的 body 标记:

    <body onload="Connect(); DisplayRow();">

虽然直到此时教程数据库中都没有任何数据,但这是同步通道以确保应用程序可以正常运行的好时机。

 ♦  将代码添加到插入行
  • 创建函数以插入客户数据。

    在以下过程中,调用 InsertBegin 可以将应用程序置于插入模式下,并将当前行中的所有值设为其缺省值。例如,ID 列收到下一个自动增量值。设置列值,然后插入新行。

    将以下函数添加到 tutorial.js



    function Insert()
    {
        try {
     CustomerTable.insertBegin();
     CustomerTable.setString( CustomerTable.schema.getColumnID( "GivenName" ), GivenName );
     CustomerTable.setString( CustomerTable.schema.getColumnID( "Surname" ), Surname );
     CustomerTable.setString( CustomerTable.schema.getColumnID( "Street" ), Street );
     if( City.length > 0 ) {
         CustomerTable.setString( CustomerTable.schema.getColumnID( "City" ), City );
     }
     if( Phone.length > 0 ) {
         CustomerTable.setString( CustomerTable.schema.getColumnID( "Phone" ), Phone );
     }
     CustomerTable.insert();
     CustomerTable.moveLast();
        } catch( ex ) {
     alert( "Error: cannot insert row: " + ex.getMessage() );
        }
    }

    将以下函数添加到 main.htm

    function ClickInsert()
    {
        FetchForm();
        Insert();
        DisplayRow();
    }