Hallo,
ich bin noch relativ neu in der CPP-Programmierung (habe eigentlich Java gelernt) und komme bei folgendem Problem einfach nicht weiter:
Ich habe eine SQLite-Tabelle mit Ländern, ihren ISO-Codes und Namen. Diese ist so aufgebaut worden:
CREATE TABLE Country (
Country_ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
'ISO-3166-Alpha-3' VARCHAR(3) NOT NULL,
'ISO-3166-Alpha-2' VARCHAR(2) NOT NULL,
Name VARCHAR(50)
);
Ein Datensatz daraus soll durch folgende Klasse repräsentiert werden:
//Country.h
#ifndef _COUNTRY_H_
#define _COUNTRY_H_
class Country
{
private:
char iso3166alpha2[2], iso3166alpha3[3];
char *name;
int countryId;
public:
Country();
Country(int countryId, char iso3166alpha2[2], char iso3166alpha3[3], char *name);
~Country();
char *getISO3166Alpha2();
char *getISO3166Alpha3();
char *getName();
int getCountryId();
};
#endif
// Country.cpp
#include "Country.h"
Country::Country(int countryId, char iso3166alpha2[2], char iso3166alpha3[3], char *name)
{
Country::countryId = countryId;
*Country::iso3166alpha2 = *iso3166alpha2;
*Country::iso3166alpha3 = *iso3166alpha3;
Country::name = name;
}
Country::~Country()
{
}
int Country::getCountryId()
{
return countryId;
}
char *Country::getISO3166Alpha2()
{
return iso3166alpha2;
}
char *Country::getISO3166Alpha3()
{
return iso3166alpha3;
}
char *Country::getName()
{
return name;
}
Es soll eine Liste angelegt werden, die sämtliche Länder enthält. Das habe ich wie folgt versucht (Auszug):
std::list<Country> countryList;
std::list<Country>::iterator position = countryList.begin();
if (sqlite3_prepare_v2(database, "SELECT * FROM Country", -1, &statement, 0) == SQLITE_OK)
while (sqlite3_step(statement) == SQLITE_ROW)
{
Country country((int)sqlite3_column_int(statement, 0), (char*)sqlite3_column_text(statement, 1), (char*)sqlite3_column_text(statement, 2), (char*)sqlite3_column_text(statement, 3));
std::cout << country.getName() << std::endl; // 1. Ausgabe
countryList.push_back(country);
}
sqlite3_finalize(statement);
for (Country c : countryList)
std::cout << c.getName() << std::endl; // 2. Ausgabe
Beide Ausgaben sollten jetzt doch eigentlich die Ländernamen angeben. Bei der ersten Ausgabe funktioniert das auch, also kann ja beim Auslesen der Daten eigentlich nichts schiefgegangen sein, oder?
Bei der zweiten Ausgabe bekomme ich dann sowas hier: XÿV (Bei allen Zeilen gleich).
Ich hätte jetzt gedacht, dass es was mit der Zeichenkodierung zu tun hat, aber dann würde die erste Ausgabe doch nicht klappen!?
Ich hoffe, ihr könnt mir helfen.
Gruß
PL1994