#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 姚先进 阅读(503) 评论(1)  编辑  收藏
Comments
  • # re: sprintf 在cgi操作数据库中的错误
    姚先进
    Posted @ 2012-04-06 14:57
    问题已经能够自己解决 !sprintf out了
    改成 sqlite3_mprintf 问题解决!!  回复  更多评论   

只有注册用户登录后才能发表评论。


网站导航: