SqliteManager | Sqlite Manager is a Dev Debug tool | Database library
kandi X-RAY | SqliteManager Summary
Support
Quality
Security
License
Reuse
- Display the edit row dialog .
- Add a row to the table .
- Populate database with test data .
- Create table .
- Translate characters from a string into a character sequence .
- Replaces the search string with the given replacement string .
- Sets the data to the columns .
- Initializes the view .
- Executes a query .
- Get an in - memory AppDatabase instance .
SqliteManager Key Features
SqliteManager Examples and Code Snippets
public class HelperSqliteDataRetriever implements SqliteDataRetriever { SqliteHelper mSqliteHelper; SQLiteDatabase mSQLiteDatabase; HelperSqliteDataRetriever(SqliteHelper sqliteHelper) { mSqliteHelper = sqliteHelper; mSQLiteDatabase = mSqliteHelper.getWritableDatabase(); } @Override public Cursor rawQuery(@NonNull String query, String[] selectionArgs) { if (mSQLiteDatabase == null || !mSQLiteDatabase.isOpen()) { mSQLiteDatabase = mSqliteHelper.getWritableDatabase(); } return mSQLiteDatabase.rawQuery(query, selectionArgs); } @Override public String getDatabaseName() { return mSqliteHelper.getDatabaseName(); } @Override public void freeResources() { // not good practice to open multiple database connections and close every time } }
SqliteManager.launchSqliteManager(this, new HelperSqliteDataRetriever(sqliteHelper), null);
Sqlite Manager library for Android Copyright (c) 2016 Ashok Varma (http://ashokvarma.me/). Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Trending Discussions on SqliteManager
Trending Discussions on SqliteManager
QUESTION
I am willing to store objects in a database. The purpose is to be able to read / write these objects with the program. The requirements are the following:
- Objects can be complex using Qt classes such as
QList
,QString
... or even can contain other objects that useQObjects
- The database should not be readable or modified by human (no text file, and if I use sqlite database, it has to be encrypted in a way)
- I should be able to remove, read an object by its name and count the number of objects in the database, without loading everything in the memory
I asked a question here, to do this with a QDataStream
with a minimalist example. But it seems it is not the best way to proceed. Would you have some suggestions regarding the solutions that exist for this purpose?
I have tried the following but it does not fulfill the requirements:
- Storing text in sqlite with
QtSQL
: but the data is accessible by using sqlitemanager for example, can be modified or removed by humans. Moreover, I have no idea regarding the way to storeQList
for example or other objects that I created and containQObject
(for example, 2QList
) - Storing binary data using
QDataStream
: in this case, I cannot count the number of objects in my file, neither read or remove a specific object without loading the entire file in memory.
I would be grateful if you could give me some suggestions or provide example, even if the example is minimalist.
ANSWER
Answered 2020-Feb-13 at 01:00I finally found a solution, especially thanks to Igor Tandetnik and thanks to the topic here
I haven't quite finalized, there is a small imperfection because I have to define an object of my user class that I don't use in order to call the readFromDB function to generate my object from the db.
On the other hand, I get this error message "QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed" each time I call my database.
Anyway, it's a bit late now, and I think it might help some people so I post this minimalist imperfect code below. I'll post an update in the next few days.
Thanks again.
#include "QString"
#include "QFile"
#include "QDataStream"
#include "qdebug.h"
#include "QtSql"
#include "QSqlDatabase"
#include "qmessagebox.h"
class User
{
protected:
QString name;
QList childrens;
public:
QString getName(){ return name;}
QList getChildrens(){ return childrens;}
void setName(QString x) {name = x;}
void setChildrens(QList x) {childrens = x;}
friend QDataStream &operator<<(QDataStream &out, const User &t)
{
out << t.name << t.childrens;
return out;
}
friend QDataStream &operator>>(QDataStream &in, User &t)
{
QString inname;
QList inchildrens;
in >> inname >> inchildrens;
t.name = inname;
t.childrens = inchildrens;
return in;
}
QByteArray object2blob( const User& user )
{
QByteArray result;
QDataStream bWrite( &result, QIODevice::WriteOnly );
bWrite << user;
return result;
}
User blob2object( const QByteArray& buffer )
{
User result;
QDataStream bRead( buffer );
bRead >> result;
return result;
}
int saveToDB( const User& user )
{
QSqlDatabase myDB = QSqlDatabase::addDatabase("QSQLITE");
myDB.setDatabaseName( "file.db");
if (!myDB.open())
{
qDebug()<<"Failed to open SQL database of registered users";
}
else
{
qDebug()<<"Successfully opening SQL database of registered users";
QSqlQuery query;
query.prepare( "CREATE TABLE IF NOT EXISTS users (name TEXT, childrens BLOB)" );
if( !query.exec() )
{
qDebug() << query.lastError();
}
else
{
qDebug() << "Table created!";
query.prepare( "INSERT INTO users (name,childrens) VALUES (:name,:childrens)" );
query.bindValue(":name", name);
query.bindValue( ":childrens", object2blob(user) );
query.exec();
}
query.clear();
myDB.close();
}
QSqlDatabase::removeDatabase("UserConnection");
return 0;
}
User readFromDB( QString name )
{
User result;
QSqlDatabase myDB = QSqlDatabase::addDatabase("QSQLITE");
myDB.setDatabaseName( "file.db");
if (!myDB.open())
{
qDebug()<<"Failed to open SQL database of registered users";
}
else
{
QSqlQuery query;
query.prepare( "SELECT * FROM users WHERE name ='"+ name +"'" );
//query.bindValue( 0, name );
if ( query.exec() && query.next() ) {
result = blob2object( query.value( 1 ).toByteArray() );
}
query.clear();
myDB.close();
}
QSqlDatabase::removeDatabase("UserConnection");
qDebug()<()<<"Jeanne"<<"Jean");
u.saveToDB(u);
User v;
v.setName("Alex");
v.setChildrens(QList()<<"Matthew");
v.saveToDB(v);
User w;
w.setName("Mario");
w.saveToDB(w);
User to_read; //to improve here
User a = to_read.readFromDB("Georges");
qDebug()<
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SqliteManager
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page