Ejemplo n.º 1
0
/*******************************
*
* Henrique Salvadori Coelho
* [email protected]
* hcoelho.com
*
********************************
*
* databaseGetPrimary()
*
* Gets an available primary key
*
* char [] databaseFile - Name of the database file
*
* Returns a valid primary key (long long)
*/
long long databaseGetPrimary ( char * databaseFile )
{
  long long keyToTest = 1;
  long long keyInDatabase = 0;
  int foundCurrentKey = 0;

  // Connecting
  FILE * databaseConnection = NULL;
  databaseConnection = databaseConnect ( databaseFile, DBMODE_R );

  // Looping until the function finds a valid key and calls the return
  do
  {

    // Looping all the registers looking for the key
    while ( databaseReadLine ( databaseConnection, "k", &keyInDatabase ) == 1 && foundCurrentKey != 1 )
    {
      // The key was found, therefore, it's not valid
      if ( keyInDatabase == keyToTest ) foundCurrentKey = 1;
    }

    // If the current key was found, jumps to the next key and resets the current key controller
    if ( foundCurrentKey == 1 )
    {
      keyToTest++;
      foundCurrentKey = 0;
    }

    // Did not find the key, it is valid
    else
    {
      databaseClose ( databaseConnection );

      return keyToTest;
    }

    databaseRewind ( databaseConnection );
  }
  while ( 1 );
}
Ejemplo n.º 2
0
void databaseDelete(struct connection *conn,int id){
struct address addr = {.id=id,.set=0};
conn->db->rows[id] = addr;
}
void databaseList(struct connection *conn){
 for(int i=0;i<MAX_ROWS;i++){
  struct address *addp = conn->db->rows[i];
  if(addp->set) addressPrint(addp);
}
}
int main(int argc,char * argv[])
{
 if(argc<3) die("use it right dumass :/  <exec> <dbfile> <action> [action parameters]");

 char *filename = argv[1];
 char *action = argv[2][0];
 struct connection *conn = databaseOpen(filename,action);
 if(argc>3) int id = atoi(argv[3]);if(id>MAX_ROWS) die("limit the rowcount dammit");
 
 switch(action){
 case 'c':databaseCreate(conn);
          databasewrite(conn);
          break;
 case 'g':if(argc<4) die("you forgot the id moron.");
          databaseGet(conn,id);break;
 case 's':if(argc<6) die("can't add a jhon doe. try to remember next time.");
          databaseSet(conn,id,argv[4],argv[5]);
          databaseWrite(conn);
          break;
 case 'd':if(argc<4) die("for the love of god..delete WHAT?");
          databaseDelete(conn,id);
          databaseWrite(conn);
          break;
 case 'l':databaseList(conn);break;
 default: die("choose from the following cause i aint taking nothing else.\n c-create\ng-get\ns-set\nd-delete\nl-list");
}
databaseClose(conn);
return 0;
}
Ejemplo n.º 3
0
int main ( int argc, char * argv [] )
{
  int       id;
  char      field1 [30];
  char      field2 [30];
  char      field3 [30];
  long long field4;
  double    field5;
  int       control;
  char      databaseLineStructure [1024];
  char      database [] = "sampleDatabase.db";
  int       primaryKey;
  char      arrayField1 [30] [200];
  char      arrayField2 [30] [200];
  char      arrayField3 [30] [200];

  // Connecting
  FILE * sampleDatabase = NULL;
  sampleDatabase = databaseConnect ( database, DBMODE_R );

  // Setting the path for the program
  //databaseGetFullPath ( SCRIPT_PATH, argv[0] );

  printf ( "%s", SCRIPT_PATH );

  printf ( "========================================\n\n" );

  // Showing the number of lines
  printf ( "The number of lines is: \n" );
  printf ( "%d\n\n", databaseNumberEntries ( database ) );


  // Showing the first line
  printf ( "The first line is: \n" );
  databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 );
  printf ( "%d, %s, %s, %s, %lld, %lf\n\n", id, field1, field2, field3, field4, field5 );


  // Skipping lines
  printf ( "Skipping 23 lines\n\n" );
  databaseSkipLines ( sampleDatabase, 23 );


  // Showing the next line
  printf ( "The 25th line is: \n" );
  databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 );
  printf ( "%d, %s, %s, %s, %lld, %lf\n\n", id, field1, field2, field3, field4, field5 );


  // Dumping contents, rewinding, dumping again and rewinding again
  printf ( "Dumping the rest of the content on the screen:\n" );
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    printf("%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5);
  }

  printf ( "\nRewinding the database\n\n" );
  databaseRewind ( sampleDatabase );

  printf ( "Dumping all the content on the screen:\n" );
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    printf( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
  }

  printf ( "\nRewinding the database again\n\n" );
  databaseRewind ( sampleDatabase );



  printf ( "The fields of the registry with id = 7 is:\n" );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5) == 1 && control == 0)
    if ( id == 7 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  if ( control == 0 ) printf ( "No results found!\n" );


  printf ( "\n\nThe fields of the registry with id = 99 is:\n" );
  databaseRewind ( sampleDatabase );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 && control == 0 )
    if ( id == 99 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  if ( control == 0 ) printf ( "No results found!\n" );


  printf ( "\n\nThe fields of the registry with field4 = 8 are:\n" );
  databaseRewind ( sampleDatabase );
  control = 0;
  while ( databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1 )
  {
    if ( field4 == 8 )
    {
      printf ( "%d, %s, %s, %s, %lld, %lf\n", id, field1, field2, field3, field4, field5 );
      control = 1;
    }
  }
  if ( control == 0 ) printf ( "No results found!\n" );


  // Closing the 'read' database
  // This is necessary because we are going to use the file to write instead of reading
  printf ( "\nClosing the database\n\n" );
  databaseClose ( sampleDatabase );

  // Looking for a primary key that is not occupied in field 1
  primaryKey = databaseGetPrimary ( database );

  // Writing a new line in the database
  printf ( "\nWriting a line in the database\n\n" );
  databaseWriteLine ( database, "dsssdf", primaryKey, "New field 1", "New field 2", "New field 3", 8ll, 3.5 );


  // Opening the database to read again
  printf ( "\nOpening the database to read\n\n" );
  sampleDatabase = databaseConnect ( "sampleDatabase.db", DBMODE_R );


  printf ( "Recording the content in arrays\n" );
  for ( control = 0; databaseReadLine ( sampleDatabase, "dsssdf", &id, field1, field2, field3, &field4, &field5 ) == 1; control++ )
  {
    strcpy ( arrayField1 [ control ], field1 );
    strcpy ( arrayField2 [ control ], field2 );
    strcpy ( arrayField3 [ control ], field3 );
  }

  printf ( "The 2nd field of the 16th position is: %s\n\n", arrayField2 [15] );

  databaseClose ( sampleDatabase );


  // Updating a field
  printf ( "Updating a line by ID\n\n" );
  databaseUpdateLine ( database, "sssdf", 26, "Field1_Updated!", "Field2_Updated!", "Field3_Updated!", 90ll, 87.678 );


  // Deleting a field
  printf("Deleting a line by ID\n\n");
  databaseDeleteLine ( database, 27 );
}