Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Database~
~ C++ ~
~Contact~

SQL / C++ Tutorial using sqlwrapped

5. Adding data

PreviousNextIndex To execute an sql query of any kind, the Query class method execute() is used. The most common situations where execute() is used is to add / modify / remove data from the database. The return value of execute() is true if the query was successful, and false if the query failed. From now on examples are no longer shown for both flavors of databases. Please see earlier example (1 and 2) for the difference between MySQL and sqlite3 wrapped. After successfully running example3, go back to example 2 to verify that the data has been stored in the database. example
int main() { Database db("localhost", "root", "", "tutorialdb"); // MySQL specific if (!db.Connected()) { printf("Database not connected - exiting\n"); exit(-1); } Query q(db); q.execute("insert into player(name) values('Anders')"); q.execute("insert into player(name) values('Grymse')"); }
example3_mysql.cppexample3_sqlite3.cpp Using mysqldump to verify the data
$ mysqldump tutorialdb player -- -- Table structure for table Player -- CREATE TABLE player ( num int(11) NOT NULL auto_increment, name varchar(100) NOT NULL default '', PRIMARY KEY (num), UNIQUE KEY name (name) ) TYPE=MyISAM; -- -- Dumping data for table Player -- INSERT INTO player VALUES (1,'Anders'); INSERT INTO player VALUES (2,'Grymse'); $
Using sqlite3 to verify the data
$ sqlite3 tutorial.db .d CREATE TABLE player ( num integer primary key autoincrement not null, name varchar(100) default '' not null, unique(name) ); INSERT INTO "player" VALUES(1, 'Anders'); INSERT INTO "player" VALUES(2, 'Grymse'); $

PreviousNext
Page, code, and content Copyright (C) 2021 by Anders Hedström