Sqlite Example

Sqlite Example

sample code


#include "sqlite3.h"
#include "sqliteInt.h"
 
char * err;
int r( sqlite3 * db, char * sql )
{ 
   int res = sqlite3_exec( db, sql, 0, 0, &err ); 
   if ( err )   
      printf( "ERROR: %s\n", err ); 
   return res;
}
 
void d( sqlite3 * db, char * sql )
{ 
   sqlite3_stmt * stmt; 
   int rc = sqlite3_prepare( db, sql, -1, &stmt, 0 ); 
   //if ( db->pErr->z )   
   //   printf( "ERROR: %s\n", db->pErr->z ); 
   printf( "%s\n", sql ); 
   printf( "------------------------------------\n" ); 
   
   int cols = sqlite3_column_count( stmt ); 
   
   for ( int i = 0; i <cols; i++ )   
      printf( "%s\t", sqlite3_column_name( stmt, i ) ); 
   printf("\n"); 
   printf( "------------------------------------\n" ); 
   do  {   
      rc = sqlite3_step( stmt );   
      if ( rc == SQLITE_ROW ) {     
         int cols = sqlite3_column_count( stmt );     
         for ( int i = 0; i <cols; i++ ) {       
            if ( i> 0 )         
               printf( "  " );       
            printf( (char * )sqlite3_column_text( stmt, i ) );
         }     
         printf( "\n" );   
      } 
   } while( rc == SQLITE_ROW ); 
   printf( "------------------------------------\n\n" ); 
   sqlite3_finalize( stmt );
}
 
int main(int argc, char * argv[])
{ 
   remove( "test.db" ); 
   sqlite3 * db; 
   sqlite3_open( "test.db", &db ); 
   /*SELECT P.Name, S.NameFROM S INNER JOIN (P INNER JOIN PS ON P.PID = PS.PID)
      ON S.SID = PS.SIDWHERE (((S.Name)=''));*/
   r( db, "DROP TABLE P" ); 
   r( db, "DROP TABLE G" ); 
   r( db, "DROP TABLE S" ); 
   r( db, "DROP TABLE PG" ); 
   r( db, "DROP TABLE PS" ); 
   r( db, "CREATE TABLE P ( PID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
             Name STRING UNIQUE);" ); 
   r( db, "CREATE UNIQUE INDEX PIndex ON P (Name);" ); 
   r( db, "CREATE TABLE G ( GID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, Name
            STRING UNIQUE);" ); 
   r( db, "CREATE UNIQUE INDEX GIndex ON G (Name);" ); 
   r( db, "CREATE TABLE S ( SID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, Name
            STRING UNIQUE);" ); 
   r( db, "CREATE UNIQUE INDEX SIndex ON S (Name);" ); 
   r( db, "CREATE TABLE PG ( PID INTEGER, GID INTEGER );" ); 
   r( db, "CREATE UNIQUE INDEX PGIndex ON PG (PID, GID);" ); 
   r( db, "INSERT INTO P VALUES( NULL, 'john' );" ); 
   r( db, "INSERT INTO P VALUES( NULL, 'mike' );" ); 
   r( db, "INSERT INTO P VALUES( NULL, 'joe' );" ); 
   r( db, "INSERT INTO P VALUES( NULL, 'dave' );" ); 
   r( db, "INSERT INTO G VALUES( NULL, 'pool' );" ); 
   r( db, "INSERT INTO G VALUES( NULL, 'home' );" ); 
   r( db, "INSERT INTO G VALUES( NULL, 'city' );" ); 
   r( db, "INSERT INTO G VALUES( NULL, 'woods' );" ); 
   r( db, "INSERT INTO PG VALUES( 2, 3 );" ); 
   r( db, "INSERT INTO PG VALUES( 3, 2 );" ); 
   r( db, "INSERT INTO PG VALUES( 2, 2 );" ); 
   r( db, "INSERT INTO PG VALUES( 3, 3 );" ); 
   d( db, "SELECT * FROM P;" ); 
   d( db, "SELECT * FROM G;" ); 
   d( db, "SELECT * FROM PG;" ); 
   d( db, "SELECT * FROM G NATURAL INNER JOIN PG;" ); 
   d( db, "SELECT * FROM P NATURAL INNER JOIN PG;" ); 
   d( db, "SELECT P.Name as PName, G.Name as GName FROM P NATURAL INNER JOIN PG
             NATURAL INNER JOIN G;" ); 
   
   sqlite3_close( db ); 
   
   return 0;
}
Dotblogs 的標籤: