C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 30th, 2009, 12:24 PM
czildren czildren is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 3 czildren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 27 sec
Reputation Power: 0
Libpq and PGconn struct

Hi guys

I am using libpq for connecting to PostgreSQL Database.

But I cant find the description of this PGconn struct anywhere. Also in Postgres's documentation, there is nothing said about it.

Best wishes

Reply With Quote
  #2  
Old April 30th, 2009, 12:34 PM
your_friend your_friend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 34 your_friend Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 11 h 2 m 5 sec
Reputation Power: 0
PGconn is defined by a typedef; search the header files for the
structure it's defined to be.

Here it is....
libpq-fe.h has typedefed pg_conn as PGConn

Code:
typedef struct pg_conn PGconn;


Check the below link for definition of struct pg_conn

http://doxygen.postgresql.org/libpq-int_8h-source.html

Reply With Quote
  #3  
Old May 5th, 2009, 01:48 AM
czildren czildren is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 3 czildren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 27 sec
Reputation Power: 0
hi guys
What I am trying to do is to make some queries to postgresql database using libpq, PQexec funcion.

generally there is no problem if I am using standard SQL query.
char* query="INSERT INTO students(id,name, age) values(2,'Chris', 23)";
PQexec(query);

But I need to make some values in this query such as name and age to be variables. Program should ask about name and age first and then prepare a statement and send it.

Suppose if I write this:
char *query="INSERT INTO student(id,name,age) values(2,";
char *name="Chris";
char *end="')";

Is there any function which allows me to concatenate those two or more strings in one string and then make a query to database? Is there any function which converts integer to string?

Best wishes

Reply With Quote
  #4  
Old May 7th, 2009, 11:56 AM
your_friend your_friend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 34 your_friend Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 11 h 2 m 5 sec
Reputation Power: 0
Try this code

Code:

#define MAX_QUERY_LEN   3000
char *query;
char *name="Chris";
int    age = 25;
char *end="')";

query = (char*)malloc(MAX_QUERY_LEN * sizeof(char));
sprintf(query,"INSERT INTO student(id,name,age) values(2,'%s',%d )", name, age);


Character field should be within single quotes.

Reply With Quote
  #5  
Old May 7th, 2009, 12:15 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 1 m 11 sec
Reputation Power: 4080
It is better to use snprintf() instead of sprintf(). That way you can control how much is written to the string and also detect if it would have overflowed the string.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #6  
Old May 10th, 2009, 02:07 AM
czildren czildren is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 3 czildren User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 27 sec
Reputation Power: 0
hi guys

That's working, but when I try to use scanf("%s",&name) it doesnt work.
char * query;
char * name;
scanf("%s",&name);
sprinf(query, "SELECT * FROM '%s'",name);

I've got a memory protect error.

Thanks in advance

Reply With Quote
  #7  
Old May 10th, 2009, 07:40 AM
your_friend your_friend is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 34 your_friend Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 11 h 2 m 5 sec
Reputation Power: 0
czildren,

query and name variables are pointers and hence they need to allocated enough memory before using them.
Code:
query = (char*)malloc(MAX_QUERY_LEN * sizeof(char));
name = (char*)malloc(MAX_NAME_LEN * sizeof(char));

Reply With Quote
  #8  
Old May 11th, 2009, 12:49 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
I'm concerned here about one last part of this... while all of the examples you gave used constant strings, the question leads me to think that the actual name field would be from user input. Concatenating user data into a SQL query leaves you vulnerable to 'SQL injection', especially if you aren't vetting the data in some way first.

While SQL injection attacks are mostly associated with web-based queries, they can occur any time you paste data from input directly into a query. I recommend using PQexecParams() rather than PQexec(), as it ensures that data entered are passed to the DBMS in such a way that it cannot spoof the query. Better still would be to use PQprepare()
Code:
    PGresult* result = NULL;
/* .. */
    result = PQprepare(dbconn, "AddStudent", "INSERT INTO students(id,name, age) values($1, $2, $3)", 3, NULL);

    if ( /* ...results are valid... */ )  
    {
        /* ... prepare the array of parameter values... */
        PQexecPrepared(dbconn, "AddStudent", 3, params, plens, pformats, rformat);
    }

This is only a general outline; you'd have to work out how you would need to use it. If nothing else, you can avoid most of the business of string concatenation.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : June 7th, 2009 at 12:26 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Libpq and PGconn struct

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap