MySQL Forums
Forum List  »  Connector/C++

not sure why i'm getting this error when trying to connect to a database with C++ and mySQL
Posted by: Cory Sanchez
Date: January 21, 2007 07:21PM

I get the following problems when trying to connect to a database that is locally on my machine...
#include <mysql.h>
#include <winsock>
#include <iostream>
using namespace std;

MYSQL* mysqlConnection = mysql_init(NULL);

if (!mysql_real_connect(mysqlConnection, "localhost", "root", "pw", "tutorialdb", 3306, NULL, 0))
//error, use mysql_error(mysqlConnection)
else
{
if (mysql_query(mysqlConnection,"SELECT * FROM blah") != 0)
//error, use mysql_error(mysqlConnection)
else
{
MYSQL_RES* result = mysql_store_result(mysqlConnection);

MYSQL_ROW row;
// get the rows 1 by 1
while ((row = mysql_fetch_row(result)))
{
// do stuff
// row[0] is the contents of col1 of the current row
// row[1] is the contents of col2 etc
}

mysql_free_result(result);
}

mysql_close(mysqlConnection);
}


The database I created with mySQL 5.0 command line, with the following:
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.27-community-nt

mysql> create database tutorialdb
-> ;
Query OK, 1 row affected (0.00 sec)

mysql> use tutorialdb;
Database changed
mysql> CREATE TABLE player (
-> num integer auto_increment not null,
-> name varchar(100) default '' not null,
-> primary key(num),
-> unique(name)
-> );
Query OK, 0 rows affected (0.09 sec)

mysql>
mysql> CREATE TABLE resource (
-> num integer auto_increment not null,
-> name varchar(100) default '' not null,
-> primary key(num),
-> unique(name)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql>
mysql> CREATE TABLE playerresource (
-> player integer default 0 not null,
-> resource integer default 0 not null,
-> amount integer default 0,
-> primary key(player, resource)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql>


Here are my errors:
Code:
1>------ Build started: Project: sss, Configuration: Debug Win32 ------
1>Compiling...
1>fggf.cpp
1>c:\mysql\include\mysql_com.h(184) : error C2146: syntax error : missing ';' before identifier 'fd'
1>c:\mysql\include\mysql_com.h(184) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\mysql\include\mysql_com.h(184) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\mysql\include\mysql_com.h(354) : error C2065: 'SOCKET' : undeclared identifier
1>c:\mysql\include\mysql_com.h(354) : error C2146: syntax error : missing ')' before identifier 's'
1>c:\mysql\include\mysql_com.h(355) : error C2059: syntax error : ')'
1>.\fggf.cpp(2) : fatal error C1083: Cannot open include file: 'winsock': No such file or directory
1>Build log was saved at "file://c:\Documents and Settings\Mr. Coffee\My Documents\Visual Studio 2005\Projects\sss\sss\Debug\BuildLog.htm"
1>sss - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any ideas waht i'm doing wrong? they are all coming from a mySQL library named,mysql_com.h

Thanks.
i'm using Visual Studio 2005



Edited 1 time(s). Last edit at 01/21/2007 09:34PM by Cory Sanchez.

Options: ReplyQuote


Subject
Views
Written By
Posted
not sure why i'm getting this error when trying to connect to a database with C++ and mySQL
6015
January 21, 2007 07:21PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.