Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. int insert() {
  2.     // Connection stuff
  3.     const char  *conninfo;
  4.     PGconn      *conn;
  5.     PGresult    *res;
  6.    
  7.     // User data
  8.     char *name;
  9.     char *last_p;
  10.     char *last_m;
  11.     char *birth;
  12.     int *phone;
  13.    
  14.     // Query parameters
  15.     int         paramFormats[5];
  16.     int         paramLengths[5];
  17.     const char  *userData[5];
  18.    
  19.     // Try to connect to the server
  20.     conninfo = "host=internal-db.s13296.gridserver.com user=db13296 password=valgrind17 dbname=db13296_tarea";
  21.     conn = PQconnectdb( conninfo );
  22.     if( PQstatus( conn ) != CONNECTION_OK ) {
  23.         fprintf( stderr, "Error de conexion con la base de datos: %s", PQerrorMessage( conn ) );
  24.         PQfinish( conn );
  25.         exit( 1 );
  26.     }
  27.    
  28.     // Get the user data from standard input
  29.     printf( "Nombre: " );
  30.     scanf( "%s", &name );
  31.     printf( "Apellido Paterno: " );
  32.     scanf( "%s", &last_p );
  33.     printf( "Apellido Materno: " );
  34.     scanf( "%s", &last_m );
  35.     printf( "Fecha de Nacimiento ( yyyy-mm-dd ): " );
  36.     scanf( "%s", &birth );
  37.     printf( "Numero de Telefono: " );
  38.     scanf( "%d", &phone );
  39.    
  40.     // Convert all to binary
  41.     userData[0] = name;
  42.     userData[1] = last_p;
  43.     userData[2] = last_m;
  44.     userData[3] = birth;
  45.     userData[4] = (char *) htonl( (uint32_t) phone );
  46.     paramFormats[0] = 0;
  47.     paramFormats[1] = 0;
  48.     paramFormats[2] = 0;
  49.     paramFormats[3] = 0;
  50.     paramFormats[4] = 1; // Binary format for the integer parameter
  51.     paramLengths[0] = sizeof( name );
  52.     paramLengths[1] = sizeof( last_p );
  53.     paramLengths[2] = sizeof( last_m );
  54.     paramLengths[3] = sizeof( birth );
  55.     paramLengths[4] = sizeof( phone );
  56.    
  57.    
  58.     // Insert it on the database
  59.     res = PQexecParams( conn,
  60.                 "INSERT INTO directorio (nombre, apPaterno, apMaterno, festejo, telefono) VALUES ($1, $2, $3, $4, $5::bigint )",
  61.                 5,       /* params */
  62.                 NULL,    /* let the backend deduce param type */
  63.                 userData,
  64.                 paramLengths,
  65.                 paramFormats,
  66.                 0 );
  67.     if( PQresultStatus( res ) != PGRES_COMMAND_OK ) {
  68.         fprintf( stderr, "Error al guardar el registro: %s", PQerrorMessage( conn ) );
  69.         PQclear( res );
  70.         PQfinish( conn );
  71.         exit( 1 );
  72.     } else {
  73.         printf( "\n-> El registro fue creado exitosamente\n" );
  74.         PQclear( res );
  75.         PQfinish( conn );
  76.         return 0;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement