Opening and Closing a Database

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Fundamentals > Working with the Database >

Opening and Closing a Database

Most of the time, you will only be dealing with one database and one database handle. Eventually you may come across a situation where you need to access the information from two or more databases. Using the built-in function, opendb, you can open up to 16 databases.

The syntax for this function is as follows:

opendb(text databasePath);

This function returns a handle to the newly opened database. The following example opens a database whose path is "c:\temp\support.dcb".

main()

{
 int db, db2;

 db2 = opendb("c:\temp\support.dcb");

}

If you had a database open prior to running this CPL, the variable, db, would hold a handle to the current database. After running the opendb function, the variable, db2, would hold the handle to the newly opened database. You now have access to two databases.

You can write a script that accesses both databases. for example, suppose that record 42 in database 2 (db2) has a special page count in the PAGES field. You want to populate this value into the page count field (also called PAGES) in your currently open database (db). To start, you will need to access the first database:

main()

{
 int db, db2;

 db2 = opendb("c:\temp\support.dcb");

 goto(db2, 42);

}

Second, you would need to contain the page count:

main()

{
 int db, db2, pageCount;

 db2 = opendb("c:\temp\support.dcb");

 goto(db2, 42);

 pageCount = db2->PAGES;

}

Now, you can cycle through the original database and store the value  stored in the pageCount variable into PAGES field of all the records in the current query.

main()

{
 int db, db2, pageCount;

 db2 = opendb("c:\temp\support.dcb");

 goto(db2, 42);

 pageCount = db2->PAGES;

 cycle(db)

 {

         db->PAGES = pageCount;

 }

}

Notice that even though the field name, PAGES, occurs in both database, we differentiate the two by using the individual database handles.

You can also add in one more line to close the database we opened. Don’t worry if you forget this step. After a CPL finishes, Concordance makes sure to close any databases that you opened in the course of running the program.

main()

{
 int db, db2, pageCount;

 db2 = opendb("c:\temp\support.dcb");

 goto(db2, 42);

 pageCount = db2->PAGES;

 cycle(db)

 {

         db->PAGES = pageCount;

 }

 close(db2);

}