在FoxPro中使用多个表

时间:2014-01-27 21:26:07

标签: sql visual-foxpro foxpro

我的IT课程有一件棘手的事情。

Two Tables: 
Classes |Class C-3, Day N-1, Lessons C-7|
Lessons |Lesson C-1, Lesson_Name C-10|

First table:
IX - 1 - MEP    
X - 2 - MBE
XI - 3 - FCM

Second table:
M - Maths
E - English
B - Biology
F - French
P - Physics
C - Chemistry

在第一个表格中,课程列是课程名称的第一个字符(例如M - 数学等等)

在第二个表中是从第一个表中输入的课程。 (课程 - 课程名称ex.M - 数学)我需要从键盘输入一个类(使用输入或获取/读取)并写出该类的课程名称。

我希望你能理解我在这里想说的话。

我尝试从工作区中第一个表的类和课程列中创建2个单独的数组,然后使用另一个工作区中的第二个表来写出课程名称,但我无法让它工作。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

这是一个快速的东西供你咀嚼。 我为你创建了这个表,并对你的样本数据进行了插入,即使表结构你会及时了解它。

至于为表创建列名,请尽量不要使用保留字或甚至可能导致混乱的函数......例如在这种情况下“Class”,“Day”。编码时,您会看到保留字通常以蓝色语法颜色显示。如果您在创建表时已经有了,请再想一想该列的更好名称。

至于VFP列名。如果表不是“数据库容器”的一部分,则列名限制只有10个字符(因此我将“Lesson_Name”更改为“LessonName”)。

我正在使用简单的SQL查询来做这个示例。您在VFP与其他SQL数据库中查看的查询差异相对较小。最重要的一点是,在VFP中,分号表示命令CONTINUES在下一行。在SQL中,分号通常表示命令的结束。

*/ Sample... create the tables and insert the values
SET SAFETY OFF
CLOSE TABLES ALL 
CREATE TABLE Classes;
    (    Class C(3),;
        Day N(1),;
        Lessons C(7) )

INSERT INTO Classes ( Class, Day, Lessons );
    VALUES ( "IX", 1, "MEP" )

INSERT INTO Classes ( Class, Day, Lessons );
    VALUES ( "X", 2, "MBE" )

INSERT INTO Classes ( Class, Day, Lessons );
    VALUES ( "XI", 3, "FCM" )

*/ Create the next table and insert their records        
CREATE TABLE Lessons ;
    (   Lesson C(1),;
        LessonName C(10) )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "M", "Math" )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "E", "English" )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "B", "Biology" )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "F", "French" )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "P", "Physics" )

INSERT INTO Lessons ( Lesson, LessonName );
    VALUES ( "C", "Chemistry" )


*/ Prepare a look and keep asking until a user hits the escape key (ASCII 27)
DO WHILE LASTKEY() <> 27
    */ prepare a value for data entry of the class
    SomeClass = "   "
    @ 1,1 say "Enter a value for a class (escape or leave blank to exit):" get SomeClass picture "!!!" 
    READ 
    IF EMPTY( SomeClass )
        EXIT 
    ENDIF 

    */ Query the classes table for the value entered
    USE IN SELECT( "FoundClass" )
    SELECT Class, Day, Lessons;
       FROM Classes;
       WHERE Class = SomeClass;
       INTO CURSOR FoundClass READWRITE 

    */ Did we find the class?   
    IF RECCOUNT( "FoundClass" ) = 0
       MESSAGEBOX( "Sorry, class was not found" )
    ELSE 
       */ Now, get all lessons associated with the class.
       USE IN SELECT( "FoundLessons" )
       SELECT Lesson, LessonName;
          FROM Lessons;
          WHERE Lesson $ FoundClass.Lessons;
          INTO CURSOR FoundLessons READWRITE 

        IF RECCOUNT( "FoundLessons" ) = 0
            MESSAGEBOX( "Sorry, no lessons found for the class" )
        ELSE 
            CLEAR
            ?
            ?
            ?
            ? "Class: " + FoundClass.Class, ;
              "  Day: ", FoundClass.Day, ;
              " Lessons: " + FoundClass.Lessons
            ? "Lessons:"  
            SELECT FoundLessons
            SCAN
               ? FoundLessons.LessonName
            ENDSCAN 
        ENDIF 
    ENDIF 

    USE IN SELECT( "FoundClass" )
    USE IN SELECT( "FoundLessons" )
ENDDO
CLEAR