Tuesday, April 07, 2009

Embedded SQL

Embedded SQL/Oracle Tutorial

Cursors

By now, if you have been following the tutorials closely, you should be quite familiar with inserting, updating, and deleting database records. The next step is to create querying functions (i.e., to handle SELECT operations).  We have intentionally left querying until last because there often are more steps to perform. Unlike the format of queries we typed into SQL*Plus, embedded SQL requires the use of cursors to successfully output the results of the query.

Cursors were invented to satisfy both the SQL and host programming languages. SQL queries handle sets of rows at a time, while C++, for example, handles only one row at a time. When we type the following SQL query into SQL*Plus:
 

SQL> select    driver_sin, count(exam_score)
   2 from      exam
   3 where     exam_type = 'L'
   4 group by  driver_sin;


we get the following output:

DRIVER_SIN COUNT(EXAM_SCORE)
---------- -----------------
 111111111                 1
 222222222                 2
 333333333                 3
 444444444                 1
In our embedded SQL code, we cannot simply specify:
 
EXEC SQL SELECT    driver_sin, count(exam_score)
         FROM      exam
         WHERE     exam_type = 'L'
         GROUP BY  driver_sin;


and expect C++ to output the results of the query. We have to fetch the results of this query into a cursor, and then output the results one at a time using C.

To use a cursor in embedded SQL, we must first declare it. We do this by using the DECLARE keyword, with the following syntax:
 

EXEC SQL DECLARE <cursor name> CURSOR FOR
SELECT ... FROM ...;


where the SELECT part of the statement specifies the query. Note that the above statement is only a declaration and the SELECT itself has not been executed yet. The declaration must occur before it is used. The scope of a cursor is the entire Pro*C++ program, but cursor statements (DECLARE, OPEN, FETCH, and CLOSE) must occur within the same precompiled unit. Therefore, for the entire program, each <cursor name> must be unique.

Once a cursor is declared, we have to open it in order to execute the query. To do this, we use the OPEN keyword, as follows:


EXEC SQL OPEN <cursor name>;


When we first open a cursor, it points to just before the first row (of the result). To retrieve rows (one at a time) which satisfy the SELECT query, we need to use the FETCH keyword. The syntax of the FETCH statement is:
 

EXEC SQL FETCH <cursor name> INTO :hostvar1, :hostvar2, ...;


Note that we have to first declare and open the cursor with cursor name before being able to use it in a FETCH statement.

After executing the FETCH statement, the cursor is set to point to the beginning of the next row of the answer set. When all rows have been fetched, sqlcode is set to 100 or 1403. Acknowledging this, we can write simple while loops which continuously fetch and print out tuple values for each row by testing sqlcode for the values 100 and 1403. You will see this in the example given below.

After all rows have been fetched, you can close the cursor with the command:
 

EXEC SQL CLOSE <cursor name>


A cursor can always be reused, so if you want to reuse your cursor, all you have to do is reopen it. The FETCH statement only moves forward in tables, so you might want to reopen a cursor to revisit and fetch previous rows in a table.
 
 

Sample Program

You should know enough about cursors by now to complete any homework involving embedded SQL.  Here is the Pro*C++ source code for maintaining the branch relation. In particular, note the subroutine called Show_Branch() which shows information for all branches.
#include <iostream.h>
#include <stdlib.h>                       // needed for atoi()
#include <stdio.h>                        // needed for gets()
#include <string.h>
#include <unistd.h>                       // needed for getpassphrase()
#include <iomanip.h>                      // needed for setw()
#define MAXBUF 50                         // maximum length of buffer
char line[MAXBUF];                        // buffer to hold stdin
EXEC SQL INCLUDE sqlca;                   // declarations for error checking
EXEC SQL WHENEVER SQLERROR    DO  print_error();
EXEC SQL WHENEVER SQLWARNING  DO  print_warning();
EXEC SQL WHENEVER NOTFOUND    DO  print_not_found();
void print_error()
{
  // display the error message returned by Oracle
  cout << "\n!! Unsuccessful operation.  Error code: " << sqlca.sqlcode;
  cout << "\n   Oracle Message: " << sqlca.sqlerrm.sqlerrmc << "\n";
}
void print_warning()
{
  // display the warning message returned by Oracle
  cout << "\n!! A warning occurred.   Error code: " << sqlca.sqlcode;
  cout << "\n   Oracle Message: " << sqlca.sqlerrm.sqlerrmc << "\n";
}
void print_not_found()
{
  // display the "row not found" message returned by Oracle
  cout << "\n!! Warning.  Row not found.  Error code: " << sqlca.sqlcode;
  cout << "\n   Oracle Message: " << sqlca.sqlerrm.sqlerrmc << "\n";
}
void Connect()
{
    // connect to database
    EXEC SQL BEGIN DECLARE SECTION;
        char userid[64];
        char password[64];
        char *DBname = "@ug";
    EXEC SQL END DECLARE SECTION;
    cout << "\nUsername: ";
    gets(userid);
    strcat(userid, DBname);
    strcpy(password, getpassphrase("Password: "));
    EXEC SQL CONNECT :userid IDENTIFIED BY :password;
}
void Insert_Branch()
{
  // Insert a tuple into the branch relation
  EXEC SQL BEGIN DECLARE SECTION;
    int        bid;
    VARCHAR    bname[20];
    VARCHAR    baddr[50];
    VARCHAR    bcity[20];
    int        bphone;
    short int  baddr_ind;
    short int  bphone_ind;
  EXEC SQL END DECLARE SECTION;
   cout << "\nBranch ID: ";
  gets(line);
  bid = atoi(line);
  cout << "\nBranch Name: ";
  gets(line);
  bname.len = strlen(line);
  strncpy((char *) bname.arr, line, bname.len);
  cout << "\nBranch Address: ";
  gets(line);
  baddr.len = strlen(line);
  strncpy((char *) baddr.arr, line, baddr.len);
  cout << "\nBranch City: ";
  gets(line);
  bcity.len = strlen(line);
  strncpy((char *) bcity.arr, line, bcity.len);
  cout << "\nBranch Phone: ";
  gets(line);
  if (strlen(line) != 0)
     bphone = atoi(line);         // phone number is not null
  else
     bphone_ind = -1;             // phone number is null;  set indicator
  EXEC SQL INSERT
           INTO    branch (branch_id, branch_name, branch_addr, branch_city,
                           branch_phone)
           VALUES (:bid, :bname, :baddr:baddr_ind, :bcity, :bphone:bphone_ind);
  //  The WHENEVER statement will handle the error processing, but
  //  to show the sequence of error messages, let's add the following.
  if (sqlca.sqlcode < 0)
     cout << "An error was detected.  The details are described above.\n";
  EXEC SQL COMMIT WORK;
}
void Delete_Branch()
{
  // Delete a tuple from the branch relation, given the branch id
  EXEC SQL BEGIN DECLARE SECTION;
    int  bid;
  EXEC SQL END DECLARE SECTION;
  cout << "Branch ID: ";
  gets(line);
  bid = atoi(line);
  EXEC SQL DELETE
           FROM   branch
           WHERE  branch_id = :bid;
  EXEC SQL COMMIT WORK;
}
 void Update_Branch()
{
  // Update the branch name, given the branch id
  EXEC SQL BEGIN DECLARE SECTION;
    int      bid;
    VARCHAR  bname[20];
  EXEC SQL END DECLARE SECTION;
   cout << "Branch ID: ";
  gets(line);
  bid = atoi(line);
  cout << "New Branch Name: ";
  gets(line);
  bname.len = strlen(line);
  strncpy((char *) bname.arr, line, bname.len);
  EXEC SQL UPDATE branch
           SET    branch_name = :bname
           WHERE  branch_id = :bid;
  EXEC SQL COMMIT WORK;
}
void Show_Branch()
{
  // Display information about branches
  EXEC SQL BEGIN DECLARE SECTION;
    int        bid;
    VARCHAR    bname[20];
    VARCHAR    baddr[50];
    VARCHAR    bcity[20];
    int        bphone;
    short int  baddr_ind;
    short int  bphone_ind;
  EXEC SQL END DECLARE SECTION;
  EXEC SQL DECLARE branch_info CURSOR FOR
           SELECT * FROM BRANCH;
  EXEC SQL OPEN branch_info;
  EXEC SQL FETCH branch_info
           INTO  :bid, :bname, :baddr:baddr_ind, :bcity, :bphone:bphone_ind;
  cout << setiosflags(ios::left);       // left justify the names to come
  cout << setw(10) << "ID"   << setw(15) << "NAME"  << setw(15) << "ADDRESS"
       << setw(15) << "CITY" << setw(15) << "PHONE" << "\n";
  cout << "--------------------------------------------------------------\n";
  while (sqlca.sqlcode >= 0  &&  sqlca.sqlcode != 100  &&
         sqlca.sqlcode != 1403)
   {
     bname.arr[bname.len] = '\0';       // null terminates the VARCHARs
     baddr.arr[baddr.len] = '\0';
     bcity.arr[bcity.len] = '\0';
     // display results;  keep the columns aligned reasonably well
     cout << setw(10) << bid       << setw(15) << bname.arr
          << setw(15) << baddr.arr << setw(15) << bcity.arr << setw(15);
     if (bphone_ind != -1)              // display phone number, if not null
        cout << bphone;
     else
        cout << " ";
     cout << "\n";
     EXEC SQL FETCH branch_info
              INTO  :bid, :bname, :baddr:baddr_ind, :bcity, :bphone:bphone_ind;
   }
  cout << "The last warning just signifies that the cursor fetched the "
       << "final record\n";
  EXEC SQL CLOSE branch_info;
  EXEC SQL COMMIT WORK;
}
int main()
{
  // simple text interface for above functions
  int  choice, quit;
  Connect();                        // connect to Oracle
  quit = 0;
  while (!quit)
    {
      cout << "\nPlease choose one of the following: \n";
      cout << "1. Insert branch\n";
      cout << "2. Delete branch\n";
      cout << "3. Update branch\n";
      cout << "4. Show   branch\n";
      cout << "5. Quit\n>> ";
      gets(line);
      choice = atoi(line);
      printf("\n\n");
      switch (choice)
        {
          case 1:  Insert_Branch();
                   break;
          case 2:  Delete_Branch();
                   break;
          case 3:  Update_Branch();
                   break;
          case 4:  Show_Branch();
                   break;
          case 5:  quit = 1;
          default: exit(0);
        }
     }
  EXEC SQL COMMIT WORK RELEASE;     // Commit and free any locks held.
  // Any additional non-SQL/non-Oracle work can go here.
}
Compile and run the code. You can now test modifications (insert, update, and delete) to the branch relation without having to start up an SQL*Plus session.

Although our example tested sqlca.sqlcode for the values 100 and 1403 in the Show_Branch() function, we could have used error trapping instead and done something like this:
 

EXEC SQL WHENEVER NOTFOUND DO BREAK;
while(1)  {
. . .
EXEC SQL FETCH branch_info
         INTO  :bid, :bname, :baddr:baddr_ind, :bcity, :bphone:bphone_ind;
. . .
}
/* restore WHENEVER NOTFOUND to what we had before */
EXEC SQL WHENEVER NOTFOUND DO print_not_found();



Embedded SQL/Oracle Tutorial  - Cursors


Friday, April 03, 2009

'Telecom sector can create 25 Million jobs'



By    siliconindia news bureau

New Delhi: Driving attention to the telecom sector, major telecom players have proposed for deployment of mobile broadband financed by private players, which would create 25 million jobs. Global GSM players like AT&T Mobile, Ericsson, Alcatel Lucent, Orascom, MTN Group and Sunil Bharti Mittal of Bharti Airtel have submitted the proposal to PM Manmohan Singh, to endorse it at the G20 meet.

The proposal also highlighted that the move will boost global GDP by around four percent. "The initiative is centered on replicating the success story of GSM and making access to the Internet via mobile broadband devices ubiquitous. The broad economic impact of this is well established in research. In emerging economies, a 10 percent increase in mobile penetration boosts annual GDP growth by 1.2 percent," said the letter.


Highlighting the efficacy of the mobile firms, the companies provide voice and basic data services to over four billion consumers, on networks that cover over six billion. A 10 percent increase in mobile penetration will boost annual GDP growth by 1.2 percent. In return for this, the mobile industry has asked for two key enabling actions from the G20 governments.

Thursday, April 02, 2009

VB Tutorial

Following is an online tutorial:

http://www.vbtutor.net/vbtutor.html

http://www.vbtutor.net/vb2008/vb2008tutor.html

Sample Applications can be downloaded with source code:

http://www.vbtutor.net/VB_Sample/sample.html

http://www.vbtutor.net/VB_Download/vbcodes.htm

http://www.vbtutor.net/VBGuide/vbguide.html

Windows Magical BUGS!!!!!!!!!!



Try out…….. Its too Interesting!!!!!!!!!

 

 

Microsoft fails to solve these problem- Really Interesting

MAGIC #1

An Indian discovered that nobody can create a FOLDER anywhere on the computer  which can be named as "CON". This is something pretty cool...and unbelievable... At Microsoft the whole Team, couldn't answer why this happened!
TRY IT NOW ,IT WILL NOT CREATE " CON " FOLDER


MAGIC #2
For those of you using Windows, do the following:

1.) Open an empty notepad file
2.) Type "Bush hid the facts" (without the quotes)
3.) Save it as whatever you want.
4.) Close it, and re-open it.

is it just a really weird bug? :-??


MAGIC #3
Microsoft crazy facts

This is something pretty cool and neat...and unbelievable... At Microsoft the whole Team, including Bill Gates, couldn't answer why this happened! It was discovered by a Brazilian. Try it out yourself...

Open Microsoft Word and type

=rand (200, 99)

And then press ENTER
then see the magic...............................

-~----------~----~----~----~------~----~------~--~---

Wednesday, April 01, 2009

U.S. private employers cut 742,000 jobs in March By siliconindia news bureau


New York: Downturn-ridden U.S. economy continues to fall further and the number of job losses is getting bigger every month. As per a report by ADP employer Services, private employers in the country have cut a record 742,000 jobs in March over 706,000 job-cuts in February.



According to the analysts, the big drop foreshadows a huge decline in the non-farm payroll reading in the government's employment report that will be released on Friday.

"It's a terrible number. It is almost a loss of three quarters of a million jobs which is possibly the highest we have seen so far over the length of this crisis," said Matt Esteve, foreign exchange trader with Tempus Consulting in Washington.

After the news of the huge job losses in March, U.S. stock futures and the dollar have fallen, while U.S. treasury bonds regained some of their lost ground.

Code here to gain money + recognition

Click the link below:

https://codesniper.forge.funambol.org/?utm_campaign=iCar%2C%20CTIA%2C%20new%20screencam%2C%20free%20whitepaper%20%26%20Storm&utm_medium=Email&utm_source=VerticalResponse&utm_term=Code%20Sniper

SOME USEFUL NEWS - Read and Make Money on your Interest

March/April 2009 | Spring Issue | Click to view this message in a browser

Welcome to the Spring issue of Funambulletin, where we strive to bring you the best, most interesting and entertaining news about mobile open source. Exclusive Funambol Introduces World's First Touchless iCar Borrowing from the iPhone, Funambol is premiering the world's first finger gesture operated vehicle. With no steering wheel, it makes driving a snap. Check out the press release and test drive video a day before anyone else. Funambol Events See Funambol at CTIA Funambol is showcasing its next major version, MobileWe v8, at CTIA, April 1-3. It includes an all new AJAX portal that makes it easy to sync content from billions of mobile phones with the Internet cloud and popular email systems and desktop apps. For more information about our doings at CTIA and to arrange a meeting, see our advisory. Funambol Video Funambol v8 Open Source MobileWe Portal Screencam Not headed to CTIA? See a screencam of the new portal on Youtube, here's the long and short of it. Free White Paper Mobile Sync, the New World Order This new white paper describes the trends that are making cloud sync compelling. It also discusses the requirements for a mobile sync solution and how open source satisfies these requirements. Get the free white paper instantly after website registration. Product Update New Phone Pack Supports BlackBerry Storm Funambol just completed work on its new monthly 'phone pack' that supports the latest mobile devices. It supports the BlackBerry Storm as well as lays the foundation to support other touchscreen devices more quickly. The new phone pack will be made available shortly on the free myFUNAMBOL portal. We will issue a press release in the next few days to signal that the phone pack is installed and ready for action. Community News New Funambol Forge Home Page Our new forge has had an extreme home (page) makeover, check it out. It shows how dynamic our community is and makes it easier to download software and access other content. Our downloads and community traffic have reached record levels since the home page was redesigned a few weeks ago. Thanks to everyone who provided feedback on the new home page and helped make it happen. Open Source Update Write Code, Get Paid We recently suggested several new Code Sniper projects that community developers can work on and get paid and recognized for their contribution. These include a Syncml Graphic Client for Maemo, GNOME Eplugin for Evolution, LinkedIn Contacts Client, Skype PIM Client, Eclipse integration and Netbeans integration. Check out the Code Sniper page for details. Funambol Customers Millions of Users Adopting Funambol There has recently been a surge in customers, ranging from established companies to startups, who have deployed Funambol Carrier Edition. These customers collectively have millions of users and have implemented some highly innovative uses of mobile sync and push email. We will be issuing a press release about them in the next few days as well as blogging and tweeting about them. Thought Leadership Smart Cloud Syncing, the Next Mobile Holy Grail? Check out this interesting OSTATIC article about smart cloud app syncing as the next mobile frontier. Fun Tweet Favorite Tweet of the Month We love reading everyone's tweets, but this one caught our attention: "iPod Touch Sets Kid's Pants on Fire. I feel bad for the kid, but it's an amusing story."

In This Issue...
Funambol Introduces World's First Touchless iCar
See Funambol at CTIA
Funambol v8 Open Source MobileWe Portal screencam
Mobile Sync, the New World Order whitepaper
New Phone Pack supports BlackBerry Storm
New Funambol Forge Home Page
Code Sniper: Write Code, Get Paid
Customer Update: Millions of Users Adopting Funambol
Smart Cloud App Syncing
Favorite Tweet of the Month
Code Sniper Community Program
Funambol offers cash bounties for developers to extend our open source capabilities. Check out the list of projects and become a Code Sniper.
Upcoming Events
CTIA Wireless 2009
April 1-3, 2009, Las Vegas
Funambol is showcasing its new Funambol v8 Open Source MobileWe Portal for billions of phones. Check out this advisory for more information and to arrange a meeting.
Stay in touch
Stay in touch with Funambol following the stream on Twitter.
About Funambulletin
Funambulletin is Funambol's periodic newsletter that contains mobile open source community and industry news, views, tips and happenings. We hope that you find Funambulletin to be informative, useful and entertaining. If you have any suggestions or contributions, please contact Stefano Maffulli, Funambol community manager, at maffulli-at-funambol com.
About Funambol Copyright (C) 2009 Funambol. All rights reserved. Funambol is the leading provider of open source push email and mobile sync worldwide. Funambol open source software has been downloaded three million times by 50,000 developers and project participants in 200 countries. The commercial version of Funambol has been deployed at service providers, mobile operators, portals, device manufacturers and ISVs including customers such as 1&1, Earthlink, AOL and CA, Inc. Funambol is headquartered in Redwood City, CA with an R&D center in Italy. For more information, please visit www.funambol.com.

iCar, CTIA, new screencam, free whitepaper & Storm

If you no longer wish to receive these emails, please reply to this message with "Unsubscribe" in the subject line or simply click on the following link: Unsubscribe


Click here to forward this email to a friend

Funambol Inc
643 Bair Island Road, Suite 305,
Redwood City, California 94063
US

Read the VerticalResponse marketing policy.

Try Email Marketing with VerticalResponse!