?
Solved

Small code for inserting....

Posted on 2003-03-06
6
Medium Priority
?
215 Views
Last Modified: 2010-04-15
Hi all,
This is a simple request.
I need a small program in C, which cam be used to insert values in the table. The database is used for this is MySQL and run this program under Linux.

So i request you to post a small C code here.

Say database name : test
table one :- fields name, age

Thanks in advance.
0
Comment
Question by:sureshp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8083053
Here's how to connect:

MYSQL mysql;

mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
    fprintf(stderr, "Failed to connect to database: Error: %s\n",
          mysql_error(&mysql));
}
0
 
LVL 6

Expert Comment

by:gj62
ID: 8083070
For more, the C API code is distributed with MySQL. It is included in the mysqlclient library and allows C programs to access a database.

Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. You can find these in the clients directory in the MySQL source distribution.

You can then adapt an example, easier than us trying to guess exactly what you need.

You can also look at the C API online at:

http://www.mysql.com/doc/en/C.html
0
 
LVL 1

Author Comment

by:sureshp
ID: 8089226
gj62,

Thanks for your response.
I am new to this area. I didn't write any c program using a database connection.
So could you please post a small program (full code) that can be used to insert some values into a table. It will be more useful for me to follow.
Thanks for your help.
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
gj62 earned 400 total points
ID: 8089394
After you connect, select db:

mysql_select_db(&mysql, "DB_NAME");

Run query:

retrun=mysql_query(&mysql, "UPDATE DB_NAME FIELDS (name,age) VALUES ('My Name','My age') WHERE custid=1");
0
 
LVL 1

Author Comment

by:sureshp
ID: 8096519
Thanks for your help.
Could you pls post a complete code here?
Regards
0
 
LVL 6

Expert Comment

by:gj62
ID: 8096537
I need to know a bit more about exactly what you want to do to post complete code...

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question