#include <stdio.h>

          #include <stdlib.h>

          #include <string.h>

          #include <sqlite3.h>


          #include "cgic.h"

           


          int cgiMain()

          {
           
          printf("Content-Type:text/html\n\n");
            printf("<html>");
            
          printf("<p>hello</p>");
           
          printf("</html>"); 

           
          char username[241];
           
          char password[241];

           cgiHeaderContentType("text/html");
           
           cgiFormString ( "username" , username , 10 );
           cgiFormString ( "password" , password , 10 );

           fprintf ( cgiOut , "<html><h2>%s</h2>" , username );
           fprintf ( cgiOut , "<h2>%s</h2></html>" , password );


           


           sqlite3 *db =NULL;
            char *zErrMsg =0;
           int rc;
           int i=0;
           int n=0;
           
           rc = sqlite3_open("test.db",&db);
           
           if(rc)
           {
            fprintf(stderr,"cannot open the database:%s\n",sqlite3_errmsg(db));
            sqlite3_close(db);
              exit(1);
           }
            else
           printf("opened the database successfuly\n");

           char *sql = "create table user( name , password );";
           sqlite3_exec(db , sql , 0 , 0 , &zErrMsg );


          sql = "insert into user values( 'yaonou' , 2007 );";   如果用這行 注釋掉snprintf 著可以編譯運(yùn)行 操作數(shù)據(jù)庫(kù)  

          // char *sqls ;
          // snprintf ( sql , sizeof(sql) , "insert into user values( %s , %s );" ,  username ,password ); 如果換成這一行的時(shí)候 boa服務(wù)器顯示502 錯(cuò)誤  為什么??、 

          // printf ( "%s" , username );
          // printf ( "%s" , password );

           printf ( "%s" , sql );
           
           
               n = sqlite3_exec( db , sql , 0 ,0 , &zErrMsg ); 

           if( n == SQLITE_OK)
           {  printf( "it have done " );
             printf( "%s",zErrMsg );
           }
           else
            printf("it has not running");
           
            sql = "select * from user;";
           sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
           if( n == SQLITE_OK)
           {  printf( "it have done " );
             printf( "%s",zErrMsg );
           }
           else
            printf( "select failed" ); 

           int sqlite3_get_table(sqlite3* , const char *sql , char***result , int *nrow , int *ncloumn , char **errmsg );


           int nrow=0;
           int ncloumn=0;
           char **firstResult;

           sql = "select * from user;";
           printf("\n");
           sqlite3_get_table( db , sql , &firstResult , &nrow , &ncloumn , &zErrMsg );
           if( n == SQLITE_OK)
           {  printf( "it have done " );
             
           }
           else
            printf( "select failed again " ); 
            printf( "%s",zErrMsg );

           printf( "row:%d cloumn:%d\n" , nrow , ncloumn );
           printf( "the result of querying is :\n" );

           for( i=0 ; i<( nrow + 1 ) * ncloumn ; i++ )
            printf( "firstResult[%d]= %s\n" , i , firstResult[i] );

            sqlite3_free_table( firstResult );

           sql = "update user set password = 'yaxianjin' where name = 'yaonou';";
           printf( "\n" );
            sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );

           nrow=0;
           ncloumn=0;
            char **secondResult;

           sql = "select * from user;";
           sqlite3_get_table( db , sql , &secondResult , &nrow , &ncloumn , &zErrMsg );

           
           printf("<html>\n");
           printf("<head><title>SQLITE CGI </title></head>\n");
           printf("<body>");
           printf("<h1>hello<h1>\n");
           printf("<p>\n");
           printf("<p>\n");

           printf( "row:%d cloumn:%d\n" , nrow , ncloumn );
           printf( "the result of querying is :\n" );

           for( i=0 ; i<(nrow+1)*ncloumn ; i++ )
            printf( "secondResult[%d]=%s\n" , i , secondResult[i] );

           sqlite3_free_table( secondResult );

           printf( "\n" );
           sqlite3_close( db );
                  return 0;
          }
          難道 snprintf 和sqlite 不能兼容 還是有什么錯(cuò)誤  ?
          而且我單獨(dú) 利用snprintf編程的時(shí)候是可以成功的

          posted on 2012-04-05 21:41 姚先進(jìn) 閱讀(516) 評(píng)論(1)  編輯  收藏
          Comments
          • # re: sprintf 在cgi操作數(shù)據(jù)庫(kù)中的錯(cuò)誤
            姚先進(jìn)
            Posted @ 2012-04-06 14:57
            問(wèn)題已經(jīng)能夠自己解決 !sprintf out了
            改成 sqlite3_mprintf 問(wèn)題解決!!  回復(fù)  更多評(píng)論   

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 建瓯市| 鄯善县| 巴林右旗| 高邑县| 南华县| 修水县| 宁南县| 新民市| 永新县| 云阳县| 西贡区| 大连市| 宁城县| 浏阳市| 晋江市| 绥棱县| 玉田县| 青海省| 白朗县| 龙州县| 桂林市| 临清市| 昆山市| 通渭县| 南康市| 体育| 岑溪市| 洪洞县| 蒲城县| 民权县| 托克托县| 华宁县| 循化| 乐都县| 定远县| 百色市| 江口县| 康保县| 香河县| 方正县| 凤阳县|