(solved)Database in c++ and exposing in qml
-
Hi, I'm working on a listview. I have created a database in javascript to enable persistent storage. But I've noticed that the performances aren't very goos as the app takes long time to storage simple data. So i decided to make the database in pure C++, but i don't know how to do it (note that I am a beginner), I've searched and googled very much but I only find static databases and models that can't be edited directly from qml.
Can anyone help me?Update : i've created the following database but it creates many errors like this : undefined refernce to...
@#include "bookmarks.h"
#include "QDateTime"
#include "QtSql/QSqlQuery"
Bookmarks::Bookmarks(QSqlDatabase database,QObject *parent) :
QSqlTableModel(parent, database)
{
connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SIGNAL(countChanged()));
setEditStrategy(OnManualSubmit);
}
void Bookmarks::generateRoleNames()
{
for (int i = 0; i < this->columnCount(); i++)
m_roles[Qt::UserRole + i] = this->headerData(i, Qt::Horizontal).toByteArray();setRoleNames(m_roles);
}
QString Bookmarks::tableCreateQuery() const
{
return QLatin1String("CREATE TABLE IF NOT EXISTS bookmarks (id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR, url VARCHAR, dateAdded DATE, thumbnail BLOB DEFAULT NULL);");
}void Bookmarks::setFilter(const QString& filter)
{
QSqlTableModel::setFilter(filter);
}bool Bookmarks::select()
{
return QSqlTableModel::select();
}void Bookmarks::insert(const QString& name, const QString& url)
{
QModelIndex index = QModelIndex();
beginInsertRows(index, rowCount(index), rowCount(index));
QDateTime dateTime = QDateTime::currentDateTime();
QString dateTimeString = dateTime.toString("hh_mm_ss yyyy_MMM_dd");
QSqlQuery sqlQuery;
sqlQuery.prepare("INSERT INTO bookmarks (name, url, dateAdded) VALUES (?, ?, ?)");
sqlQuery.addBindValue(name);
sqlQuery.addBindValue(url);
sqlQuery.addBindValue(dateTimeString);
sqlQuery.exec();
select();
endInsertRows();
submitAll();
}void Bookmarks::remove(const QString& url)
{
if (!contains(url))
return;QSqlQuery sqlQuery; sqlQuery.prepare("SELECT id FROM bookmarks WHERE url = ?"); sqlQuery.addBindValue(url); sqlQuery.exec(); sqlQuery.first(); int indexToDelete = -1; for (int row = 0; row < rowCount(); ++row) { if (createIndex(row, 0).data(Qt::DisplayRole).toInt() == sqlQuery.value(0).toInt()) { indexToDelete = row; break; } } if (indexToDelete >= 0) { beginRemoveRows(createIndex(indexToDelete, 0), indexToDelete, indexToDelete); removeRow(indexToDelete); submitAll(); endRemoveRows(); }
}
void Bookmarks::togglePin(const QString& url)
{
if (contains(url))
remove(url);
else
insert(url, url);
}void Bookmarks::update(int index, const QString& name, const QString& url)
{
QModelIndex nameIndex = createIndex(index, 1);
QModelIndex urlIndex = createIndex(index, 2);
QModelIndex dateAddedIndex = createIndex(index, 3);
setData(nameIndex, name);
setData(urlIndex, url);
setData(dateAddedIndex, QDateTime::currentDateTime);
emit dataChanged(nameIndex, dateAddedIndex);
submitAll();
}bool Bookmarks::contains(const QString& url)
{
QSqlQuery sqlQuery;
sqlQuery.prepare(QLatin1String("SELECT id FROM bookmarks WHERE url = ?"));
sqlQuery.addBindValue(url);
sqlQuery.exec();
return sqlQuery.first();
}@ -
Are you using QtQuick 2.0? If so, perhaps the "local storage api":http://doc-snapshot.qt-project.org/5.0/qtquick-modelviewsdata-localstorage.html will help.
As for the errors you're seeing, they are link time errors, I presume? That usually means you haven't implemented some of the functions specified in the class declaration.
Cheers,
Chris. -
It is not id,but ld.
Ld is the standard GNU linker program.
It used to link yout program with libraries it uses.
If you use QML then you need the declarative libraries.
If you use QSockets then you need networking librararies,etc.
The most probable reason for your problem is that your project file does
not contain references to Qt sql database libs.
Open your project file (the one ending in .pro) and find the starting with QT
at the end of the line add sql
So it looks like :
QT += networking sql (NOTE: This is an example your line does not have to be exactly like this one)
If you cannot find such line then add it like this
QT += sql
This was taken from http://stackoverflow.com/questions/3315833/how-can-i-add-the-qtsql-library-in-qt-creater
Because you are a beginner,I would advise reading some articles about C++ linking.
Google for c++ linkers help.