Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

SQLite-Tabelle auslesen und als Liste speichern

Empfohlene Antworten

Veröffentlicht

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

Benutz nicht char* und char[], wenn du nicht genau weißt, was du tust. Dein Code hat da mehrere Probleme.

Die Zeiger, die dir sqlite3_column_text liefert, sind nur bis zum nächsten Aufruf von sqlite3_step oder sqlite3_finalize gültig. Wenn du die Strings darüber hinaus speichern willst, musst du sie kopieren.

Arrays kann man nicht mit = zuweisen. Das hast du vermutlich gemerkt, daher stammt wohl dieses Konstrukt:

*Country::iso3166alpha2 = *iso3166alpha2;

Damit kopierst du aber nur das erste Zeichen.

Wenn du char*/char[] benutzt, musst du sicherstellen, dass deine Strings nullterminiert sind, und dass auch genügend Platz für eine Nullterminierung da ist. In iso3166alpha2 beispielsweise ist nur Platz für 1 Zeichen + Terminierung.

Warum benutzt du nicht std::string?

  • Autor

Super, einfach std::string benutzen klappt, danke. Nur, damit ich dass dann auch weiß: Hätte ich das mit char* lösen wollen, hätte es dann gereicht, statt


*Country::iso3166alpha2 = *iso3166alpha2; 

das zu tun:

strcpy(iso3166alpha2, Country::iso3166alpha2);

?

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.