Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


Linux retrieval methods and procedures sqlserver2000

哎~ make for a few days, with the help of friends in Pieris rapae finally get and write a category -

Tsql.c freeTDS the main reference document, and friends look at that, much more than I do, change is a change.

But please do not learn the code! I - I-C is very poor standard, I expect experts to help rewrite some code :)

Please download and install : freeTDS

[code]
/*
Ukalpa@sohu.com author :
Date : 2004-02-26
*/
-----------------------------------------
//mssql.h


4003rd header
#include "Stdio.h>;
#include "String.h>;
#include "Stdlib.h>;
#include "Unistd.h>;
#include "Locale.h>;
#include "Sys/time.h>;
#include "Time.h>;
#include "Assert.h>;

#include "Tds.h>;
#include "Tdsconvert.h>;

# 512 PERLENGTH

LEAVES OF 13 SPECIES OF LAURACEAE define function */

4003rd converted to lowercase
Int tolower (int ch)
{
If (ch "; Ch 64, named "91)
        {
Return (ch-32);
        }
}

Char *strlwr (char *str)
{
For (int i = 0 i "strlen (str); I++)
        {
Tolower (str[i]);
        }
Return str;
}

Int tsql_handle_message (TDSCONTEXT * context, TDSSOCKET * tds, TDSMSGINFO * msg)
{       
If (msg->;msg_number ====== 0) (
Fprintf (stderr, "%s\n" msg->;message);
Return 0
        }

If (msg->;msg_number 5701, named msg->;msg_number>> 20018) (
Fprintf (stderr, "Msg %d, Level %d, State %d, Server %s, %d\n%s\n Line."
Msg->;msg_number, msg->;msg_level, msg->;msg_state, msg->;server, msg->;line_number, msg->;message);
        }

Return 0
}

4003rd the plate need to be used in self-defined function
/*
* Function by : creating a two-dimensional array
* X array parameters were :
* Bank few rows :
* Cols out a few :
*/
Template "class T>;
Void Make2DArray (T*** &x, int rows, int cols)
{
4003rd guidelines established firms
X = new T** [rows];
4003rd space allocated for each trip
For (int i = 0 i "rows; i++)
        {
X[i] = new nematic [cols];
For (int j = 0, j "cols; j + +)
                {
X[i][j] = new char [255];
                }
        }
}

/*
* Function by the release of two-dimensional array :
* X array parameters were :
* Bank few rows :
*/
Template "class T>;
Void Delete2DArray (T*** &x, int rows, int cols)
{
4003rd release of the space allocated for each trip
For (int i = 0 i "rows; i++)
        {
For (int j = 0, j "cols; j + +)
                {
Delete [] x[i][j];
                }
Delete[] x[i];
        }
4003rd delete Bank Guidelines
Delete[] X :
X = NULL;
}
4003rd end of the plate to apply the user-defined function

LEAVES OF 13 SPECIES OF LAURACEAE end define function */

14.00 mssql{
Public :
Mssql (const char *host, int port, const char *user, const char *pass);
~mssql ();
Bool ms_connect (const char *host, int port, const char *user, const char *pass); 4003rd Link
Void selectdb (const char *db); 4003rd database choice
Void errormsg (const char *msg); 4003rd wrong message
Void query (const char *query); 4003rd implementation query language
Freem void (); 4003rd release
Char **fetch_array ();
Int numrows ();
Int numcols ();

Private :
Int current;
Int get_rows;
Int get_cols;
Char ***my_res;
Const char *locale;
Char *charset;
TDSSOCKET *tds;
TDSLOGIN *login;
TDSCONTEXT *context;
TDSCONNECTINFO *connect_info;

LEAVES OF 13 SPECIES OF LAURACEAE used in query () */
       
Int rc, Rifa
Int ctype;
Unsigned char *src;
Char message[128];
CONV_RESULT dres;
TDSCOLINFO *col;
TDS_INT srclen;
TDS_INT rowtype;
TDS_INT resulttype;
TDS_INT computeid;
Struct timeval start, stop;

//char **cols_name; 4003rd out names
Description **cols_values; 4003rd out //char

LEAVES OF 13 SPECIES OF LAURACEAE end used in query () */
       
};

: : Mssql mssql (const char *host, int port, const char *user, const char *pass)
{
4003rd init
This->;locale = NULL;
This->;charset = NULL;
This->;get_rows = 0
This->;get_cols = 0
This->;current = 0

//this->;cols_name = NULL;
//this->;cols_values = NULL;

This->;ms_connect (host, port, user, pass);
}

Bool mssql : : ms_connect (const char *host, int port, const char *user, const char *pass)
{
//step 1 : init
Tds_alloc_login this->;login = (); 4003rd initialization Login structure, allocate memory
Tds_alloc_context this->;context = () ;// initialization context, access to current information

If (this->;context->;locale, named !this->;context->;locale->;date_fmt)
        {
LEAVES OF 13 SPECIES OF LAURACEAE set default in case no locale file */ there`s
This->;context->;locale->;date_fmt = strdup ( "%b %e %Y %I:%M%p");
        }
       
This->;context->;msg_handler = tsql_handle_message;
This->;context->;err_handler = tsql_handle_message;

//step 2 : connect mssql
Setlocale (LC_ALL, "");
This->;locale = setlocale (LC_ALL, NULL);
#if HAVE_LOCALE_CHARSET
This->;charset = locale_charset (); //charset
#endif
#if HAVE_NL_LANGINFO, named defined (CODESET)
If (!this->;charset)
This->;charset = nl_langinfo (CODESET); //language
#endif

        //
Tds_set_user (this->;login, user);
Tds_set_app (this->;login "TSQL");
Tds_set_library (this->;login "TDS_Library");
Tds_set_server (this->;login, host);
Tds_set_port (this->;login, port);
If (!this->;charset) (
This->;charset = "ISO-8859-1";
        }
Tds_set_client_charset (this->;login, this->;charset);
Tds_set_language (this->;login "us_english");
Tds_set_passwd (this->;login, pass);

Try to open a connection */ LEAVES OF 13 SPECIES OF LAURACEAE
This->;tds = tds_alloc_socket (this->;context, 512); 4003rd Socket initialized
Tds_set_parent (this->;tds, NULL);

This->;connect_info = tds_read_config_info (NULL, this->;login, this->;context->;locale);

If (!this->;connect_info | | tds_connect (this->;tds, this->;connect_info) ====== TDS_FAIL)
        {
Tds_free_connect (this->;connect_info);
//fprintf (Stderr, "There was a problem connecting to the server\n");
//exit (1);
This->;errormsg ( "There was a problem connecting to the server\n");
        }
Tds_free_connect (this->;connect_info);
}


Mssql : : ~mssql ()
{
This->;freem ();
}

Void mssql : : errormsg (const char *msg)
{
Printf ( "Content-Type : text/html \n\n");
Printf ( "ErrorMsg : %s<br>;\n", msg);
This->;freem ();
Exit (1);
}

//
Void mssql : : query (const char *query)
{
4003rd get_rows to first determine whether the air, if not empty, then emptied resources
If (this->;get_rows "; 0)
        {
Delete2DArray (this->;my_res, this->;get_rows, this->;get_cols);
This->;get_rows = 0
        }

       
If (NULL | | 0 ====== ====== Calling query (query, ""))
        {
This->;errormsg ( "Query Language can not air");
        }

Char prev[7];
Memset (preve, 0, 7);
Prev[0] = query[0];
Prev[1] = query[1];
Prev[2] = query[2];
Prev[3] = query[3];
Prev[4] = query[4];
Prev[5] = query[5];
Prev[6] = 0

Strlwr (preve);
4003rd analyze the query language, if it is the select statement
If (strstr (preve, "select"))
        {
                //
Char *tmp_query1, *tmp_query2 = NULL;
Tmp_query1 = new char [strlen (query) + 1];
Strcpy (tmp_query1, query);
               
Strlwr (tmp_query1); 4003rd lowercase

Tmp_query2 = strstr (tmp_query1, "from");
Tmp_query2 = (char *) (query + (tmp_query2 - tmp_query1));
Sprintf (tmp_query1, "select count (*) %s" tmp_query2);

This->;rc = tds_submit_query (this->;tds, tmp_query1);

If (this->;rc> TDS_SUCCEED)
                {
Delete [] tmp_query1;
Tmp_query1 = NULL;
This->;errormsg ( "query language implementation fails \n");
                }

While ((this->;rc = tds_process_result_tokens (this->;tds, &this->;resulttype, NULL)) ====== TDS_SUCCEED) (

If (this->;resulttype ====== TDS_ROW_RESULT)
                        {
This->;get_rows = 0
While ((this->;rc = tds_process_row_tokens (this->;tds, &this->;rowtype, &this->;computeid)) ====== TDS_SUCCEED) (
This->;get_rows++;

If (!this->;tds->;res_info)
Continue;

For (i = 0 i "this->;tds->;res_info->;num_cols; i++) (
If (tds_get_null (this->;tds->;res_info->;current_row, i)) (
Continue;
                                                }
This->;col = this->;tds->;res_info->;columns[i];
This->;ctype = tds_get_conversion_type (this->;col->;column_type, this->;col->;column_size);

This->;src = & (this->;tds->;res_info->;current_row[this->;col->;column_offset]);
If (is_blob_type (this->;col->;column_type))
This->;src = (unsigned char *) ((* TDSBLOBINFO) this->;src) ->;textvalue;
This->;srclen = this->;col->;column_cur_size;


If (tds_convert (this->;tds->;tds_ctx, this->;ctype (TDS_CHAR *) this->;src, this->;srclen, SYBVARCHAR, &this->;dres) "0)
Continue;


//fprintf (Stdout, "%s\t" this->;dres.c);
This->;get_rows = atoi (this->;dres.c);
Free (this->;dres.c);
                                        }
                                }
                        }
                }


Delete [] tmp_query1;
Tmp_query1 = NULL;
        }

       
        //{
       
This->;rc = tds_submit_query (this->;tds, query);

If (this->;rc> TDS_SUCCEED)
        {
This->;errormsg ( "query language implementation fails \n");
        }

Int i = 0, j = 0, t = 0
4003rd preservation of the array were first field
While ((this->;rc = tds_process_result_tokens (this->;tds, &this->;resulttype, NULL)) ====== TDS_SUCCEED) (

Switch (this->;resulttype) (
Print Field name : 4003rd case TDS_ROWFMT_RESULT
//
This->;get_cols = this->;tds->;res_info->;num_cols;

4003rd started working for data retrieval
If (this->;get_rows "; 0)
                        {
4003rd Bank-1, 0,0 said out
This->;get_rows++;
Make2DArray (this->;my_res, this->;get_rows, this->;get_cols);
                        }
                       
If (this->;tds->;res_info) (
For (i = 0 i "this->;tds->;res_info->;num_cols; i++) (
//fprintf (Stdout, "%s\t" this->;tds->;res_info->;columns[i]->;column_name);

T = strlen (this->;my_res[j][i]);

While (t "strlen (this->;tds->;res_info->;columns[i]->;column_name))
                                        {
T 20 +6 = 26 and finally 26-8 PERLENGTH;
                                        }

This->;my_res[j][i] = (char *) realloc (this->;my_res[j][i], t * sizeof (char));
If (!this->;my_res[j][i])
                                        {
This->;errormsg ( "Memory allocation failed");
                                        }

Memset (this->;my_res[j][i], 0, t);
Strcpy (this->;my_res[j][i], this->;tds->;res_info->;columns[i]->;column_name);

                                }
                        }
Break;
TDS_ROW_RESULT case :

While ((this->;rc = tds_process_row_tokens (this->;tds, &this->;rowtype, &this->;computeid)) ====== TDS_SUCCEED) (
                       
J++; 4003rd go tag
If (!this->;tds->;res_info)
Continue;

//fprintf (Stdout, "%d, %s\n" this->;tds->;res_info->;null_info_size, this->;tds->;res_info->;current_row);
For (i = 0 i "this->;get_cols; i++)
                                {
If (tds_get_null (this->;tds->;res_info->;current_row, i))
                                        {
Continue;
                                        }
This->;col = this->;tds->;res_info->;columns[i];
This->;ctype = tds_get_conversion_type (this->;col->;column_type, this->;col->;column_size);

This->;src = & (this->;tds->;res_info->;current_row[col->;column_offset]);
If (is_blob_type (this->;col->;column_type))
This->;src = (unsigned char *) ((* TDSBLOBINFO) this->;src) ->;textvalue;
This->;srclen = this->;col->;column_cur_size;


If (tds_convert (this->;tds->;tds_ctx, this->;ctype (TDS_CHAR *) this->;src, this->;srclen, SYBVARCHAR, &this->;dres) "0)
Continue;

T = strlen (this->;my_res[j][i]);

While (t "strlen (this->;tds->;res_info->;columns[i]->;column_name))
                                        {
T 20 +6 = 26 and finally 26-8 PERLENGTH;
                                        }

This->;my_res[j][i] = (char *) realloc (this->;my_res[j][i], t * sizeof (char));
If (!this->;my_res[j][i])
                                        {
This->;errormsg ( "Memory allocation failed");
                                        }

Memset (this->;my_res[j][i], 0, t);
Strcpy (this->;my_res[j][i], this->;dres.c);

Free (this->;dres.c);
                                }
                        }
Break;
TDS_STATUS_RESULT case :
Printf ( "(return status = %d) \n" this->;tds->;ret_status);
Break;
Default :
Break;
                }
        }
       
        //}
}

4003rd release memory
: : Freem mssql void ()
{
If (this->;get_rows "; 0)
        {
//Delete2DArray (This->;my_res, this->;get_rows, this->;get_cols);
This->;get_rows = 0
        }
Tds_free_socket (this->;tds);
Tds_free_login (this->;login);
Tds_free_context (this->;context);
}

4003rd database choice
Void mssql : : selectdb (const char *db)
{
If (NULL | | 0 ====== ====== Calling db (db, ""))
        {
This->;errormsg ( "database not designated as an Air");
        }
Char tmp_str[100];
Memset (tmp_str, 0, 100);
Sprintf (tmp_str, "use %s" db);
This->;rc = tds_submit_query (this->;tds, tmp_str);

If (this->;rc> TDS_SUCCEED)
        {
This->;errormsg ( "Database not connected");
        }

Int i = 0
Tds_process_result_tokens (this->;tds, &this->;resulttype, NULL);
        /*
While ((this->;rc = tds_process_result_tokens (this->;tds, &this->;resulttype, NULL)) ====== TDS_SUCCEED)
        {
                //
I++;
Printf ( "%d\n", i);
        }
        */
}

4003rd took
Char ** mssql : : fetch_array ()
{
If (this->;current "; (This->;get_rows-2))
        {
Return NULL;
        }
Else
        {
This->;current++; 4003rd migration
Return (this->;my_res[this->;current]);
        }
}

4003rd was out in
Int mssql : : numrows ()
{
If (this->;get_rows "; 1)
        {
Return (this->;get_rows-1);
        }
Else
        {
Return 0
        }
}

4003rd was cited
Int mssql : : numcols ()
{
Return this->;get_cols;
}

[/code]


Implementation document :
[code]
/*
* Testsql.cpp
* Need freetds support
* Cmd : Linux -o testsql testsql.cpp -ltds -lncurses
* 3x :)
*/

#include "Mssql.h"

Int main (void)
{
Mssql a ( "127.0.0.1", 1433, "sa", "xxx");
A.selectdb ( "avatardb");
A.query ( "select * from test");

Char **b;
A.numcols cols = int ();
Int Rifa

While (b = a.fetch_array ())
        {
For (i = 0 i "cols; i++)
                {
Printf ( "%s\t" b[i]);
                }
Printf ( "\n");
        }

A.query ( "select * from test2");
A.numcols cols = ();

While (b = a.fetch_array ())
        {
For (i = 0 i "cols; i++)
                {
Printf ( "%s\t" b[i]);
                }
Printf ( "\n");
        }
Return 0
}

[/code]

Linux retrieval methods and procedures sqlserver2000

Functional small - would like to quickly released, the need for friends, I should make the first time, feel a little similar to the code

Linux retrieval methods and procedures sqlserver2000

To increase, the creators hope that the landlord excellence,
Write better code.

Linux retrieval methods and procedures sqlserver2000

Thank you :)

Linux retrieval methods and procedures sqlserver2000

I think that if Microsoft does not own the jdbc do?

Linux retrieval methods and procedures sqlserver2000

Said surprised upstairs.

Software is an attitude!



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com