We will be using the database to persist employee data.
This article will be an step by step guide on how to use a SQLite database with a Xamarin Forms application. It is the best suited relational database for mobile applications as it has very small footprint. The most commonly used relational database with Xamarin Forms is SQLite. The type of database to use mainly depends upon the requirement of the application, but in most of MIS (Management Information Systems) based application relational databases are used for this purpose. All you need to do before running this example is to create your SQLite database and write the valid path to it in many mobile applications it’s a requirement to store data locally and for that purpose we use databases with the applications.
You can find the whole source code on my github. To remove all persons, just run: QSqlQuery query Query.prepare("SELECT name FROM people WHERE name = (:name)") QString name = query.value(idName).toString() Ĭheck if person with specific name exists: QSqlQuery query Int idName = query.record().indexOf("name") Print all persons: QSqlQuery query("SELECT * FROM people") Query.prepare("DELETE FROM people WHERE name = (:name)") Rules of creating queries are simple, so I think you already know how removing person looks like: if (personExists(name)) QSqlQuery::lastError() is very useful, you should not skip it in your implementation. Query.prepare("INSERT INTO people (name) VALUES (:name)") you should check if args are ok first. bool DbManager::addPerson(const QString& name)
After prepare call bindValue to fill the placeholders with proper values. QDebug() << "Error: connection with database fail" įor managing people resources we need the possibility to:Īctions can be implemented in following way: define QSqlQuery, then call prepare method - it can be raw SQL query string, or it can contain placeholders for variables - placeholders must begin with colon. M_db = QSqlDatabase::addDatabase("QSQLITE") DbManager::DbManager(const QString& path) The path argument value is the path to your database file. The constructor of DbManager sets up the database connection and opens it. Now you can create a class responsible for database access. In the Additional Dependencies add Qt5Sqld.lib for Debug and Qt5Sql.lib for release. In VS linker settings is located in Properties/Configuration properties/Linker/Input. You need to do it if your compilation fails with unresolved external symbol error. If you don’t use QtCreator but another IDE, for example Visual Studio, your project may need setting linker dependency to QtSql.lib. It will link your project against QtSql module. Access database from Qt applicationĬreate new Qt project. Now we can create C++ application to play with it. Sqlite> CREATE TABLE people(ids integer primary key, name text) Ĭongrats, your database is ready.
To leave the sqlite console just type “.quit”. It cointains a key (integer) and name (text). With sqlite you can create such a simple database with two console commands. Let’s assume, for simplicity, that we need to store people data - only name and corresponding id. Read more about sql drivers… Design and create the database
Error should be followed by a console output that informs you which drivers you have installed: “QSqlDatabase: available drivers:”.
If you face problems like “QSqlDatabase: QSQLITE driver not loaded” you need to install drivers manually. Depending on your distribution, you can have the drivers installed by default with your Qt or not. Qt requires drivers to deal with SQL databases. I have chosen SQLite engine because it’s the easiest engine to set up (it requires no server, no configuration…), still it’s suitable for the most of possible applications. Here is some short example that presents how to do it. Accessing SQL databases from C++ applications is very simple with Qt library.