NewsGroup .... gmane.comp.db.sqlite.general
  Subject ...... Loading Data
  Date ......... 2011-07-28
  Post_By ...... Daniel Spain
  Edit_By ...... Stanley C. Kitching

  I added some white space and new-line characters
  to Daniel's original post using a first pass
  through Python and a second pass though vim.
 
  # --------------------------------------------------------
 
  hey all. 
  
  i got my engine to compile so now im doing basic SQL syntax 
  of load the user, since its in a very early production stage 
  the players table only has 4 values to load. 
  
  Table :  Players
  
    UserID varchar( 30 )  /* their constant userid */          
    Race smallint         /* their race number */
    Class smallint        /* their class number */
    Loaded smallint       /* character was loaded ? */    
  
  when a user hits 'P' to play from the mainmenu 
  the program executes loaduser() 
  
  this queries the database and loads their user data, 
  and if it does not exist it will insert an initial record.
  
  i've tried this a bunch of different ways 
  that result in false queries or inserting multiple records 
  with the same username. 
  
  i'll post what im doing and someone can point me 
  in the right directi on


/* snippet from SQLMUD.C */ 

player = &player_data[ usrnum ] ; 

if ( ( margc == 1 )  && ( toupper( margv[ 0 ][ 0 ] ) == 'P' ) ) 
{
  /* menu command to play the game */
 
  loaduser() ; 

  if ( player->loaded )     
  {
    pmlt( "I loaded your character record. \r" ) ; 
  }
  else
  {
    pmlt( "I did not load your character record, 
           creating a new one.\r" ) ;
  }
} 

/* snippet from MUDUSER.C */  

VOIDloaduser( VOID )/* 

load character data */
{
  sqlite3 *database ; 

  sqlite3_stmt *statement ; 

  const CHAR *tail ; 

  INT rc ; 

  usaptr = &usroff( usrnum ) ; 

  /* lets open the database */

  rc = sqlite3_open( "SQLMUD.DB" , &database ) ;
 
  /* error: unable to establish connection */

  if ( rc != SQLITE_OK )
  {
    pmlt( "I was unable to establish a connection 
           to the database! Exiting. \r" ) ; 

    return ; 
   
   }

   /* create a query string */

   sprintf( 
     vdatmp , 
     "select * from Players where UserID = '%s'" , 
      usaptr->userid ) ;
 
   rc = sqlite3_prepare_v2( 
          database , vdatmp , INPSIZ , &statement , &tail ) ;
 
   if( rc != SQLITE_OK )
   {
     // did not query i think here is 
     // where i insert a new character record ?

     pmlt( "Could not query you in the database, 
            i think i need to insert. \r" ) ; 

     sqlie3_close( database ) ;
 
     return ; 
    }

    player->loaded = 0 ; 

    while( sqlite3_step( statement ) == SQLITE_ROW )
    {
      pmlt( "LOAD: USERID = %s \r" , 
             sqlite3_column_text( statement , 0 ) ) ;
 
      pmlt( "LOAD: RACE = %s \r" , 
             sqlite3_column_text( statement , 1 ) ) ;

      pmlt( "LOAD: CLASS = %s \r" , 
             sqlite3_column_text( statement , 2 ) ) ;

      pmlt( "LOAD: LOADED = %s \r" , 
             sqlite3_column_text( statement , 3 ) ) ; 
  
      strncpy( player->userid , 
               sqlite3_column_text( statement , 0 ) , UIDSIZ ) ;

      player->race = atoi( sqlite3_column_text( statement , 1 )) ; 

      player->class = atoi( sqlite3_column_text( statement , 2) ) ; 

      player->loaded = atoi( sqlite3_column_text( statement , 3 ) ) ; 

  } sqlite3_finalize( statement ) ; 

  sqlite3_close( database ) ;
}


  so what im trying to accomplish is : 

  when they hit the menu command 'P' 
  it loads their data.within loaduser() 
  i want it to query the database for them by name.

  if it dont exist create an initial record 
  containing default dataelse load their data.

  the user flag 'loaded' tells the game 
  to either create a newcharacter 
  or enter with the loaded data.  

  i got several other samples 
  i been playing with 
  but ill start with this one 
  to get pointed in the right direction. ; 



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to