![]() |
Database »
Database »
General
Intermediate
License: The BSD License
Another Embedded Database For C++By Vijay Mathew PandyalakalA C++ wrapper to SQLite database |
SQL, VC6, Windows, SQL-Server, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Here is another wrapper for the famous SQLite database engine. Yes, I know about CppSQLite. It is an excellent wrapper for SQLite. I do not claim my code is superior to CppSQLite in some way. But if you already know the JDBC API, then you will find my library really easy and straightforward to use.
Let us jump right into some example snippets that will help you start using the classes right away. To create or open a database, use the Connection class' open() function.
Connection conn; conn.open("mydb"); // the argument is the database name.
To execute SQL commands, get a handle to the Statement object. Call the execute() function to execute all SQL commands except SELECT. The execute() function will return the number of rows affected by the query.
Statement* stmt = conn.createStatement(); stmt->execute("create table albums(title,artist)"); // creates a table stmt->execute( "insert into albums values('Brothers in Arms','Dire Straits')"); // insert some data stmt->execute("insert into albums values('Unplugged','Eric Clapton')");
To execute a SELECT statement, use the executeQuery() function. This function returns a handle to a ResultSet. use the next() function to navigate through the ResultSet. The ResultSet class has a number of getXXX() functions that take the number of the column as it's argument and return that column's value as the specified type. The different getXXX() functions are:
std::string getString(int colNum)
int getInt(int colNum)
long getLong(int colNum)
unsigned int getUInt(int colNum)
unsigned long getULong(int colNum)
float getFloat(int colNum)
double getDouble(int colNum)
bool getBoolean(int colNum) The ResultSet has an object of ResultSetMetaData embedded with in it. We can get a handle to this object with a call to getMetaData(). The ResultSetMetaData contains information like the number of columns in the ResultSet, their names and data types.
ResultSet* rslt = stmt->executeQuery("select * from albums"); ResultSetMetaData* r_mtdt = rslt->getMetaData(); int cols = r_mtdt->getColumnCount(); while(rslt->next()) { for(int i=0;i<cols;i++) { printf("%s (%s): %s ",r_mtdt->getColumnName(i+1).c_str(), r_mtdt->getColumnType(i+1).c_str(),rslt->getString(i+1).c_str()); } printf("\n"); }
The DatabaseMetaData class contains the meta data of the entire database. We can obtain a DatabaseMetaData handle by calling the getDatabaseMetaData() function of the Connection class.
Call the refreshMetaData() function first, if you want the latest database meta data.
conn.refreshMetaData(); if(conn.getDatabaseMetaData()->doesObjectExist("albums","table")) // check to see if a table exists stmt->execute("drop table albums");// delete it if it exists
The DatabaseMetaData contains the following information:
We can use the doesObjectExist() function to check if an object exists in the database. The first argument of this function is the name of the object and the second argument is it's type. This can be either "table" or "index".
SQLException
close() function on the Connection object explicitly. All resources are automatically deleted by the library when the Connection object's destructor is executed.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Jan 2007 Editor: Chris Maunder |
Copyright 2004 by Vijay Mathew Pandyalakal Everything else Copyright © CodeProject, 1999-2010 Web10 | Advertise on the Code Project |