#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 著可以編譯運行 操作數據庫  

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

          // 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 不能兼容 還是有什么錯誤  ?
          而且我單獨 利用snprintf編程的時候是可以成功的

          posted on 2012-04-05 21:41 姚先進 閱讀(514) 評論(1)  編輯  收藏
          Comments
          • # re: sprintf 在cgi操作數據庫中的錯誤
            姚先進
            Posted @ 2012-04-06 14:57
            問題已經能夠自己解決 !sprintf out了
            改成 sqlite3_mprintf 問題解決!!  回復  更多評論   

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          主站蜘蛛池模板: 松滋市| 汤阴县| 金阳县| 石阡县| 揭阳市| 家居| 固阳县| 汉阴县| 屯留县| 内江市| 噶尔县| 舟山市| 永济市| 巴楚县| 钟山县| 开平市| 巴里| 抚顺县| 台中市| 鄱阳县| 景谷| 阳东县| 太保市| 彭阳县| 河西区| 阿拉尔市| 枝江市| 伊宁县| 曲沃县| 梅州市| 香港 | 长垣县| 寿光市| 措勤县| 司法| 巴彦县| 宁安市| 龙川县| 申扎县| 托克逊县| 兴山县|