数据库设计的代码,连接的是金仓数据库
时间: 2024-03-23 08:43:52 浏览: 62
mybatisplus-generator 代码生成器 人大金仓数据库支持扩展
以下是一个简单的数据库设计代码,使用的是金仓数据库:
```cpp
#include <QSqlDatabase>
#include <QSqlQuery>
// 连接数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("your_database_name");
db.setUserName("your_username");
db.setPassword("your_password");
// 打开数据库
if (!db.open()) {
qDebug() << "Failed to open database";
return;
}
// 创建用户表
QSqlQuery query;
query.exec("CREATE TABLE users ("
"id INT PRIMARY KEY AUTO_INCREMENT,"
"username VARCHAR(50) NOT NULL,"
"password VARCHAR(50) NOT NULL,"
"email VARCHAR(50) NOT NULL)");
// 创建试题表
query.exec("CREATE TABLE questions ("
"id INT PRIMARY KEY AUTO_INCREMENT,"
"title VARCHAR(100) NOT NULL,"
"option_a VARCHAR(50) NOT NULL,"
"option_b VARCHAR(50) NOT NULL,"
"option_c VARCHAR(50) NOT NULL,"
"option_d VARCHAR(50) NOT NULL,"
"answer VARCHAR(50) NOT NULL)");
// 创建考试表
query.exec("CREATE TABLE exams ("
"id INT PRIMARY KEY AUTO_INCREMENT,"
"title VARCHAR(100) NOT NULL,"
"start_time DATETIME NOT NULL,"
"end_time DATETIME NOT NULL)");
// 创建考试-试题关联表
query.exec("CREATE TABLE exam_questions ("
"exam_id INT NOT NULL,"
"question_id INT NOT NULL,"
"PRIMARY KEY (exam_id, question_id),"
"FOREIGN KEY (exam_id) REFERENCES exams(id),"
"FOREIGN KEY (question_id) REFERENCES questions(id))");
```
这段代码使用了QSqlDatabase类和QSqlQuery类来连接和操作数据库。其中,创建了四个表格:users、questions、exams和exam_questions,分别用于存储用户信息、试题信息、考试信息和考试-试题关联信息。您可以根据具体需求进行修改和完善。同时,需要注意的是,由于金仓数据库与MySQL数据库在数据类型、SQL语法等方面存在一些差异,可能需要进行一些调整和修改。
阅读全文