Mojtaba Ahmadi
Mojtaba Ahmadi

Reputation: 1098

database error in Qt?

I wanna make a database with Qt that stores the names and grades of a class.I represented a class named tables.when I run it ,an error happens and say "the program stopped unexpectedly"!!!what is the problem? my other question is that how can I make some tables in one database .how should I change my class(the codes below)?

database.h:

#ifndef DATABASE_H
#define DATABASE_H

#include <QtSql>
#include <QString>
#include <random>

class tables
{

private:
    QString name;
    QString table_name;
    QSqlDatabase db;

public:
    tables(QString);
    tables(QString,QString);
    void table_completer(int);
    QString rand_name();
    QString make_string(int);
    ~tables();
};

tables :: tables(QString nt)
{
    table_name = nt;
}

tables :: tables(QString n,QString nt)
{
    name = n;
    table_name = nt;
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(name);
    db.open();
}

QString tables :: rand_name(){
    QString a = "abcdefghijklmnopqrstuvwxyz";
    QString s = "";
    int b = rand()%3 + 4;
    for(int i=0;i<b;i++){
        int n = rand()%25;
        s += a[n];
    }
    return s;
}

QString tables :: make_string(int num)
{
    QString result;
    result.append(QString("%1").arg(num));
    return result;
}

void tables :: table_completer(int students_numbers)
{
    QSqlQuery query;
    query.exec("CREATE TABLE"+table_name+"(firstname text,lastname text,math int,physics int,litrature int,chemistry int);");
    tables t(name,table_name);
    for(int i=0;i<students_numbers;i++){
        int a = rand()%20;
        QString e = t.make_string(a);
        int b = rand()%20;
        QString f = t.make_string(b);
        int c = rand()%20;
        QString g = t.make_string(c);
        int d = rand()%20;
        QString h = t.make_string(d);
        query.exec("INSERT INTO"+table_name+"VALUES("+t.rand_name()+","+t.rand_name()+","+e+","+f+","+g+","+h+")");
    }
}

tables :: ~tables()
{
    db.close();
}



#endif // DATABASE_H

main:

tables ab("mydatabase.db","class1");
ab.table_completer(30);

Upvotes: 0

Views: 355

Answers (2)

Mousa
Mousa

Reputation: 2290

The second query is for another connection (because you've opened another database by creating other instance of tables), so you have to get another instance of QSqlQuery for executing it.

And also note that your sql commands have syntax errors: After TABLE and INTO you have to put a space to prevent combining it with the table name and also you have to put the string values into single quotes:

query.exec("CREATE TABLE "+table_name+"(firstname text,lastname text,math int,physics int,litrature int,chemistry int);");
tables t(name,table_name);
QSqlQuery newQuery;
...

newQuery.exec("INSERT INTO "+table_name+"VALUES('"+t.rand_name()+"','"+t.rand_name()+"',"+e+","+f+","+g+","+h+")");

EDIT: I've corrected the statement noted by fasked. Thanks

Upvotes: 1

fasked
fasked

Reputation: 3645

There are too many connections with database are created. In constructor of table class you establish connection with database using:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(name);
db.open();

You create instance of table class in main function. In table_completer function you also create instance of table class.

Also in function table_completer you create QSqlQuery. It uses QSqlDatabase instance associated with default connection name because you don't specify the name explicitly. QSqlDatabase is like singleton. Because the new connection has the same name as the old one, the old QSqlDatabase object will be replaced with the new object. QSqlQuery still stores the pointer to the old QSqlDatabase but it is removed (destroyed) already - so it will crash.

Creation of new QSqlQuery in loop works because it uses valid QSqlDatabase (the new one) instance.

Upvotes: 1

Related Questions