1313
1414#include < wx/stdpaths.h>
1515
16+ #include < wx/database/sqlite3/sqlite_database.h>
17+
18+ /* *
19+ * Sample application showing how to use basics of wxDatabase
20+ * To test different databases, you will need to setup appropriate connection using one
21+ * of the method provided at the end of the file. Be sure to enable that specific database
22+ * support via ENABLE_*** CMake directives before build to add support for that database.
23+ * Improvements are welcome as pull requests to https://github.com/mtangoo/wxDatabase
24+ *
25+ */
26+
1627
1728class wxDatabaseApp : public wxAppConsole
1829{
1930 public:
2031 virtual bool OnInit ();
2132 virtual int OnRun ();
2233 private:
23- wxDatabase* GetDatabase ( const wxString& conf = " " );
34+ wxDatabase* GetSQLiteDatabase ( );
2435};
2536
2637wxIMPLEMENT_APP (wxDatabaseApp);
@@ -35,75 +46,20 @@ int wxDatabaseApp::OnRun()
3546 wxDatabase *pDatabase = NULL ;
3647 try
3748 {
38- // opening a database via wxDatabase::GetDatabase() hides all the details of the varied database arguments
39-
40- // calling the sample GetDatabase() with a non-empty "config entry" string also avoids the need for the wxDatabaseApp.conf file
41-
42- // simply comment in one of the GetDatabase() calls below
43-
44- // use the wxDatabaseApp.conf file
45- // pDatabase = GetDatabase();
46-
47- // SQLite3 direct
48- // pDatabase = GetDatabase("[SQLite]\ndatabase=C:/wxDev/sqlite/test.sqlite");
49-
50- // SQLite3 via ODBC
51- // pDatabase = GetDatabase("[ODBC]\nConnection=DRIVER=SQLite3 ODBC Driver;Database=C:/wxDev/sqlite/test.sqlite\nDbType=SQLITE\n");
52-
53- // PostgreSQL
54- // pDatabase = GetDatabase("[PostgreSQL]\nlibrary_location=C:/Program Files/PostgreSQL/8.3/bin\nserver=127.0.0.1\ndatabase=test\nuser=postgres\npassword=jesus\nport=5432");
55-
56- // MySQL
57- // pDatabase = GetDatabase("[MySQL]\nlibrary_location=C:/Program Files/MySQL/MySQL Server 5.1/bin\nserver=127.0.0.1\ndatabase=test\nuser=root\npassword=jesus");
58-
59- // TDS direct
60- // The following lines were added to freetds.conf for server=manyleaves_sqlexpress
61- // [manyleaves_sqlexpress]
62- // host = manyleaves
63- // instance = sqlexpress
64- // # port = 1433
65- // instance and port are mutually exclusive
66- // pDatabase = GetDatabase("[TDS]\nfreetds=C:/wxDev/freetds-1.00.24/freetds.conf\nserver=manyleaves_sqlexpress\ndatabase=test\nuser=\npassword=\nversion=7.1");
67-
68- // TDS via ODBC
69- // pDatabase = GetDatabase("[ODBC]\nConnection=DRIVER=SQL Server;SERVER=manyleaves\\\\sqlexpress;TRUSTED_CONNECTION=Yes;DATABASE=test\nDSN=\nDbType=TDS\n");
70-
71- // TDS via ODBC using MARS
72- // pDatabase = GetDatabase("[ODBC]\nConnection=DRIVER={SQL Server Native Client 10.0};MARS_CONNECTION=Yes;SERVER=manyleaves\\\\sqlexpress;TRUSTED_CONNECTION=Yes;DATABASE=test\nDSN=\nDbType=TDS\n");
73-
74- // MS Access via ODBC
75- // pDatabase = GetDatabase("[ODBC]\nConnection=DRIVER={microsoft access driver (*.mdb)};dbq=C:/wxDev/msaccess/test.mdb;\nDSN=\nDbType=TDS\n");
76-
77- wxFileName f (wxStandardPaths::Get ().GetExecutablePath ());
78- wxString appPath (f.GetPath ());
79-
80- // If it does not work for Windows, Install drivers from http://www.ch-werner.de/sqliteodbc/
81- pDatabase = GetDatabase (" [ODBC]\n Connection=DRIVER=SQLite3 ODBC Driver;Database=" +appPath + wxFileName::GetPathSeparator () + " wxdatabase.db\n DbType=SQLITE\n " );
82-
83- if (!pDatabase) throw (wxDatabaseException (-1 , " Unable to establish connection to a database" ));
84-
49+ wxDatabase *pDatabase = GetSQLiteDatabase (); // Use method appropriate to the database you want to test
8550 // Create table
86- wxString deleteSqlTB = " DROP TABLE Names;" ;
87- try
88- {
89- pDatabase->RunQuery (deleteSqlTB);
90- }
91- catch (wxDatabaseException&)
92- {
93- }
94-
9551 wxString createSqlTB = " CREATE TABLE Names (ID INT PRIMARY KEY NOT NULL, Name VARCHAR(50) NOT NULL);" ;
96- pDatabase->RunQuery (createSqlTB);
52+ pDatabase->RunQuery (createSqlTB);
9753
9854 // insert into table
9955 wxString sqlStefano = " INSERT INTO Names VALUES(1, 'Stefano Mtangoo');" ;
10056 wxString sqlAndrew = " INSERT INTO Names VALUES(2, 'Andrew aka Many Leaves');" ;
10157 wxString sqlUpendo = " INSERT INTO Names VALUES(3, 'Upendo Stefano');" ;
102-
58+
10359 pDatabase->RunQuery (sqlStefano);
10460 pDatabase->RunQuery (sqlAndrew);
10561 pDatabase->RunQuery (sqlUpendo);
106-
62+
10763 // Prepared statement
10864 wxPreparedStatement* pStatement = pDatabase->PrepareStatement (" INSERT INTO Names (ID, Name) VALUES(?, ?);" );
10965 if (pStatement)
@@ -113,7 +69,7 @@ int wxDatabaseApp::OnRun()
11369 pStatement->RunQuery ();
11470 pDatabase->CloseStatement (pStatement);
11571 }
116-
72+
11773 // Select from table
11874 wxDatabaseResultSet *pResults = pDatabase->RunQueryWithResults (" SELECT * FROM Names;" );
11975 if (pResults)
@@ -127,7 +83,11 @@ int wxDatabaseApp::OnRun()
12783 }
12884 pDatabase->CloseResultSet (pResults);
12985 }
130- pDatabase->Close ();
86+
87+ // When done with the datbase connection, close it
88+ pDatabase->Close ();
89+ // Delete Pointer, or use smart pointer for db connection
90+ if (pDatabase) delete pDatabase;
13191 }
13292 catch (wxDatabaseException& e)
13393 {
@@ -138,50 +98,22 @@ int wxDatabaseApp::OnRun()
13898
13999 return e.GetErrorCode ();
140100 }
141- if (pDatabase)
142- delete pDatabase;
143101
144102 return 0 ;
145103}
146104
147- wxDatabase* wxDatabaseApp::GetDatabase (const wxString& configString)
105+ /* *
106+ * Methods below shows how to connect different databases using wxDatabase.
107+ * Just replace GetSQLiteDatabase() in OnRun() with any of these to test specific database
108+ * Remember to enable that specific database in CMake before building to make it available
109+ */
110+ wxDatabase* wxDatabaseApp::GetSQLiteDatabase ()
148111{
149- wxDatabase* pDatabase = NULL ;
150- wxInputStream* configStream = NULL ;
151-
152- if (configString.IsEmpty ())
153- {
154- wxString configPath (" wxDatabaseApp.conf" );
155- if (!wxFileName::FileExists (configPath))
156- {
157- wxPrintf (" Configuration file \" %s\" not found" , configPath);
158- return NULL ;
159- }
160- configStream = new wxFileInputStream (configPath);
161- if (!configStream->IsOk ())
162- {
163- wxPrintf (" Configuration file \" %s\" cannot be opened" , configPath);
164- return NULL ;
165- }
166- }
167- else
168- {
169- configStream = new wxStringInputStream (configString);
170- }
171- wxFileConfig config (*configStream);
172-
173- wxString err = wxEmptyString;
174- // returns the first non-empty entry in the config stream
175- pDatabase = wxDatabase::GetDatabase (config, &err);
176- if (pDatabase == NULL )
177- {
178- wxPrintf (" Cannot establish database connection from %s" , err);
179- return NULL ;
180- }
181-
182- wxPrintf (" Running %s" , pDatabase->GetTypeName ());
183- if (pDatabase->IsViaODBC ()) wxPrintf (" [ODBC]" );
184- wxPrintf (" \n\n " );
185-
186- return pDatabase;
187- }
112+ wxFileName f (wxStandardPaths::Get ().GetExecutablePath ());
113+ wxString dbPath = f.GetPath () + " wxDatabase.db" ;
114+ wxRemoveFile (dbPath);
115+
116+ wxDatabase* pDatabase = new wxSqliteDatabase (dbPath, false );
117+
118+ return pDatabase;
119+ }
0 commit comments