MySQL++ get data from table

hi,

I have trouble with getting info from my table, this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdafx.h"
#include <iostream>
#include <mysql++.h>
#include <manip.h> 
using namespace std;

#define HOST ""
#define USER "senergy"
#define PASSWORD ""
#define DATABASE ""
#define PORT 3306

// deleted info :)

int main(int argc, char *argv[])
{
	try
	{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		mysqlpp::Query query = connection.query("SELECT * FROM accounts");
		mysqlpp::StoreQueryResult result = query.store();
		cout << result[0]["login"];
	}
	catch(mysqlpp::Exception e){
		cerr << "problem: " << e.what() << endl;
		return -1;
	}
	cin.get();
	return 0;
}


well, after executing, I'll get this error:

http://filebeam.com/e5275cc9d8a9738d7f6e672692cca1ae.jpg

and if I use
"SELECT login FROM accounts"

and

cout << result[0] I'll get 1 as output, not my login username, without any error, any suggestions?
Last edited on
It sounds like your query is not returning any results. You could check that using something like this:
1
2
3
		cout << "Number of records: " << result.size() << '\n';
		for(size_t i = 0; i < result.size(); ++i)
			cout << result[i]["id"] << '\n';
yea, it has 0 results, so, what can I do? my table is NOT empty, I have 2 accounts there (done by me)
Perhaps you didn't select a database?
1
2
3
4
5
#define HOST ""
#define USER "senergy"
#define PASSWORD "" 
#define DATABASE "" // database name here?
#define PORT 3306 
Last edited on
yes I did, I'm using almost same code to create accounts
Well your code seems to work for me. Can you post your code for creating the accounts and the database schema? You can get the database schema using:

mysqldump -d -u user -p database > database-schema.sql
I'm using windows so I'll post my accounts.sql file instead of that one from mysqldump (I think they are same)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int CreateAccount(HWND hWnd, char cUsername[16], char cPassword[16], char cEmail[32])
{
	std::string queryy;
	queryy += "INSERT INTO accounts(acct, login, password, gm, banned, email, banreason) VALUES(LAST_INSERT_ID(), '";
	queryy += cUsername;
	queryy += "', '";
	queryy += cPassword;
	queryy += "', '0', '0', '";
	queryy += cEmail;
	queryy += "', '0')";
	try{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		mysqlpp::Query query = connection.query(queryy.c_str());
		mysqlpp::SimpleResult result = query.execute();
	}
	catch(mysqlpp::Exception e){
		return -1;
	}
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
DROP TABLE IF EXISTS `accounts`;
CREATE TABLE `accounts` (
  `acct` int(10) unsigned NOT NULL auto_increment COMMENT 'Unique ID',
  `login` varchar(32) collate utf8_unicode_ci NOT NULL COMMENT 'Login username',
  `password` varchar(32) collate utf8_unicode_ci NOT NULL COMMENT 'Login password',
  `gm` varchar(32) collate utf8_unicode_ci NOT NULL default '' COMMENT 'Game permissions',
  `banned` int(10) unsigned NOT NULL,
  `email` varchar(32) collate utf8_unicode_ci NOT NULL COMMENT 'contact address',
  `banreason` varchar(255) collate utf8_unicode_ci default NULL,
  PRIMARY KEY  (`acct`),
  UNIQUE KEY `a` (`login`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Account Information';


+ how can I make email unique key too? like login, so there won't be 2 same email, is it possible in table or I need to make check function in program?
Well your code works fine for me. I don't use windows so I made this test program from a slight modification of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <iostream>
#include <mysql++.h>
#include <manip.h>

using namespace std;

#define HOST "localhost"
#define USER "test"
#define PASSWORD "test"
#define DATABASE "test"
#define PORT 3306

int CreateAccount(char cUsername[16], char cPassword[16], char cEmail[32])
{
	std::string queryy;
	queryy += "INSERT INTO accounts(acct, login, password, gm, banned, email, banreason) VALUES(LAST_INSERT_ID(), '";
	queryy += cUsername;
	queryy += "', '";
	queryy += cPassword;
	queryy += "', '0', '0', '";
	queryy += cEmail;
	queryy += "', '0')";
	try{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		mysqlpp::Query query = connection.query(queryy.c_str());
		mysqlpp::SimpleResult result = query.execute();
	}
	catch(mysqlpp::Exception& e){
		return -1;
	}
	return 0;
}

int main()
{
	CreateAccount("wibble", "wobble", "wibble@wobble.com");

	try
	{
		mysqlpp::Connection connection(DATABASE, HOST, USER, PASSWORD, PORT);
		mysqlpp::Query query = connection.query("SELECT * FROM accounts");
		mysqlpp::StoreQueryResult result = query.store();

		cout << "Number of records: " << result.size() << '\n';
		cout << result[0]["login"];
	}
	catch(mysqlpp::Exception& e){
		cerr << "problem: " << e.what() << endl;
		return -1;
	}

	return 0;
}
Number of records: 1
wibble


EDIT: To make email unique then you can set it unique like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
DROP TABLE IF EXISTS `accounts`;
CREATE TABLE `accounts` (
  `acct` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique ID',
  `login` varchar(32) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Login username',
  `password` varchar(32) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Login password',
  `gm` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Game permissions',
  `banned` int(10) unsigned NOT NULL,
  `email` varchar(32) COLLATE utf8_unicode_ci NOT NULL COMMENT 'contact address',
  `banreason` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`acct`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `a` (`login`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Account Information';



Last edited on
edit123712: nvm, everything is working now :)
Last edited on
Topic archived. No new replies allowed.