Click here to Skip to main content
12,349,379 members (28,297 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
#include <iostream>
#include <mysql.h>

using namespace std;
 
MYSQL mysql,*connection;
MYSQL_RES result;
MYSQL_ROW row;
 
char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";
 
int query_state;
 
int main (int argc, char **argv)
{
	mysql_init(&mysql);
 
	connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);
 
	if (connection==NULL)
	{
		cout<<mysql_error(&mysql)<<endl;
	}
 
	else
	{
		(mysql_query(&mysql, "INSERT into 'vcs'.'users' ('id', 'fname', 'sname') VALUES ('3', 'rana', 'sameer')"));
		if (query_state !=0) {
		cout << mysql_error(connection) << endl;
		return 1;
		}
	}
	mysql_close(&mysql);
 
}

I am using this code for establish the connection with my phpmyadmin sql
and I get succeed.
but the following data is not entered in my database.
how can I do this???
Posted 29-Feb-12 0:42am
Edited 29-Feb-12 0:45am
Jochen Arndt132.7K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I'd try to pass connection and not &mysql to the mysql_query function.

Best regards
Espen Harlinn
  Permalink  
Comments
CPallini 7-Jan-13 4:06am
   
5.
Espen Harlinn 7-Jan-13 4:11am
   
Thank you, Carlo - My congratulations for making it to the MVP list this year too :-D
CPallini 7-Jan-13 4:24am
   
Well, it looks I am in good company! :-D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I think it's a lack of commit after insertion. Could you try a querry for creating tables (as creatin tables does not require a commit). if it works that mean that u miss to end your code with a commit querry.
  Permalink  
Comments
prince_rumeel 29-Feb-12 8:41am
   
bro it has been done...............
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Your query is wrong, you wrote your query like below:

INSERT into 'vcs'.'users' ('id', 'fname', 'sname') VALUES ('3', 'rana', 'sameer')

Change your query to below:

INSERT into `vcs`.`users` (`id`, `fname`, `sname`) VALUES ('3', 'rana', 'sameer')
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100