Creation SQLITE database
-
Hello everyone
I'm working on a program and I'm stuck on something I do not understand. I would like the user to create his own database SQLITE (SQLITE: All his information in a file without server) by clicking on a button (bttncreate in the code) but I do not know how to write the code for that.
Here is a picture of the table and the headers
Some details to understand the code.
One button: bttncreate
A SQLITE database connected to the table at the top.Here are the codes
Fencene.h#include <QObject> #include <QWidget> #include <QGridLayout> #include <QComboBox> #include <QPushButton> #include <QFormLayout> #include <QTextEdit> #include <QSpinBox> #include <QGroupBox> #include <QRadioButton> #include <QtSql/QSqlDatabase> #include <QMessageBox> #include <QtSql/QSqlTableModel> namespace Ui { class FenCene; } class FenCene : public QWidget { Q_OBJECT public: explicit FenCene(QWidget *parent = nullptr); ~FenCene(); private: Ui::FenCene *ui; QSqlDatabase *db; //for my database. QSqlTableModel *model; //join table and database QPushButton* bttoncreate; private slots: void newfiledb(); // It's a simple slot that I have not connected to a signal. But I would //like it to be connected to the signal "bttncreate" of nouveaudialogue.ui. }; #endif // FENCENE_H
Fencene.cpp
#include "fencene.h" #include "ui_fencene.h" FenCene::FenCene(QWidget *parent) : QWidget(parent), ui(new Ui::FenCene) { ui->setupUi(this); bttoncreate = new QPushButton("Cahier Electronique de Note de L'Enseignant Principal (CEN_EP)"); bttoncreate->setFont(QFont("Times New Roman", 20)); bttoncreate->setCursor(Qt::PointingHandCursor); connect(bttoncreate, &QPushButton::clicked, this, &FenCene::newfiledb); } FenCene::~FenCene() { delete ui; } void FenCene::newfiledb() // SLot no connected.... { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("......"); //Each User give an name at database db.setHostName("local"); //i don't know if it's necessary if(!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), qApp->tr("La connexion n'est pas correctement établis à la base de donnée" "Reprenez le processus.\n\n" "Cliquer sur annuler pour quitter"); return false ; //i don't know if it's necessary } QSqlQuery query; //Student Only query.exec("create table student (ID int, " "Nomprenoms varchar(100), " "DateDeNaissance varchar(20), " "Sexe varcher (1)" "Varchar(20), " "matiere(20)," "notei1 int, " "notei2 int, " "notei3 int, " "notei4 int, " "notei5 int, " "noted1 int, " "noted2 int, " "Comment bool (15)))"); // I did not put Moy Interro, Moy Sem and Moy Coef into the database. // Because you told me that the table will manage the calculation and // that it is useless to store them in the database but I do not know if it // is the exact formula. //table student + table class + Table subject + table note = student record model = new QSqlTableModel; model->setTable("Student"); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->select(); model->removeColumn(0); model->setHeaderData(0, Qt::Horizontal, "Nom(s) & Prénom(s)"); model->setHeaderData(1, Qt::Horizontal, "Sexe"); model->setHeaderData(2, Qt::Horizontal, "Statut"); model->setHeaderData(3, Qt::Horizontal, "Coef"); model->setHeaderData(4, Qt::Horizontal, "1è EF"); model->setHeaderData(5, Qt::Horizontal, "2è EF"); model->setHeaderData(6, Qt::Horizontal, "3è EF"); model->setHeaderData(7, Qt::Horizontal, "4è EF"); model->setHeaderData(8, Qt::Horizontal, "5è EF"); model->setHeaderData(9, Qt::Horizontal, "Moy Interro"); //No Include in database model->setHeaderData(10, Qt::Horizontal, "1è Dev"); model->setHeaderData(11, Qt::Horizontal, "2è Dev"); model->setHeaderData(12, Qt::Horizontal, "Moy Sem"); // No Include in database model->setHeaderData(13, Qt::Horizontal, "Moy Coef"); // No Include in database model->setHeaderData(14, Qt::Horizontal, "Mentions"); QTableView *view = new QTableView; view->setModel(model); QVBoxLayout *v = new QVBoxLayout; v->addWidget(view); return true; }
Please help me for the coding.
I am available for all questions.
Thank you in advance -
@Nafab213
Glancing, code looks about right. This looks more like a question about how to create a widget than anything else. At the moment, you do not see any button, right? You have not put your button on the page. Start by just changing to:bttoncreate = new QPushButton("Cahier Electronique de Note de L'Enseignant Principal (CEN_EP)", this);
Now you can see & click it, right? Then in due course do it properly with layouts.
db.setDatabaseName("......");
Replace the dots with the filename/path you want.
-
-
@Nafab213
I'm not going to write the code for you (perhaps someone else will), but prompt the user for the desired filename innewfiledb()
, or something similar.If you have read the code .. You will see that everything is not correct.
Since you seem to know this, would you like to provide a hint as to what is wrong or an error message or something, or would you like us to figure it out ourselves as an exercise? https://stackoverflow.com/questions/27844759/how-to-create-a-sqlite-database-in-qt gives sample code, yours looked vaguely along the same lines to me....
-
Thanks....
I do not ask you to write me the code but I want to know how to proceed.
Qaund I'm talking about problem, I think first on the board.
I know it's encouraging laziness if you write it to me but I've already read dozens of courses and tried to write the beginning of the code. When I read them, I do not really understand the courses when I see practical examples on projects and that's how I wrote the first part of my code.
If you've got me an identical project you've worked on or something else, I'll take some examples.
There are cells that are merging or split but I do not know how to put it in the code. Also the five empty lines of the table will be shown in the table but I do not know how to do it.
It's just an intuition of realism. If I do not decipher it, it will not appear but I want to know how to do it. -
@Nafab213
I'm sorry but I simply don't happen to have code written which will do what you want. Perhaps someone else will, so I will not comment after this.But I will say that others will find it difficult when your questions keep changing completely. First off I believe your push button was simply not being displayed, so I don't know what you were doing. Then you said you wanted to know how to create a database in SQLite. But now you are saying what you want to know are questions about cells merging, empty lines etc., which is simply in complete contradiction to not being able to connect to a database.
So I don't intend to be rude, but I think you should consider what your actual question is, for others to answer.
-
@JonB said in Creation SQLITE database:
I'm sorry but I simply don't happen to have code written which will do what you want. Perhaps someone else will, so I will not comment after this.
But I will say that others will find it difficult when your questions keep changing completely. First off I believe your push button was simply not being displayed, so I don't know what you were doing. Then you said you wanted to know how to create a database in SQLite. But now you are saying what you want to know are questions about cells merging, empty lines etc., which is simply in complete contradiction to not being able to connect to a database.
So I don't intend to be rude, but I think you should consider what your actual question is, for others to answer.Excuse me then ..
Let's focus on the SQLITE database.
Look at the code of the database.
Is this correct? -
@Nafab213
No, because I spot"Sexe varcher (1)"
But then, politely, I didn't realise I was responsible for spotting spelling mistakes, having to look at every single character in what you have typed.
And then you also have:
"Varchar(20), " "matiere(20),"
which are both obviously wrong. And nobody could possibly write this correctly for you, because they won't know what you actually want.
If you are going to have this level of problem, I would suggest you get hold of whatever equivalent of MySQL Workbench that SQLite provides (I know it provides something similar) and get the SQL code working there each time, before you copy it into your Qt code. You will get much better help on mistakes etc. interactively in a tool like that than direct from Qt code. This is not an "annoyance" for you, it looks like you really do need to do that in order to get anywhere productively.
-
I understood
For the "Sex Varcher (1)" because I would like to store a single letter in it.
either M for Male or F for Female.
It's good as that ?For
"Varchar (20),"
"Material (20),"
It's a mistake I wanted to write
"matiere Varchar(20), "I already corrected in the file.
Other errors?
Thank you.... -
@Nafab213
Hi
For
db.setDatabaseName("......"); //Each User give an name at database
you can use https://doc.qt.io/qt-5/qinputdialog.htmlbool ok; QString text = QInputDialog::getText(this, tr("Add New Database"), tr("database name:"), QLineEdit::Normal, "default", &ok); if (ok && !text.isEmpty()) databasename=text; // ....
Also this tool is super for inspecting the database and learn about it
https://sqlitebrowser.org/ -
For the "Sex Varcher (1)" because I would like to store a single letter in it.
No, because I said you have mis-spelt it. You need to look carefully at how you spell words, that is an extremely common part of programming for which you are really responsible. That's why I said you would really benefit from doing this in an interactive tool, not in code. Please do yourself a favor and download @mrjj 's link to https://sqlitebrowser.org/, believe me it's going to be worth it for you.
-
I received your answer and I will read the links afterwards.
So for my base ..
Do you have any advice to give me on my database?
Look at this part of my code ..if(!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), qApp->tr("La connexion n'est pas correctement établis à la base de donnée" "Reprenez le processus.\n\n" "Cliquer sur annuler pour quitter"); return false ; //i don't know if it's necessary }
It's necessary ?
Thanks -
@Nafab213
Hi, i talk about the return statement inif(!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), qApp->tr("La connexion n'est pas correctement établis à la base de donnée" "Reprenez le processus.\n\n" "Cliquer sur annuler pour quitter"); return false ; //i don't know if it's necessary -> should just be return, since parent function has void as return. }