Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. 需要帮助

需要帮助

Scheduled Pinned Locked Moved Unsolved Chinese
1 Posts 1 Posters 537 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Nafab213N Offline
    Nafab213N Offline
    Nafab213
    wrote on last edited by
    #1

    大家好。
    我是编程的初学者,我目前正在从事一个项目,因为我缺乏知识。
    在我的程序中,我创建了一个数据库,我想将它连接到一个视图(模型/视图)。 我尝试使用QSqlRelationalTableModel,但它没有考虑只有两个表。
    我想请你帮忙写下视线代码。
    我可以提出所有问题。
    fencene.h

    #ifndef FENCENE_H
    #define 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>
    #include <QSqlQuery>
    #include "logindb.h"
    
    namespace Ui {
    class FenCene;
    }
    
    class FenCene : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit FenCene(QWidget *parent = nullptr, LoginDB *login = nullptr);
        ~FenCene();
    
    private:
        Ui::FenCene *ui;
    //    QSqlDatabase *db; //for my database.
        LoginDB *login;
    //    QSqlTableModel *model; //join table and database
        QSqlQueryModel *model;
    
    
    public slots:
        void newfiledb(); // Connected to NouveauDialog in FenPrincipale.cpp
    };
    
    #endif // FENCENE_H
    
    

    fencene.cpp

    #include "fencene.h"
    #include "ui_fencene.h"
    #include <QDir>
    #include <QDebug>
    #include <QSqlRelationalTableModel>
    
    FenCene::FenCene(QWidget *parent, LoginDB *loginDB) :
        QWidget(parent),
        ui(new Ui::FenCene),
        login(loginDB)
    {
        ui->setupUi(this);
    }
    
    
    FenCene::~FenCene()
    {
        delete ui;
    }
    
    void FenCene::newfiledb()
    {
        /*QString path = QDir::homePath() + "/CEN.db";*/ // Home-Directory of User + Name of Database File
        // We maybe can create a subfolder in HomeDir and put .db file there later.
    
        //    qDebug() << path; // Output in Console to check Path to DB-File
    
        if(login->conOpen())
        {
            QSqlQuery query(login->db);
            query.exec("CREATE TABLE IF NOT EXISTS Student "
                       "(id INTEGER PRIMARY KEY, "
                       "name VARCHAR(30), "
                       "prename VARCHAR(50), "
                       "sex VARCHAR(1), "
                       "classID INTEGER, "
                       "FOREIGN KEY(classID) REFERENCES Class)");
            query.exec("INSERT INTO Student VALUES(NULL, 'Name0', 'Prenom0', 'M', 0)");
            query.exec("INSERT INTO Student VALUES(NULL, 'Name1', 'Prenom1', 'F', 0)");
            query.exec("INSERT INTO Student VALUES(NULL, 'Name2', 'Prenom2', 'M', 0)");
    
    
            query.exec("CREATE TABLE IF NOT EXISTS Class "         //The list of classes is in Qnouveaudialogue.ui
                       "(id INTEGER PRIMARY KEY, "                    //The name of a class is: choice (combox2) + choice (combox4) + choice (combox5).
                       "name VARCHAR(20), "          //Example: 6èmeMC1.
                       "classteacher INTEGER, "
                       "FOREIGN KEY(classteacher) REFERENCES Teacher)");
            query.exec("INSERT INTO Class VALUES(NULL, '6èmeMC1', 0)");
            query.exec("INSERT INTO Class VALUES(NULL, '6èmeMC1', 0)");
            query.exec("INSERT INTO Class VALUES(NULL, '6èmeMC1', 1)");
    
    
            query.exec("CREATE TABLE IF NOT EXISTS Teacher"   //I suggested that the teacher's name not be in the database.
                       "(id INTEGER PRIMARY KEY, "    //It's useless but I respect your instructions in the code so I put it in it.
                       "name VARCHAR(30), "
                       "prename VARCHAR(50), "
                       "sex VARCHAR(1))");
            query.exec("INSERT INTO teacher VALUES(NULL, 'NomTeacher0', 'PrenomTeacher0', 'M')");
            query.exec("INSERT INTO teacher VALUES(NULL, 'NomTeacher1', 'PrenomTeacher1', 'M')");
            query.exec("INSERT INTO teacher VALUES(NULL, 'NomTeacher2', 'PrenomTeacher2', 'M')");
    
    
            query.exec("CREATE TABLE IF NOT EXISTS Course" //(0 for Science, 1 for Litterature, 2 for Autres)
                       "(id INTEGER PRIMARY KEY, "
                       "name VARCHAR(30), "
                       "groupID INTEGER, "
                       "teacherID INTEGER, "
                       "FOREIGN KEY(groupID) REFERENCES CourseGroup, "
                       "FOREIGN KEY(teacherID) REFERENCES Teacher)");
            query.exec("INSERT INTO Course VALUES(NULL, 'CommunicationEcrite', 0, 0)"); //The list of course are in the Qnouveaudialogue
            query.exec("INSERT INTO Course VALUES(NULL, 'Lecture', 1, 0)");              //This is the combox 2
            query.exec("INSERT INTO Course VALUES(NULL, 'Anglais', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Espagnol', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Allemand', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Arabe', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Histoiregeographie', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Philosophie', 1, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Mathematiques', 0, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'PCT', 0, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'SVT', 0, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'EPS', 2, 0)");
            query.exec("INSERT INTO Course VALUES(NULL, 'Conduite', 1, 0)");
    
    
            query.exec("CREATE TABLE IF NOT EXISTS Coursegroup"
                       "(id INTEGER PRIMARY KEY, "
                       "name VARCHAR(20))");
            query.exec("INSERT INTO Coursegroup VALUES(NULL, 'Science')");
            query.exec("INSERT INTO Coursegroup VALUES(NULL, 'Littérature')");
            query.exec("INSERT INTO Coursegroup VALUES(NULL, 'Autres')");
    
    
            query.exec("CREATE TABLE IF NOT EXISTS Grade"   // What do I have to put in this table? // This is the table which stores the students' grades
                       "(id INTEGER PRIMARY KEY, "          // Its connected to student (by the student ID) and the course (by the courseID)
                       "studentID INTEGER, "
                       "courseID INTEGER, "
                       "gradeName VARCHAR(10), "          // the "Name" of the grade is for "Notei1-5" and "Noted1-2", so you can save, which grade it is
                       "grade INTEGER, "                       // the grade value
                       "comment VARCHAR(100), "
                       "FOREIGN KEY(studentID) REFERENCES Student, "
                       "FOREIGN KEY(courseID) REFERENCES Course)");                 // additional / optional comment from teacher on this grade / note
            query.exec("INSERT INTO Grade VALUES(NULL, 0, 3, 'Test', 2, 'Comment')");  // ID N°3 FOR Espagnol
            query.exec("INSERT INTO Grade VALUES(NULL, 0, 3, 'Test2', 3, 'Comment')");
            query.exec("INSERT INTO Grade VALUES(NULL, 0, 3, 'Test3', 4, 'Comment5')");
    
    
            //// We need SQL_RelationalTableModel later to display data correctly (JOIN Tables)
            ////    QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
    
            model = new QSqlQueryModel(this);
            query.exec("SELECT * FROM Student");
            model->setQuery(query);
            ui->tableView->setModel(model);
        }
    
        // Next I will change this function here. At the moment it still creates new tables every time and inserts our test data.
    
        // dont need to close in every function. DB can be connected until we dont want to view / select / delete or insert data
    //    login->conClose();
    }
    
    
    
    
    
    

    0_1555464318583_ppppp.PNG

    提前谢谢你......

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved