[英]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? 我想用Qt创建一个数据库来存储类的名称和等级。我代表了一个名为tables
的类。当我运行它时,会发生错误并说“程序意外停止”!!!问题是什么? 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);
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. 第二个查询用于另一个连接(因为您通过创建其他tables
实例打开了另一个数据库),因此您必须获取另一个QSqlQuery
实例来执行它。
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: 另请注意,您的sql命令有语法错误:在TABLE
和INTO
之后,您必须放置一个空格以防止将其与表名组合,并且您还必须将字符串值放入单引号中:
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 . 编辑:我已经纠正了fasked提到的声明 。 Thanks 谢谢
There are too many connections with database are created. 创建了与数据库的连接太多。 In constructor of table
class you establish connection with database using: 在table
类的构造函数中,您使用以下命令与数据库建立连接:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(name);
db.open();
You create instance of table
class in main
function. 您在main
函数中创建table
类的实例。 In table_completer
function you also create instance of table
class. 在table_completer
函数中,您还可以创建table
类的实例。
Also in function table_completer
you create QSqlQuery
. 同样在函数table_completer
您可以创建QSqlQuery
。 It uses QSqlDatabase
instance associated with default connection name because you don't specify the name explicitly. 它使用与默认连接名称关联的QSqlDatabase
实例,因为您未明确指定名称。 QSqlDatabase
is like singleton. QSqlDatabase
就像是singleton。 Because the new connection has the same name as the old one, the old QSqlDatabase
object will be replaced with the new object. 由于新连接与旧连接的名称相同,因此旧的QSqlDatabase
对象将替换为新对象。 QSqlQuery
still stores the pointer to the old QSqlDatabase
but it is removed (destroyed) already - so it will crash. QSqlQuery
仍然存储指向旧QSqlDatabase
的指针,但它已被删除(销毁) - 因此它将崩溃。
Creation of new QSqlQuery
in loop works because it uses valid QSqlDatabase
(the new one) instance. 在循环中创建新的QSqlQuery
是有效的,因为它使用有效的QSqlDatabase
(新的)实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.