Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Small code for inserting....

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
sureshp
Asked:
sureshp
  • 4
  • 2
1 Solution
 
gj62Commented:
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
 
gj62Commented:
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
 
sureshpAuthor Commented:
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
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!

 
gj62Commented:
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
 
sureshpAuthor Commented:
Thanks for your help.
Could you pls post a complete code here?
Regards
0
 
gj62Commented:
I need to know a bit more about exactly what you want to do to post complete code...

0

Featured Post

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.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now