//*********************************************************************** // FETCH ROWS FROM THE TABLE "T1"......select * from T1; int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field int f2=20;//f2 field int rettype ; ret = SQLPrepare(stmt,(unsigned char*)"SELECT F3,F2 FROM T1 ",SQL_NTS); rettype = ret; if(rettype!=0)return 1; //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,) ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,NULL); ret = SQLBindCol(stmt,2,SQL_C_SLONG,&f2,0,NULL); int j, count=0; ret = SQLExecute(stmt); SQLSMALLINT noc; ret = SQLNumResultCols(stmt,&noc); rettype=ret; if(rettype != 0 ) { printf("SQLNumResultCol() returns = %d\n",noc); return 1; } while(SQL_SUCCEEDED(ret = SQLFetch(stmt))) { count++; } /*SQLSMALLINT noc; ret =SQLNumResultCols(stmt,&noc); printf("SQLNumResultCols() returns=%d\n",noc);*/ ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row fetched=%d\n",count); return 0; }
//************************************************************************* int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN data source ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); //SERVER=192.168.1.114;PORT=5678 if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source successfully..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 2; } ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); printf("Disconnected from the Datasource\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt,char *tablename) { int ret; char f1[50]= "praba"; char f2[50]= "praba"; char f3[50]= "praba"; char f4[50]= "praba"; short f5; char f6[50]= "praba"; ret = SQLPrimaryKeys(stmt, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)tablename,SQL_NTS); checkrc(ret,__LINE__); short totalFields=0; ret = SQLNumResultCols (stmt, &totalFields); checkrc(ret,__LINE__); printf( "No of columns in resultset = %d\n",totalFields); ret = SQLBindCol(stmt,1,SQL_C_CHAR,f1,sizeof(f1),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_CHAR,f2,sizeof(f2),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_CHAR,f4,sizeof(f4),NULL); checkrc(ret,__LINE__); SQLBindCol(stmt, 5, SQL_C_SHORT, &f5, 0, NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_CHAR,f6,sizeof(f6),NULL); checkrc(ret,__LINE__); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))){ printf("catalogName%s \t SchemaName=%s \t TableName=%s \t ColumnName = %s \t KEY_SEQ=%hd\t Primarykey = %s \n ",f1,f2,f3,f4,f5,f6); } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); return 0; }
//************************************************************************* int InsertTest(SQLHANDLE env,SQLHANDLE dbc,SQLHANDLE stmt) { int ret; int f1=90; // f1 field int f2=20;//f2 field ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(?,?)",SQL_NTS); checkrc(ret,__LINE__); // BIND PARAMETER ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f2,0,NULL); checkrc(ret,__LINE__); int i,count=0; for(i=0;i<10;i++) { f1++; f2++; ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); count++; } printf("Total row inserted=%d\n",count); return 0; }
//************************************************************************* int InsertTest(SQLHANDLE env,SQLHANDLE dbc,SQLHANDLE stmt) { SQLRETURN ret; int f1=0; // f1 field char f2[15]="jitendra";//f2 field SQLINTEGER slen=SQL_NTS; ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(?,?)",SQL_NTS); checkrc(ret,__LINE__); // BIND PARAMETER ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_LONG,SQL_INTEGER,0,0,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)f2,0,&slen); checkrc(ret,__LINE__); int i,count=0; for(i=0;i<1000;i++) { f1++; ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); count++; } printf("Total row inserted=%d\n",count); return 0; }
int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; char f1[50]= "praba"; char f2[50]= "praba"; char f3[50]= "praba"; char f4[50]= "praba"; char f5[50]= "praba"; ret = SQLTables(stmt, (SQLCHAR*)"csql", SQL_NTS, (SQLCHAR*)"csql", SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"Table", SQL_NTS ); checkrc(ret,__LINE__); short totalFields=0; ret = SQLNumResultCols (stmt, &totalFields); checkrc(ret,__LINE__); printf( "No of columns in resultset = %d\n",totalFields); ret = SQLBindCol(stmt,1,SQL_C_CHAR,f1,sizeof(f1),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_CHAR,f2,sizeof(f2),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_CHAR,f4,sizeof(f4),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,5,SQL_C_CHAR,f5,sizeof(f5),NULL); checkrc(ret,__LINE__); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))) { printf("\tF1=%s \t F2=%s \t F3=%s \t F4= %s \t F5=%s \n ",f1,f2,f3,f4,f5); } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); return 0; }
int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt,char *tablename) { int ret; char f2[50]= "praba"; SWORD f1; char f4[50]= "praba"; SWORD f3; int f5; char f6[50]= "praba"; ret = SQLStatistics(stmt, NULL, 0, NULL, SQL_NTS, (SQLCHAR*) tablename, SQL_NTS, SQL_INDEX_ALL, SQL_QUICK); checkrc(ret,__LINE__); short totalFields=0; ret = SQLNumResultCols (stmt, &totalFields); checkrc(ret,__LINE__); printf( "No of columns in resultset = %d\n",totalFields); ret = SQLBindCol(stmt,4,SQL_C_SHORT,(void*)&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_CHAR,f2,sizeof(f2),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,7,SQL_C_SHORT,(void *)&f3,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,9,SQL_C_CHAR,f4,sizeof(f4),NULL); checkrc(ret,__LINE__); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))){ printf(" Unique =%hd \tIndexName = %s \t Type=%hd ColumnName = %s \n ",f1,f2,f3,f4); } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); return 0; }
//************************************************************************* int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=csql;SERVER=localhost;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=csql;SERVER=localhost;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("connection failed\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[100]="CREATE TABLE EMP(EID INT,SALARY INT)"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); //AFTER CLOSE THE CONNECTION ,CALL execute ret = SQLExecute(stmt); int rettype = ret; if(ret ) printf("After closing the connection, Execution failed\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); if(rettype ==0)return 1; return 0; }
int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; SQLCHAR szSchema[STR_LEN]; SQLCHAR szCatalog[STR_LEN]; SQLCHAR szColumnName[STR_LEN]; SQLCHAR szTableName[STR_LEN]; SQLCHAR szTypeName[STR_LEN]; SQLCHAR szRemarks[REM_LEN]; SQLCHAR szColumnDefault[STR_LEN]; SQLCHAR szIsNullable[STR_LEN]; SQLINTEGER ColumnSize; SQLINTEGER BufferLength; SQLINTEGER CharOctetLength; SQLINTEGER OrdinalPosition; SQLSMALLINT DataType; SQLSMALLINT DecimalDigits; SQLSMALLINT NumPrecRadix; SQLSMALLINT Nullable; SQLSMALLINT SQLDataType; SQLSMALLINT DatetimeSubtypeCode; ret = SQLColumns(stmt, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"T1", SQL_NTS, NULL, 0); checkrc(ret,__LINE__); short totalFields = 0 ; ret = SQLNumResultCols (stmt, &totalFields); checkrc(ret,__LINE__); printf( "No of columns in resultset = %d\n",totalFields); SQLBindCol(stmt, 1, SQL_C_CHAR, szCatalog, STR_LEN,NULL); SQLBindCol(stmt, 2, SQL_C_CHAR, szSchema, STR_LEN, NULL); SQLBindCol(stmt, 3, SQL_C_CHAR, szTableName, STR_LEN,NULL); SQLBindCol(stmt, 4, SQL_C_CHAR, szColumnName, STR_LEN, NULL); SQLBindCol(stmt, 5, SQL_C_SSHORT, &DataType, 0, NULL); SQLBindCol(stmt, 6, SQL_C_CHAR, szTypeName, STR_LEN, NULL); SQLBindCol(stmt, 7, SQL_C_SLONG, &ColumnSize, 0, NULL); SQLBindCol(stmt, 8, SQL_C_SLONG, &BufferLength, 0, NULL); SQLBindCol(stmt, 9, SQL_C_SSHORT, &DecimalDigits, 0, NULL); SQLBindCol(stmt, 10, SQL_C_SSHORT, &NumPrecRadix, 0, NULL); SQLBindCol(stmt, 11, SQL_C_SSHORT, &Nullable, 0, NULL); SQLBindCol(stmt, 12, SQL_C_CHAR, szRemarks, REM_LEN, NULL); SQLBindCol(stmt, 13, SQL_C_CHAR, szColumnDefault, STR_LEN, NULL); SQLBindCol(stmt, 14, SQL_C_SSHORT, &SQLDataType, 0, NULL); SQLBindCol(stmt, 15, SQL_C_SSHORT, &DatetimeSubtypeCode, 0,NULL); SQLBindCol(stmt, 16, SQL_C_SLONG, &CharOctetLength, 0, NULL); SQLBindCol(stmt, 17, SQL_C_SLONG, &OrdinalPosition, 0, NULL); SQLBindCol(stmt, 18, SQL_C_CHAR, szIsNullable, STR_LEN, NULL); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))) { printf("szCatalog =%s \t szSchema=%s \t szTableName=%s \t szColumnName= %s \t DataType = %d ColumnSize = %d Nullable =%d szRemarks = %s, OrdinalPosition =%d szIsNullable =%s\n ",szCatalog,szSchema,szTableName,szColumnName,DataType,ColumnSize,Nullable,szRemarks,OrdinalPosition,szIsNullable); } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); return 0; }
//************************************************************** // Main <smile> All sub-programs/functions should be called // from here. Note: they should also be listed in lciheader.h //************************************************************** int main(int argc, char *argv[]) { int retval=0; // a return value indicator int verbose=0; // for verbosity // note: switches: //lci -a = advanced help //lci -d = perform a diff of the md5output and old md5output //lci -v = verbose, verbose = 1 //lci = slightly verbose, verbose = 0, // the module name is printed as it performes each module //lci -s = silent, verbose = -1, nothing printed at all. //lci -r = run checkrpm module. //lci -m = force a distribution mode, e.g. lci -m redhat //lci -h = give help output //lci -o outfile = put output in outfile instead of lci.out //lci -w = print output in html format //lci -x module = exclude module from checks int diff=0; // do we run the md5 diff? int rpmmodule=0; // do we run the rpm module? int inetexists=0; // if inetd.conf exists, this is 1 int distribution=1; // what distro are we on? // -1=user specified a distro, its in man_distro // 1=redhat // 2=debian // 3=solaris // 4=gentoo // 5=macosx // 6=slackware // default = redhat int html=0; // html output? // html = 0: normal output //html = 1: user wants html output char release[50]; // array for release level char kernel[50]; // what kernel user is running static char *man_distro; // if the user specifies a distribution const char * header =NULL; // to print out the header static char *out_file = "lci.out"; // output filename var char xlist[100]; // modules to exclude int xarray[33] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int somethinginxlist = 0; // note if no filename given, default = lci.out FILE *tempptr; // a temp file pointer uid_t uidme; uidme = getuid(); // we used to set the umask of the user here... // note that does not work all of the time. // instead use open with options below. Also, // warn user at bottom about setting umask to // sensible value before modifying files. // we will again try to set umask here... mode_t old_umask; old_umask=(umask(0177)); // Now do options processing int i; for (i=1; i < argc; i++) { if(argv[i][0] == '-') { switch (argv[i][1]) { case 'a': usage(); break; case 'h': usage(); break; case 'd': diff = 1; break; case 'm': strcpy(man_distro, argv[i]+3); break; case 'o': strcpy(out_file, argv[i]+3); break; case 'r': rpmmodule = 1; break; case 's': verbose = -1; break; case 'v': verbose = 1; break; case 'w': html = 1; out_file="lci.html"; break; case 'x': strcpy(xlist,argv[i]+3); somethinginxlist = 1; break; default : usage(); break; } } } if (!out_file) // if no filename after -o { usage(); // note usage() will exit lci } // if an exclude list was given, parse the file and // fill the array of excludes.. if (somethinginxlist == 1) { if (findexclude(xlist, xarray) != 0) { // something went wrong perror("Error reading exclude list...\n"); return(-1); } } // check if output exists, if it does, make a // backup and rm the orig. named one. tempptr = fopen(out_file, "r"); if (tempptr != 0) { // output file exists // make a backup copy char *newname = NULL; if ((newname = (char *) calloc(2, sizeof(out_file))) == NULL) { perror("Can not allocate temp memory in lcimain.\n"); perror("Strange. Bailing...\n"); exit(-1); } fclose(tempptr); // add .old onto the old outfile name sprintf(newname, "%s.old", out_file); // gnu, but should be ok... // whoops, we are now failure checking this... retval = 0; retval = rename(out_file, newname); if (retval < 0) { // damn, rename bombed perror("GNU rename bombed in lcimain.\n"); perror("Very odd, you should have seen errors, report them :) \n"); exit(-1); } // free up calloc'd mem free(newname); } // now that we have a backup, remove the outfile // don't care about failure here... remove(out_file); // not seem to work 100% of the time on my system // for making files w/chmod 0600... // reverting to open with options... retval = 0; if ((retval = open(out_file, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) { perror("Could not make file w/perms 0600...\n"); perror("Possible link attack while creating/opening file!\n"); perror("Bailing in lcimain...\n"); exit(-1); } // we print this out no matter what verbose is printf("Starting LCI...\n"); // remove old tempfiles (if lci got killed) // if this fails, we should stop _now_! if (rm() < 0) { perror("Could not remove tempfiles during startup.\n"); perror("You may need root permissions to do this.\n"); return(-1); } // read that in, check kernel version and "release" if (verbose >= 0) { printf("Getting system information...\n"); } retval = versions(release, kernel, verbose); if (retval < 0) { // something went wrong... perror("Problem in versions function.\n"); return(0); } // note about Mandrake: until I can find vast differences // between RedHat/Mandrake in terms of configs, we assume // that it is redhat and don't check /etc/mandrake_rel... // simple check to see if we are on RedHat or Debian // first, did the user specify a distro? if (distribution != -1) { if ((system("cat /etc/redhat-release 1>/dev/null 2>/dev/null >/dev/null")) == 0) { // we are on redhat distribution = 1; } if ((system("cat /etc/debian_version 1>/dev/null 2>/dev/null >/dev/null")) == 0) { // we are on debian distribution = 2; } if ((system("uname |grep SunOS 1>/dev/null 2>/dev/null >/dev/null")) == 0) { // we are on Solaris/SunOS distribution = 3; } if ((system("uname -a |grep gentoo 1>/dev/null 2>/dev/null >/dev/null")) == 0) { // we are on gentoo distribution = 4; } if ((system("uname |grep Darwin 1>/dev/null 2>/dev/null >/dev/null")) == 0) { // we are on Mac OSX distribution = 5; } if ((system("cat /etc/slackware-version 1>/dev/null 2>/dev/null >/dev/null")) == 0) // JTO { // we are on slack distribution = 6; } } if (distribution == -1) { // user specified a distribution if ((strcmp(man_distro, "redhat")) == 0) { // redhat distribution = 1; } if ((strcmp(man_distro, "centos")) == 0) { // CentOS, RHEL-alike distribution = 1; } if ((strcmp(man_distro, "caos")) == 0) { // CaOS, RHL-alike distribution = 1; } if ((strcmp(man_distro, "fedora")) == 0) { // Post-RHL distribution = 1; } if ((strcmp(man_distro, "debian")) == 0) { // debian distribution = 2; } if ((strcmp(man_distro, "solaris")) == 0) { // solaris distribution = 3; } if ((strcmp(man_distro, "mandrake")) == 0) { // mandrake, same as redhat, sortof distribution = 1; } if ((strcmp(man_distro, "gentoo")) == 0) { // gentoo distribution = 4; } if ((strcmp(man_distro, "macosx")) == 0) { // Mac OSX distribution = 5; } if ((strcmp(man_distro, "slackware")) == 0) // JTO { // slackware- JTO distribution = 6; } // default is redhat else { distribution = 1; } } // end if (distribution == -1) // ********* checks are below *****************// // ok, we should have enough info, run the checks if (verbose >= 0) { printf("Running modules...\n"); } // we need to make a header if html = 1, as this // could confuse the issue by putting it in the // first module... call dostuff here... if (html == 1) { html = 2; // this tells dostuff to write out header html header = "\n"; // can't be NULL if ((dostuff(0, out_file, 0, header, html)) < 0) { // something went wrong perror(" Creation of list failed."); return (-1); } html = 1; // change html int back to one } // run checkpkgs // did user put it in exclude list? if (xarray[0] == 0 ) { retval = 0; retval = checkpkgs(distribution, out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkpkgs module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // run checkrpm if rpmmodule = 1 and distribution =1 // did user put it in exclude list? if (xarray[1] == 0) { retval = 0; if ((rpmmodule == 1) && (distribution == 1)) { retval = checkrpm(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkrpm module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // run checkinetd // did user put it in exclude list? if (xarray[2] == 0) { // if hosts file exists run checkhostsfiles inetexists = checkinetd(out_file,distribution, verbose, html); // if inetexists == 1, it does! // if inetexists == 0, it does not... // if inetexists == -1, we had some problems. if (inetexists == 1) { checkhostsfiles(out_file, verbose, html); } if (inetexists == 0) { // do nothing... ; } if (inetexists == -1) { // there was a problem. User should have seen // messages from checkinetd program. // do nothing here and continue... // module bombed, clean up rm(); } } // check inittab // did user put it in exclude list? if (xarray[3] == 0) { retval = 0; retval = checkinittab(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkinittab module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check logging // did user put it in exclude list? if (xarray[4] == 0) { retval = 0; retval = checklogging(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checklogging module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check for setuid/setgid files // did user put it in exclude list? if (xarray[5] == 0) { retval = 0; retval = checkset(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkset module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check for world writable files/directories // did user put it in exclude list? if (xarray[6] == 0) { retval = 0; retval = checkwrite(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkwrite module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check for .exrc and .forward files // did user put it in exclude list? if (xarray[7] == 0) { retval = 0; retval = checkdotfiles(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkdotfiles module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check /etc/passwd for uid 0 other than // root and users not needed on the sys. // did user put it in exclude list? if (xarray[8] == 0) { retval = 0; retval = checkpasswd(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkpasswd module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // check to see if sticky bits are set // on /tmp & /var/tmp. I will add more // to this later on... // did user put it in exclude list? if (xarray[9] == 0) { retval = 0; retval = checkfiles(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkfiles module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[10] == 0) { retval = 0; retval = checkumask(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in chumask module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[11] == 0) { retval = 0; retval = checkftpusers(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkftpusers module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[12] == 0) { retval = 0; retval = checkrc(release, kernel, distribution, out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkrc module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[13] == 0) { retval = 0; retval = checkkbd(release, out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkkbd module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[14] == 0) { if (distribution != 6) // need to fix for slack, JTO { retval = 0; retval = checklimits(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checklimits module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // end if its not slack if statement } // did user put it in exclude list? if (xarray[15] == 0) { retval = 0; retval = checkssh(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checkssh module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[16] == 0) { retval = 0; retval = checkopenfiles(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checkopenfiles module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[17] == 0) { retval = 0; retval = checkissue(out_file, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checksissue module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[18] == 0) { retval = 0; retval = checkwww(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checkwww module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[19] == 0) { retval = 0; retval = checkmd5(out_file, distribution, verbose, html, diff); if (retval < 0) { // something went wrong.. perror("Problem in checkmd5 module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[20] == 0) { if ((distribution != 3) && (distribution !=5)) // we are not on Solaris, so we can run this module { retval = 0; retval = checkmodules(out_file, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checkmodules module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } } // did user put it in exclude list? if (xarray[21] == 0) { retval = 0; retval = checksecuretty(out_file, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checksecuretty module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[22] == 0) { retval = 0; retval = checkrcperms(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkrcperms module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[23] == 0) { retval = 0; retval = checknet(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checknet module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[24] == 0) { retval = 0; retval = checknetforward(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checknetforward module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } // did user put it in exclude list? if (xarray[25] == 0) { if ((distribution !=3) && (distribution != 5)) { // we are on linux... retval = 0; retval = checknetp(kernel, out_file, distribution, verbose, html); if (retval < 0) { perror("Problem in checknetpromisc module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } } // did user put it in exclude list? if (xarray[26] == 0) { if (distribution == 1) // redhat (or derived) specific { retval = 0; retval = checkcfg(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkcfg module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // did user put it in exclude list? if (xarray[27] == 0) { if ((distribution !=3) && (distribution != 5)) // we are on linux { retval = 0; retval = checkbpass(distribution, out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkbpass module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // did user put it in exclude list? if (xarray[28] == 0) { if ((distribution != 3) && (distribution != 5)) // we are on linux { retval = 0; retval = checkipv4(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkipv4 module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // did user put it in exclude list? if (xarray[29] == 0) { if ((distribution !=3) && (distribution !=5)) // we are on linux { retval = 0; retval = checkx(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkx module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // did user put it in exclude list? if (xarray[30] == 0) { if ((distribution !=3) && (distribution !=5)) // we are on linux { retval = 0; retval = checkftp(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkftp module.\n"); perror("You should have seen errors...\n"); // module bombed, clean up rm(); return(0); } } } // did user put it in exclude list? if (xarray[31] == 0) { retval = 0; retval = checklistening(out_file, distribution, verbose, html); if (retval < 0) { // something went wrong.. perror("Problem in checklistening module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // did user put it in exclude list? if (xarray[32] == 0) { retval = 0; retval = checkdisk(out_file, verbose, html); if (retval < 0) { // something went wrong... perror("Problem in checkdisk module.\n"); perror("You should have seen errors...\n"); rm(); return(0); } } // this is to put a footer html at the end of the output // file if the user wanted html output. if (html == 1) { html = 3; // tells dostuff to do an html footer header = "\n"; // can't be NULL if ((dostuff(0, out_file, 0, header, html)) < 0) { // something went wrong perror(" Creation of list failed."); return (-1); } html = 1; // set html int back... habit } else { // print a general footer header = "\n"; if ((dostuff(0, out_file, 0, header, 0)) < 0) { // something went wrong perror(" Creation of footer failed."); return(-1); } } // tell the user we are finished and where the output is // we print this out no matter what verbose is printf("Finished.\n"); printf("Check %s for details.\n", out_file); // in silent mode we don't say much if (verbose >= 0) { printf("Don't forget to check your umask or file perms\n"); printf("when modifying files on the system.\n"); } // set the umask back... umask(old_umask); return(0); }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } //****************************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 char(30))"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("\nTABLE CREATED\n"); //**************************************************************** // INSERT FUNCTION InsertTest(env,dbc,stmt); //***************************************************************** // DELETE FROM T1; printf("delete from T1 where F1=10\n"); SQLCHAR delete1[50]="DELETE FROM T1 WHERE F1=10;"; ret = SQLPrepare(stmt,delete1,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLINTEGER nor; ret = SQLRowCount(stmt,&nor); printf("sqlrowcount() returns=%d\n",nor); // nor should return 1. if(nor!=1)return 1; ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS); checkrc(ret,__LINE__); //************************************************ printf("\ndelete from T1 where F1 > 900\n"); ret = SQLPrepare(stmt,(unsigned char*)"DELETE FROM T1 WHERE F1 > 900",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLINTEGER nor1; ret = SQLRowCount(stmt,&nor1); printf("sqlrowcount() returns=%d\n",nor1); if(nor1!=100)return 1; ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS); checkrc(ret,__LINE__); //************************************************************* printf("\ninsert into T1 values(1001,'1000')\n"); ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(1001,'1000')",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLINTEGER nor2; ret = SQLRowCount(stmt,&nor2); checkrc(ret,__LINE__); printf("sqlrowcount() returns=%d\n",nor2); if(nor2!=1)return 1; ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS); //***************************************************** printf("\nUPDATE T1 SET F2='THOUSAND' WHERE F1<10\n"); ret = SQLPrepare(stmt,(unsigned char*)"UPDATE T1 SET F2='THOUSAND' WHERE F1<10",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLINTEGER nor3; ret = SQLRowCount(stmt,&nor3); checkrc(ret,__LINE__); printf("sqlrowcount() returns=%d\n",nor3); if(nor3!=9)return 1; /*ret = SQLFreeStmt(stmt,SQL_RESET_PARAMS); checkrc(ret,__LINE__);*/ //*********************************************** //FetchTest(env,dbc,stmt); //**************************************************************** //drop the table ret = SQLPrepare(stmt,(unsigned char*)"drop table T1;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table 'T1' dropped\n"); ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } //****************************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT)"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("\nTABLE CREATED\n"); //***************************** InsertTest(env,dbc,stmt); FetchTest(env,dbc,stmt); //**************************************************************** SQLCHAR drop[100]="DROP TABLE T1"; ret = SQLPrepare(stmt,drop,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table 'T1' dropped successfully\n"); ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
//*********************************************************************** // FETCH ROWS FROM THE TABLE "T1"......select * from T1; int DeleteTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field short int f2=20;//f2 field char f3[50]= "jitendra"; float f4 = 2.5; float f5 = 10.50; int f9 = 5; long long f10 = 15000; int result; SQLINTEGER slen=SQL_NTS; char strDate[20]; char strTime[20]; char strTimestamp[30]; SQL_DATE_STRUCT date; date.year=2008; date.month=03; date.day=18; //********************************************** SQL_TIME_STRUCT time; time.hour = 5; time.minute = 22; time.second = 10; //********************************************* SQL_TIMESTAMP_STRUCT timestamp; timestamp.year = 2008; timestamp.month = 03; timestamp.day = 18; timestamp.hour = 5; timestamp.minute = 22; timestamp.second = 10; timestamp.fraction = 764576; int f1temp,f2temp; char f3temp[20]; float f4temp, f5temp; SQL_DATE_STRUCT f6Date; SQL_TIME_STRUCT f7Time; SQL_TIMESTAMP_STRUCT f8Timestamp; int f9temp; long long f10temp; ret = SQLPrepare(stmt,(unsigned char*)"DELETE FROM T1 WHERE F1=? AND F2=? AND F3=? AND F4=? AND F5=? AND F6=? AND F7=? AND F8=? AND F9=? AND F10=?",SQL_NTS); checkrc(ret,__LINE__); //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,) ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&f2temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)f3temp,0,&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,4,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_FLOAT,0,0,&f4temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,5,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_REAL,0,0,&f5temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,6,SQL_PARAM_INPUT,SQL_C_TYPE_DATE,SQL_TYPE_DATE,196,0,&f6Date,sizeof(f6Date),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,7,SQL_PARAM_INPUT,SQL_C_TYPE_TIME,SQL_TYPE_TIME,196,0,&f7Time,sizeof(f7Time),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,8,SQL_PARAM_INPUT,SQL_C_TYPE_TIMESTAMP,SQL_TYPE_TIMESTAMP,196,0,&f8Timestamp,sizeof(f8Timestamp),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,9,SQL_PARAM_INPUT,SQL_C_TINYINT,SQL_TINYINT,0,0,&f9temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,10,SQL_PARAM_INPUT,SQL_C_SBIGINT,SQL_BIGINT,0,0,&f10temp,0,NULL); checkrc(ret,__LINE__); //******************************************************************************************************* int j, count=0; f6Date=date; f7Time=time; f8Timestamp=timestamp; for(j=0;j<20;j++) { f1temp=f1++; f2temp=f2++; strcpy(f3temp,"jitendra"); f4temp=f4++; f5temp=f5; f9temp=f9; f10temp=f10; ret = SQLExecute(stmt); checkrc(ret,__LINE__); count++; } ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row deleted=%d\n",count); return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in conenction\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } //***************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT, PRIMARY KEY(F2));"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLPrepare(stmt,(SQLCHAR *)"CREATE INDEX INDS ON T1(F3) TREE;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("\nTABLE CREATED\n"); //*************************** FetchTest(env,dbc,stmt,"T1"); //************************************************** SQLCHAR drop[100]="DROP TABLE T1"; ret = SQLPrepare(stmt,drop,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); if(ret!=SQL_SUCCESS && ret !=SQL_SUCCESS_WITH_INFO) { printf("Statement failed\n"); return 1; } printf("Tables dropped successfully\n"); ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
//************************************************************************* int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=127.0.0.1;SERVER=localhost;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source successfully..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); // call to commit ret = SQLTransact(env,dbc,SQL_COMMIT); int rettype = ret; if(ret) printf("After disconnect,commit failed\n"); else printf("After disconenct, commit passed\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); if (rettype == 0) return 1; return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source successfully..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } //****************************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 INT)"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("\nTABLE CREATED\n"); //**************************************************************** // insert tuple into table ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(100,200)",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); //***************************************************************** // FETCHING ROWS FROM TABLE int f1=1; int f2=2; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1",SQL_NTS); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,1,SQL_C_LONG,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_LONG,&f2,0,NULL); checkrc(ret,__LINE__); int j,count=0; ret = SQLExecute(stmt); checkrc(ret,__LINE__); //close the connection ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); // after closing call fetch. int rettype; while(SQL_SUCCEEDED(ret =SQLFetch(stmt))) { if(ret ) printf("After disconnect, Fetch failed\n"); else count++; } rettype=ret; printf("Number of rows are fetched=%d\n",count); // ***************************************************************** // again connect for drop the table ret = SQLConnect (dbc, (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nagain Connected to the Data Source successfully..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); ret = SQLPrepare(stmt,(unsigned char*)"drop table T1;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table dropped\n"); //**************************************************************** ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); if(rettype == 0) return 1; printf("After closing the connection, fetch does not work\n"); return 0; }
//*********************************************************************** int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field short int f2=20;//f2 field char f3[50]= "praba"; float f4 = 2.5; float f5 = 10.50; int f9=5; long long f10=15000; SQLINTEGER slen = SQL_NTS; SQL_DATE_STRUCT date,f6Date; SQL_TIME_STRUCT time, f7Time; SQL_TIMESTAMP_STRUCT timestamp, f8Timestamp; date.year=2008; date.month=03; date.day=18; time.hour=5; time.minute=22; time.second=10; timestamp.year=2008; timestamp.month=03; timestamp.day=18; timestamp.hour=5; timestamp.minute = 22; timestamp.second = 10; timestamp.fraction = 764576; int f1temp,f2temp; char f3temp[20]; float f4temp, f5temp; int f9temp; long long f10temp; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1 ",SQL_NTS); checkrc(ret,__LINE__); short totalFields=0; ret = SQLNumResultCols (stmt, &totalFields); checkrc(ret,__LINE__); printf( "No of columns in resultset = %d\n",totalFields); UWORD icol; UCHAR colName[IDENTIFIER_LENGTH]; SWORD colNameMax; SWORD nameLength; SWORD colType; SQLULEN colLength; SWORD scale; SWORD nullable; icol = 1; colNameMax = IDENTIFIER_LENGTH; while(icol <=10){ ret = SQLDescribeCol(stmt, icol, colName, colNameMax, &nameLength, &colType, &colLength, &scale, &nullable); checkrc(ret,__LINE__); printf("Name %s len %d type %d collen %d scale %d nullable %d\n",colName,nameLength,colType,colLength,scale,nullable ); icol ++; } ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_SSHORT,&f2,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_FLOAT,&f4,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,5,SQL_C_FLOAT,&f5,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_TYPE_DATE,&date,sizeof(date),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,7,SQL_C_TYPE_TIME,&time,sizeof(time),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,8,SQL_C_TYPE_TIMESTAMP,×tamp,sizeof(timestamp),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,9,SQL_C_TINYINT,&f9,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,10,SQL_C_SBIGINT,&f10,0,NULL); checkrc(ret,__LINE__); int j, count=0; f6Date=date; f7Time=time; f8Timestamp=timestamp; for(j=0;j<10;j++) { f1temp=f1++; f2temp=f2++; strcpy(f3temp,"praba"); f4temp=f4++; f5temp=f5; f9temp=f9; f10temp=f10; ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLFetch(stmt); ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); count++; printf("F1=%d\tF2=%d\tF3=%s\tF4=%f\tF5=%f\tDATE=%d/%d/%d\tTIME=%d-%d-%d\tTIMESTAMP=%d/%d/%d %d-%d-%d\tF9=%d\tF10=%lld\n ",f1,f2,f3,f4,f5,date.year,date.month,date.day,time.hour,time.minute,time.second,timestamp.year,timestamp.month,timestamp.day,timestamp.hour,timestamp.minute,timestamp.second,f9,f10); } ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row fetched=%d\n",count); return 0; }
//*********************************************************************** // FETCH ROWS FROM THE TABLE "T1"......select * from T1; int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field short int f2=20;//f2 field char f3[50]= "jitendra"; float f4 = 2.5; float f5 = 10.50; int f9 = 5; long long f10 = 15000; int result; SQL_DATE_STRUCT date; char strDate[30]; date.year=2007; date.month=03; date.day=18; // strcpy(strDate,"{d '2008-03-18'}"); //********************************************** SQL_TIME_STRUCT time; time.hour = 5; time.minute = 22; time.second = 10; //********************************************* SQL_TIMESTAMP_STRUCT timestamp; timestamp.year = 2007; timestamp.month = 03; timestamp.day = 18; timestamp.hour = 5; timestamp.minute = 22; timestamp.second = 10; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1",SQL_NTS); checkrc(ret,__LINE__); //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,) ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_SSHORT,&f2,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_FLOAT,&f4,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,5,SQL_C_FLOAT,&f5,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_TYPE_DATE,&date,sizeof(date),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,7,SQL_C_TYPE_TIME,&time,sizeof(time),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,8,SQL_C_TYPE_TIMESTAMP,×tamp,sizeof(timestamp),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,9,SQL_C_TINYINT,&f9,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,10,SQL_C_SBIGINT,&f10,0,NULL); checkrc(ret,__LINE__); int j, count=0; ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Fetching starts on table T1 :\n"); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))) { printf("F1=%d\tF2=%d\tF3=%s\tF4=%f\tF5=%f\tDATE=%d/%d/%d\tTIME=%d-%d-%d\tTIMESTAMP=%d/%d/%d %d-%d-%d\tF9=%d\tF10=%lld\n ",f1,f2,f3,f4,f5,date.year,date.month,date.day,time.hour,time.minute,time.second,timestamp.year,timestamp.month,timestamp.day,timestamp.hour,timestamp.minute,timestamp.second,f9,f10); count++; } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row fetched=%d\n",count); return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; //Allocate an environment handle ret = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need ODBC3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //Allocate a Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); //connect to Data source ret = SQLConnect (dbc, (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); /* //connect using unixODBC Driver Manager ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*) "DSN=mycsql;USER=root;PASSWORD=manager;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT); */ if(SQL_SUCCEEDED(ret)) { printf("Connected to CSQL\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 2; } // Allocation of statement handle for DDL nad DML Operation ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); // create table 'T1' with two fields, F1 INTEGER AND F2 CHAR. SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 CHAR(20))"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table T1 created\n"); //Insert 10 Tuples into the table 'T1' int id=10; char name[20]="THIRU"; char names[10][20]={"Gopal", "Aruna", "Kanchana", "Vijay", "Ganga", "XieLiang", "Rajesh", "Steve", "Veda", "Jitendra" }; SQLINTEGER slen = SQL_NTS; ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(?,?);",SQL_NTS); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&id,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)name,0,&slen); checkrc(ret,__LINE__); int i,count=0; for(i=0;i<10;i++) { id++; strcpy(name,names[i]); ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); count++; } printf("%d Rows inserted\n",count); //Fetch rows from the table 'T1' int id1=10; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,1,SQL_C_SLONG,&id1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_CHAR,name,sizeof(name),NULL); checkrc(ret,__LINE__); count=0; ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Fetching starts on table T1 :\n"); while(SQL_SUCCEEDED(ret=SQLFetch(stmt))) { printf("F1:%d\tF2:%s\n",id1,name); count++; } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("%d rows fetched\n",count); //Delete all the rows from the table 'T1' ret = SQLPrepare(stmt,(unsigned char*)"DELETE FROM T1 WHERE F1=?;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&id1,0,NULL); checkrc(ret,__LINE__); count=0; for(i=0;i<10;i++) { id++; ret = SQLExecute(stmt); checkrc(ret,__LINE__); count++; } ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("%d Rows deleted\n",count); // drop the table 'T1' SQLCHAR drop[50]="DROP TABLE T1"; ret = SQLPrepare(stmt,drop,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table T1 dropped\n"); //Free the statement handle ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); //Disconnect from the Data source ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); printf("Disconnected from CSQL\n"); //Free the connection handle ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); //Free the environment handle ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
//*********************************************************************** // FETCH ROWS FROM THE TABLE "T1"......select * from T1; int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field short int f2=20;//f2 field char f3[50]= "praba"; float f4 = 2.5; float f5 = 10.50; int f9=5; long long f10=15000; SQLINTEGER slen = SQL_NTS; SQL_DATE_STRUCT date,f6Date; SQL_TIME_STRUCT time, f7Time; SQL_TIMESTAMP_STRUCT timestamp, f8Timestamp; date.year=2008; date.month=03; date.day=18; time.hour=5; time.minute=22; time.second=10; timestamp.year=2008; timestamp.month=03; timestamp.day=18; timestamp.hour=5; timestamp.minute = 22; timestamp.second = 10; timestamp.fraction = 764576; int f1temp,f2temp; char f3temp[20]; float f4temp, f5temp; int f9temp; long long f10temp; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1 WHERE F1=? AND F2=? AND F3=? AND F4=? AND F5=? AND F6=? AND F7=? AND F8=? AND F9=? AND F10=?",SQL_NTS); checkrc(ret,__LINE__); //ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SMALL,SQL_SMALL,0,0,) ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_INTEGER,0,0,&f2temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)f3temp,0,&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,4,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_FLOAT,0,0,&f4temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,5,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_REAL,0,0,&f5temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,6,SQL_PARAM_INPUT,SQL_C_TYPE_DATE,SQL_TYPE_DATE,196,0,&f6Date,sizeof(f6Date),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,7,SQL_PARAM_INPUT,SQL_C_TYPE_TIME,SQL_TYPE_TIME,196,0,&f7Time,sizeof(f7Time),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,8,SQL_PARAM_INPUT,SQL_C_TYPE_TIMESTAMP,SQL_TYPE_TIMESTAMP,196,0,&f8Timestamp,sizeof(f8Timestamp),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,9,SQL_PARAM_INPUT,SQL_C_TINYINT,SQL_TINYINT,0,0,&f9temp,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,10,SQL_PARAM_INPUT,SQL_C_SBIGINT,SQL_BIGINT,0,0,&f10temp,0,NULL); checkrc(ret,__LINE__); //******************************************************************************************************* ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_SSHORT,&f2,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_FLOAT,&f4,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,5,SQL_C_FLOAT,&f5,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_TYPE_DATE,&date,sizeof(date),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,7,SQL_C_TYPE_TIME,&time,sizeof(time),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,8,SQL_C_TYPE_TIMESTAMP,×tamp,sizeof(timestamp),NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,9,SQL_C_TINYINT,&f9,0,NULL); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,10,SQL_C_SBIGINT,&f10,0,NULL); checkrc(ret,__LINE__); int j, count=0; f6Date=date; f7Time=time; f8Timestamp=timestamp; for(j=0;j<10;j++) { f1temp=f1++; f2temp=f2++; strcpy(f3temp,"praba"); f4temp=f4++; f5temp=f5; f9temp=f9; f10temp=f10; ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLFetch(stmt); ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); count++; printf("F1=%d\tF2=%d\tF3=%s\tF4=%f\tF5=%f\tDATE=%d/%d/%d\tTIME=%d-%d-%d\tTIMESTAMP=%d/%d/%d %d-%d-%d\tF9=%d\tF10=%lld\n ",f1,f2,f3,f4,f5,date.year,date.month,date.day,time.hour,time.minute,time.second,timestamp.year,timestamp.month,timestamp.day,timestamp.hour,timestamp.minute,timestamp.second,f9,f10); } ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row fetched=%d\n",count); return 0; }
//************************************************************************* int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the Data source ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,env); checkrc(ret,__LINE__); return 1; } //********************************************************************* //again call to driver connect ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=ADAPTER;SERVER=127.0.0.1;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); int rettype = ret; if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("Connection name in use\n"); } //********************************************************************** ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); if(rettype ==0)return 1; return 0; }
//*********************************************************************** // FETCH ROWS FROM THE TABLE "T1"......select * from T1; int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt) { int ret; int f1=10; // f1 field short int f2=20;//f2 field char f3[50]= "praba"; float f4 = 2.5; float f5 = 10.50; int f9=5; long long f10=15000; SQLINTEGER slen = SQL_NTS; SQLINTEGER slen1[11]; SQL_DATE_STRUCT date,f6Date; SQL_TIME_STRUCT time, f7Time; SQL_TIMESTAMP_STRUCT timestamp, f8Timestamp; slen1[0]=0; date.year=2008; date.month=03; date.day=18; time.hour=5; time.minute=22; time.second=10; timestamp.year=2008; timestamp.month=03; timestamp.day=18; timestamp.hour=5; timestamp.minute = 22; timestamp.second = 10; timestamp.fraction = 764576; int f1temp,f2temp; char f3temp[20]; float f4temp, f5temp; int f9temp; long long f10temp; ret = SQLPrepare(stmt,(unsigned char*)"SELECT * FROM T1 ",SQL_NTS); checkrc(ret,__LINE__); //******************************************************************************************************* ret = SQLBindCol(stmt,1,SQL_C_SLONG,&f1,0,&slen1[1]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,2,SQL_C_SSHORT,&f2,0,&slen1[2]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,3,SQL_C_CHAR,f3,sizeof(f3),&slen1[3]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,4,SQL_C_FLOAT,&f4,0,&slen1[4]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,5,SQL_C_FLOAT,&f5,0,&slen1[5]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,6,SQL_C_TYPE_DATE,&date,sizeof(date),&slen1[6]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,7,SQL_C_TYPE_TIME,&time,sizeof(time),&slen1[7]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,8,SQL_C_TYPE_TIMESTAMP,×tamp,sizeof(timestamp),&slen1[8]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,9,SQL_C_TINYINT,&f9,0,&slen1[9]); checkrc(ret,__LINE__); ret = SQLBindCol(stmt,10,SQL_C_SBIGINT,&f10,0,&slen1[10]); checkrc(ret,__LINE__); int j, count=0; f6Date=date; f7Time=time; f8Timestamp=timestamp; ret = SQLExecute(stmt); checkrc(ret,__LINE__); while(SQL_SUCCEEDED(ret = SQLFetch(stmt))){ count++; int k=1; if(slen1[k++] == SQL_NULL_DATA) printf("F1=NULL "); else printf("F1=%d ",f1); if(slen1[k++] == SQL_NULL_DATA) printf("F2=NULL "); else printf("F2=%d ",f2); if(slen1[k++] == SQL_NULL_DATA) printf("F3=NULL "); else printf("F3=%s",f3); if(slen1[k++] == SQL_NULL_DATA) printf("F4=NULL "); else printf("F4=%f ",f4); if(slen1[k++] == SQL_NULL_DATA) printf("F5=NULL "); else printf("F5=%f ",f5); if(slen1[k++] == SQL_NULL_DATA) printf("F6=NULL "); else printf("F6=%d/%d/%d ",date.year,date.month,date.day); if(slen1[k++] == SQL_NULL_DATA) printf("F7=NULL "); else printf("F7=%d-%d-%d ",time.hour,time.minute,time.second); if(slen1[k++] == SQL_NULL_DATA) printf("F8=NULL "); else printf("F8=%d/%d/%d %d-%d-%d ",timestamp.year,timestamp.month,timestamp.day,timestamp.hour,timestamp.minute,timestamp.second); if(slen1[k++] == SQL_NULL_DATA) printf("F9=NULL "); else printf("F9=%d ",f9); if(slen1[k++] == SQL_NULL_DATA) printf("F10=NULL \n"); else printf("F10=%lld \n",f10); } ret = SQLCloseCursor(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); printf("Total row fetched=%d\n",count); return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support ret =SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); checkrc(ret,__LINE__); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;", (SQLSMALLINT) strlen ("DSN=mycsql;MODE=csql;SERVER=127.0.0.1;PORT=5678;"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } ret = SQLSetConnectAttr(dbc,SQL_ATTR_AUTOCOMMIT,(void*)SQL_AUTOCOMMIT_OFF,SQL_IS_UINTEGER); checkrc(ret,__LINE__); //****************************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 INT);"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLFreeStmt(stmt, SQL_CLOSE); printf("\nTABLE CREATED\n"); //**************************************************************** ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(1,1);",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLFreeStmt(stmt, SQL_CLOSE); ret = SQLEndTran(SQL_HANDLE_DBC,dbc,SQL_COMMIT); if(ret!=SQL_SUCCESS && ret !=SQL_SUCCESS_WITH_INFO) printf("error in commit\n"); //InsertTest(env,dbc,stmt); //***************************************************************** //update ret = SQLPrepare(stmt,(unsigned char*)"UPDATE T1 SET F2=100;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); SQLFreeStmt(stmt, SQL_CLOSE); ret = SQLEndTran(SQL_HANDLE_DBC,dbc,SQL_COMMIT); if(ret!=SQL_SUCCESS && ret !=SQL_SUCCESS_WITH_INFO) printf("error in commit\n"); checkrc(ret,__LINE__); //*********************************************************** //FETCH FetchTest(env,dbc,stmt); //**************************************************************** ret = SQLExecDirect(stmt,(unsigned char*)"DROP TABLE T1",SQL_NTS); checkrc(ret,__LINE__); SQLFreeStmt(stmt, SQL_CLOSE); printf("Table T1 dropped\n"); ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 0; }
//************************************************************************* // FUNCTION FOR INSERTING ROWS IN IT. int InsertTest(SQLHANDLE env,SQLHANDLE dbc,SQLHANDLE stmt) { int ret; int f1=90; // f1 field short int f2=20;//f2 field char *f3; f3 =NULL; //(char*)malloc(50); float f4 = 2.5; float f5 = 10.50; int f9 = 5; long long f10 = 15000; int result; SQLINTEGER slen = SQL_NULL_DATA; //*********************************** // STRUCTURE FIOR DATE DATATYPE SQL_DATE_STRUCT date; char strDate[30]; date.year=2008; date.month=03; date.day=18; // strcpy(strDate,"{d '2008-03-18'}"); //******************************* // STRUCTURE FOR TIME DATATYPE. SQL_TIME_STRUCT time; time.hour = 5; time.minute = 22; time.second = 10; //***************************** // STRUCTURE FOR TIMESTAMP DATATYPE SQL_TIMESTAMP_STRUCT timestamp; timestamp.year = 2008; timestamp.month = 03; timestamp.day = 18; timestamp.hour = 5; timestamp.minute = 22; timestamp.second = 10; timestamp.fraction = 764576; //****************************** // PREPARE THE STATEMENT. ret = SQLPrepare(stmt,(unsigned char*)"INSERT INTO T1 VALUES(?,?,?,?,?,?,?,?,?,?)",SQL_NTS); checkrc(ret,__LINE__); // BIND PARAMETER FOR ALL THE FIELD ret = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_SLONG,SQL_INTEGER,0,0,&f1,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,2,SQL_PARAM_INPUT,SQL_C_SSHORT,SQL_SMALLINT,0,0,&f2,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,196,0,(void*)f3,0,&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,4,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_FLOAT,0,0,&f4,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,5,SQL_PARAM_INPUT,SQL_C_FLOAT,SQL_REAL,0,0,&f5,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,6,SQL_PARAM_INPUT,SQL_C_TYPE_DATE,SQL_TYPE_DATE,196,0,&date,sizeof(date),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,7,SQL_PARAM_INPUT,SQL_C_TYPE_TIME,SQL_TYPE_TIME,196,0,&time,sizeof(time),&slen); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,8,SQL_PARAM_INPUT,SQL_C_TYPE_TIMESTAMP,SQL_TYPE_TIMESTAMP,196,0,×tamp,sizeof(timestamp),NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,9,SQL_PARAM_INPUT,SQL_C_TINYINT,SQL_TINYINT,0,0,&f9,0,NULL); checkrc(ret,__LINE__); ret = SQLBindParameter(stmt,10,SQL_PARAM_INPUT,SQL_C_SBIGINT,SQL_BIGINT,0,0,&f10,0,NULL); checkrc(ret,__LINE__); int i,count=0; // EXECUTE THE STATEMENT for(i=0;i<10;i++) { f1++; f2++; f4++; ret = SQLExecute(stmt); checkrc(ret,__LINE__); ret = SQLTransact(env,dbc,SQL_COMMIT); checkrc(ret,__LINE__); count++; } printf("Total row inserted=%d\n",count); return 0; }
int main() { SQLHENV env; SQLHDBC dbc; SQLHSTMT stmt; SQLRETURN ret; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; // Aloocate an environment handle ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env); checkrc(ret,__LINE__); //we need odbc3 support SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); //ALLOCATE A Connection handle ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc); checkrc(ret,__LINE__); // connect to the DSN mydsn ret = SQLConnect (dbc, (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"), (SQLCHAR *) "root", (SQLSMALLINT) strlen ("root"), (SQLCHAR *) "manager", (SQLSMALLINT) strlen ("")); if(SQL_SUCCEEDED(ret)) { printf("\nConnected to the Data Source..\n"); } else { printf("error in connection\n"); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); return 1; } //****************************************************************** // TABLE CREATED ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt); checkrc(ret,__LINE__); SQLCHAR table[200]= "CREATE TABLE T1(F1 INT,F2 INT)"; ret = SQLPrepare(stmt,table,SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); printf("\nTABLE CREATED\n"); //**************************************************************** InsertTest(env,dbc,stmt); //***************************************************************** int ret1; ret1=FetchTest(env,dbc,stmt); //**************************************************************** ret = SQLPrepare(stmt,(unsigned char*)"drop table T1;",SQL_NTS); checkrc(ret,__LINE__); ret = SQLExecute(stmt); checkrc(ret,__LINE__); printf("Table 'T1' Dropped\n"); ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt); checkrc(ret,__LINE__); ret = SQLDisconnect(dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc); checkrc(ret,__LINE__); ret = SQLFreeHandle(SQL_HANDLE_ENV,env); checkrc(ret,__LINE__); if(ret1 == 0)return 1; return 0; }