GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Apr-2004, 21:03
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

urgent help needed :c + mysql insert


Hi
Below is a program that i've written doing an insertion in mysql. It inserts Bob,bibo,bb,yes. My problem is suppose i've to insert a value not directly i.e. to insert the value return by a function. E.g. If the function is returning the name,
x=name();
and i have to insert, the value of x or the value return by the function name() in the database, how do i do it?

Thanx
jack


CPP / C++ / C Code:

#include <stdio.h>
#include <stdlib.h>

#include "mysql.h"

MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

void exiterr(int exitcode)
{
    fprintf( stderr, "%s\n", mysql_error(&mysql) );
    exit( exitcode );
}

int main()
{
    //uint i = 0;

    char *username, *passwd, *query, *temp, *query2;
    my_ulonglong  i;


    query = "INSERT INTO schema VALUES ('Bob','bibo','bb','yes')";
    query2 = "SELECT username,password FROM Users";

    //temp = (char *)calloc(strlen(test) + strlen(query) + 2, sizeof(char));

    mysql_init(&mysql);
    //mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"myapp2");
    if (!mysql_real_connect(&mysql,"localhost","root","jacky","Passwd",0,NULL,0))
    {
        fprintf(stderr, "Failed to connect to database: Error: %s\n",
        mysql_error(&mysql));
    }

    //Doing the insert
    if (mysql_query(&mysql, query))
        exiterr(3);

    //Doing a select
    if (mysql_query(&mysql, query2))
        exiterr(3);

    if (!(res = mysql_store_result(&mysql)))
        exiterr(4);

    i = mysql_num_rows(res);

    printf ("Number of rows =  %lu\n", i);

    while((row = mysql_fetch_row(res))) {
        //for (i=0 ; i < mysql_num_fields(res); i++)
            //printf("%s\t",row[i]);
	printf("Username : %s, Password : %s\n",row[0], row[1]);
    }

    if (!mysql_eof(res))
        exiterr(5);

    mysql_free_result(res);
    mysql_close(&mysql);
}

Last edited by dsmith : 13-Apr-2004 at 21:09. Reason: Use [c] & [/c] for syntax highlighting
  #2  
Old 13-Apr-2004, 21:16
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello again Jack.

I have never used mysql in C programming so there may be a better answer, but you could use sprintf to create your query string. So using your code, it could be changed to:
CPP / C++ / C Code:
sprintf(query,"INSERT INTO schema VALUES (%s,%s,%s,'yes')",x,y,z);
 
This will create a query string into query substituting in for x,y and z just like printf. You do need to make sure that your query string is big enough to hold all the information though.

Like I said, there may be a easier or better way to do this, but I see no reason why this wouldn't work.

Hope this helps,
d
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP, MySQL, WML skyloon MySQL / PHP Forum 0 05-Mar-2004 07:53
A problem Between MySQL <> phpBB mirable MySQL / PHP Forum 3 10-Sep-2003 05:31
select & delete mysql queries skyloon MySQL / PHP Forum 1 22-Jul-2003 13:26
MySQL Syntax Error DropZite MySQL / PHP Forum 3 09-Jul-2003 04:00
Windows: From only £20p/y,Linux: from $10p/m. ASP, ASP.NET, PHP, Free MySQL, +More EyotaHosts Web Hosting Advertisements & Offers 0 28-Jun-2003 13:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.