Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 29-Feb-12 0:45am
v2
Comments
Member 13703826 26-Nov-22 14:54pm    
I tried this code with visual studio 2017 and got unresolved external symbol for mysql_init, mysql_real_connect, mysql_error,mysql_query. Checked the mysql.h and VS2017 prompted for definition.
Is there any who knows a solution to these issues? Thank you in advance.

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

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


Change your query to below:

SQL
INSERT into `vcs`.`users` (`id`, `fname`, `sname`) VALUES ('3', 'rana', 'sameer')
 
Share this answer
 
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.
 
Share this answer
 
Comments
prince_rumeel 29-Feb-12 8:41am    
bro it has been done...............
I'd try to pass connection and not &mysql to the mysql_query function.

Best regards
Espen Harlinn
 
Share this answer
 
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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900