już mam coś fajnego
służy do przerzucania do bazy danych z system plików, z tym, że pliki nie są prawdziwe i wszystkie mają wartość określoną przez PLIK
program korzysta z bazy danych podanej tutaj
http://forum.php.pl/viewtopic.php?t=4037Kod
#define PQHOST NULL
#define PQPORT NULL
#define PQOPTIONS NULL
#define PQTTY NULL
#define PQNAME "postgres"
#define PQUSER "php_pl"
#define PQPASS "**************"
#define INSERT_DIR ""
#define NAME "/"
#define PLIK "Plik testowy, który wkładam do bazy za pomocą programu w C"
#define FALSE 0
#define TRUE !FALSE
#include<libpq-fe.h>
#include<unistd.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<errno.h>
#include<fcntl.h>
PGconn *conn;
char *insertid( /* PGconn *conn */ )
//PGconn *conn
{
extern PGconn *conn;
PGresult *res;
res = PQexec( conn, "SELECT last_value FROM categories_id_seq;" );
if( !res || PQresultStatus( res ) != PGRES_TUPLES_OK )
{
puts( "Cannot get ID" );
}
return PQgetvalue( res, 0, 0 );
}
int insertdir( /* PGconn *conn, */ char *path, char *name, char *id )
//PGconn *conn
//const char *path;
//const char *name;
//const char id[20];
{
char *query, *iid, *temppath;
PGresult *res;
extern PGconn *conn;
DIR *dir;
FILE *file;
struct dirent *dirs;
struct stat *status;
if( ( strcmp( name, "." ) == 0 ) || ( strcmp( name, ".." ) == 0 ) )
return TRUE;
temppath = malloc( 200 );
strcpy( temppath, path );
strcat( temppath, ( ( strcmp( path, "/" ) == 0 ) || ( strcmp( path, "" ) == 0 ) )? "": "/" );
strcat( temppath, name );
if( ( dir = opendir( temppath ) ) != NULL )
{
query = malloc( 100 );
printf( "Tworzenie katalogu: %sn", temppath );
strcpy( query, "SELECT mkdir('" );
strcat( query, ( strcmp( name, "" ) == 0 ) ? "root" : name );
strcat( query, "', ");
strcat( query, id );
strcat( query, ");" );
printf( "Wykonywane zapytanie: %sn", query );
res = PQexec( conn, query );
free( query );
if( !res || PQresultStatus( res ) != PGRES_TUPLES_OK )
{
printf( "Błędne zapytanie:%sn", query );
printf( "%sn%sn", PQresultErrorMessage( res ), PQerrorMessage( conn ) );
return FALSE;
}
iid = insertid( conn );
while( ( dirs = readdir( dir ) ) != NULL )
if( insertdir( /* conn, */ temppath, dirs->d_name, iid ) == FALSE )
return FALSE;
free( temppath );
return TRUE;
}
else if( errno == ENOTDIR )
{
query = malloc( 200 );
printf( "Tworzenie pliku: %sn", temppath );
strcpy( query, "SELECT mk('" );
strcat( query, name );
strcat( query, "', " );
strcat( query, id );
strcat( query, ", '" );
strcat( query, PLIK );
strcat( query, "');" );
printf( "Wykonywane zapytanie: %sn", query );
res = PQexec( conn, query );
free( query );
free( temppath );
if( !res || PQresultStatus(res) != PGRES_TUPLES_OK )
{
printf( "Błędne zapytanie:%sn", query );
return FALSE;
}
}
else
return TRUE;
}
int main( void )
{
extern PGconn *conn;
conn = PQsetdbLogin( PQHOST, PQPORT, PQOPTIONS, PQTTY, PQNAME, PQUSER, PQPASS );
PQexec( conn, "BEGIN;" );
printf( "%sn", PQerrorMessage( conn ) );
if( insertdir( /* conn, */ INSERT_DIR, NAME, "1" ) == FALSE )
{
puts( "Inserting Failed!" );
PQexec( conn, "ROLLBACK;" );
PQfinish( conn );
exit( 1 );
}
PQexec( conn, "COMMIT;" );
PQfinish( conn );
puts( "Program succed" );
return 0;
}
jak na razie nie wkłada jeszcze plików tak jakbym chciał, ale nie udało mi się jeszcze zrobić poprawnego ich otwierania i czytania