Having the support of a database within an application helps to store the data, and keep them in order to consult effectively.
SQLite ( http://www.sqlite.org/ ) is a relational database that is within applications and allows you to do everything that usually require a traditional database is divided into client and server (see MySQL, PostgreeSQL).
For now I tested the integration with only two languages, C + + / Qt4 and Java.
In both cases the results were excellent and the integration was very rapid.
QT4
Qt4 provides a library to interact with the db. Here is an excerpt of code that allows you to measure the running time of two simple queries.
int main (int argc, char * argv []) {
QTime t;
t.start ();
QSqlDatabase QSqlDatabase db =:: addDatabase (QSQLITE ");
db.setDatabaseName (qt.db ");
db.open ();
QSqlQuery * query = new QSqlQuery("insert into T1 values (1,'Insert from program.');",db);
int result = 1;
result = (query->exec()) ? 0 : -1;
query->prepare("SELECT * FROM T1;");
query->exec();
while(query->next())
{
QString line = query->value(0).toString() + ","+query->value(1).toString();
QString deb = "Linea trovata: " + line;
qDebug(deb.toStdString().c_str());
}
int elapsed = t.elapsed();
return elapsed;}
By running the program on my laptop the running time was 3 milliseconds. A good result I think.
Java
Here is a small example of Java code that does the same thing.
public static void main (String [] args) throws SQLException {try {
Class.forName ("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection ("jdbc: sqlite: test . db ");
Statement stat = conn.createStatement ();
stat.executeUpdate (" insert into T1 values (1,'Insert from program.');");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Con il la parte di codice scritta in Java non ho eseguito dei test di performance ma ha garantito una portabilità incredibile visto che il file .jar contiene al suo interno la libreria per interagire con il database.
Questo è solo un primo articolo che vuole fornire lo spunto a tutti quelli che verranno. Spero di essere stato utile.
Alla prossima.
0 comments:
Post a Comment