database issue
-
i created a project in qt,i used sqlite database ,when i run its working fine
my project code:
QSqlDatabase mydb;void connClose()//closeing the connection and also removing default connections if any database is connected. { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); } bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not. { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("/home/nikhil/digilogic.db"); if(!mydb.open()){ qDebug()<<("failed to open the database"); return false; } else{ qDebug()<<("connected..."); return true; } }
my problem is when i deploy my project and give to user with database file and setting database file location its working ,but if i did'nt set the location its not working because path is not correct ,now what i'm requesting to help me is please tell me the code which sets default location in user pc and access database,please somebody help ,thanks in advance.
-
@nikhilsarma said in database issue:
now what i'm requesting to help me is please tell me the code which sets default location in user pc and access database,please somebody help
Use https://doc.qt.io/qt-5/qstandardpaths.html to get the path to the location/folder where you want to store the database file.
-
@nikhilsarma Lets say you want to store the database file in user documents folder (C:/Users/<USER>/Documents on Windows) then you do:
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db"; mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName(dbFilePath);
-
@jsulm sir thank you very much for the above code ,i have one more question ,if i send my database file to user and he will save it somewhere in his pc ,i want my program to search for that database file in user pc and use it,is it possible sir?
-
@nikhilsarma said in database issue:
is it possible sir?
Yes, it is. Use https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName for that.
-
@nikhilsarma If you open the link I gave you you will see code example. What else do you need?
-
sir i am using like this but its opening files folder not opening database file and search data for
user login credentials,please sir give me a sample code to open database file and search data in it,please help me sir.QSqlDatabase mydb;
void connClose()//closeing the connection and also removing default connections if any database is connected. { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); } bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not. {
// QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
// QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";
// mydb=QSqlDatabase::addDatabase("QSQLITE");
// mydb.setDatabaseName(dbFilePath);// mydb=QSqlDatabase::addDatabase("QSQLITE"); //mydb.setDatabaseName("/home/nikhil/digilogic.db");
// QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
// "/home/nikhil/digilogic.db",
// tr("digilogic.db"));
QStringList files = QFileDialog::getOpenFileNames(
this,
"digilogic.db",
"/home",
".db");if(!mydb.open()){ qDebug()<<("failed to open the database"); return false; } else{ qDebug()<<("connected..."); return true; }
-
@nikhilsarma said in database issue:
QFileDialog::getOpenFileNames
This is not what I suggested to use. Use https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
And where in your code do you set the db file in mydb?! -
@jsulm said in database issue:
QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";May I suggest a more robust version:
QString path =QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); QString dbFilePath = path + QDir::separator() + "digilogic.db";
this guarantees a path where your app can read and write to :D
and the recommended enum to use would be
QStandardPaths::AppDataLocation
for read/write data only your application is intended to use -
@jsulm sir,i am very new to qt ,i didnt have any training in it ,but i managed to do alot of programming in it ,but i got stuck in this paths concept please give me a solution,
this is my code of loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files ,what i am requesting you to help me is without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem and give me some code,thank you very much for your help till now ,please give me some solution.
class loginwindow : public QMainWindow
{
Q_OBJECT
public:QSqlDatabase mydb; void connClose()//closeing the connection and also removing default connections if any database is connected. { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); } bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not. { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("/home/nikhil/digilogic.db"); if(!mydb.open()){ qDebug()<<("failed to open the database"); return false; } else{ qDebug()<<("connected..."); return true; } }
public:
loginwindow(QWidget *parent = nullptr);
~loginwindow();private slots:
void on_userlogin_clicked();void on_signupbutton_clicked();
private:
Ui::loginwindow *ui;
};#endif // LOGINWINDOW_H
-
@nikhilsarma said in database issue:
without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem
I understood your problem already before and told you what you can use to let the user search for database file.
Now you post code without my suggestion! Why? Do you want me to write the code for you? I will do it this time, but I really hope you will start to at least try to do what others suggest...bool connOpen(const QString &dbFileName) { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName(dbFileName); ... // Somewhere else (I don't know where you want to do this, up to you QString dbFileName = QFileDialog::getOpenFileName( this, tr("Open File"), "/home", tr("Database (*.db)"); if (!dbFileName.isNull()) loginWindowInstance.cannOpen(dbFileName);
-
@nikhilsarma said in database issue:
// Somewhere else (I don't know where you want to do this, up to you) this part is confuseing me
I cannot see what can be confusing. Put the code @jsulm gave you to open the database wherever it is that ... you want to open the database.
-
Thank you sir,for your response ,what i am trying to do is ,i created a loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files those are funtions are connOpen and connClose ,
class loginwindow : public QMainWindow
{
Q_OBJECT
public:QSqlDatabase mydb; QString dbFileName = QFileDialog::getOpenFileName( this, tr("Open File"), "/home", tr("Database (*.db)")); bool connOpen(const QString &dbFileName) { mydb=QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName(dbFileName); if (!dbFileName.isNull()) loginwindowInstance.connOpen(dbFileName);// i am getting error as (use of underdeclared identifier loginwindowinstance ) if(!mydb.open()){ qDebug()<<("failed to open the database"); return false; } else{ qDebug()<<("connected..."); return true; } } void connClose()//closeing the connection and also removing default connections if any database is connected. { mydb.close(); mydb.removeDatabase(QSqlDatabase::defaultConnection); }
so if i wanted to use those functions in another cpp files i am doing like this
void employeeinfo::on_delete_2_clicked() { loginwindow conn; username=ui->Username_Employinfo->text(); if(!conn.connOpen()){ qDebug()<<"Failed to open the database"; return; } conn.connOpen();//this function will open your connection QSqlQuery qry; qry.prepare("Delete from employinfo where username='"+username+"'"); if(qry.exec( )) { QMessageBox::information(this,tr("Delete"),tr("Deleted")); conn.connClose(); } else { QMessageBox::critical(this,tr("error::"),qry.lastError().text()); }
}
Creating object of loginwindow class and using to open database,i want to open and close database only through loginwindow class ,so that i will create object of that class and use in other files.
i hope u understand my problem sir.
-
@nikhilsarma said in database issue:
class loginwindow : public QMainWindow
{
Q_OBJECT
public:
QSqlDatabase mydb;QString dbFileName = QFileDialog::getOpenFileName(
this,
tr("Open File"),
"/home",
tr("Database (*.db)"));I suggest you learn C++ basics as this code is invalid.
You need to use QFileDialog::getOpenFileName when you want to ask user to select the database file.
Since its your app I don't know when user should select the database file, this is something you have to think about... -
@jsulm sir,I know all cpp concepts,but I think QFileDialog concept is of qt’s cpp ,which is were I am faceing difficulty to understand,
Sir one last help I need from you is, what all concepts I need to learn to access or configure database file’s or any kind of file’s from anywhere in my PC and in user PC also ,thanks ones again sir for replying to post.
-
Hi,
@nikhilsarma said in database issue:
class loginwindow : public QMainWindow
{
Q_OBJECT
public:
QSqlDatabase mydb;QString dbFileName = QFileDialog::getOpenFileName(
this,
tr("Open File"),
"/home",
tr("Database (*.db)"));bool connOpen(const QString &dbFileName)
{getOpenFileName is a static method. You can't use it in such a variable declaration.
As @jsulm already wrote, mydb should not exist in the first place as explained in the QSqlDatabase documentation.
-
@nikhilsarma said in database issue:
,I know all cpp concepts
Then why do you write invalid code? It doesn' tmatter that QFileDialog is a class from Qt, it could be any class.
This code is invalid because you most certainly do not want to show the file dialog when you create an instance of that class:class loginwindow : public QMainWindow { Q_OBJECT public: QSqlDatabase mydb; // Bellow line is invalid QString dbFileName = QFileDialog::getOpenFileName( this, tr("Open File"), "/home", tr("Database (*.db)"));
And did you actually think about what you want to do? Don't you think you should call QFileDialog::getOpenFileName when you want to ask user to select the db file?
And I repeat it once more: do NOT store database connections in member variables! mydb member is not needed...