-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookedit.cpp
68 lines (60 loc) · 1.67 KB
/
bookedit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "bookedit.h"
#include <QDebug>
BookEdit::BookEdit(const QString &dbpath,
const QString &connName)
: MySQLite(dbpath, connName)
{
}
BookEdit::~BookEdit()
{
if(DBModel != NULL)
{
delete DBModel;
DBModel = NULL;
}
}
bool BookEdit::addNewRecord(const QString &word,
const QString &mean,
const QString &connection,
const int &lektion)
{
QString sql = QString("insert into WordsBook "
"(Wort, Bedeutung, Connection, Lektion)"
" values ('%1', '%2', '%3', '%4')")
.arg(word)
.arg(mean)
.arg(connection)
.arg(lektion);
return exec(sql);
}
bool BookEdit::removeRecord(const int &rowNum)
{
QString sql = QString("delete from WordsBook where Wid = '%1'")
.arg(rowNum);
if(rowNum == getRowCount())
{
QString resetsql = QString("update sqlite_sequence set seq = %1 where name = '%2'")
.arg((getRowCount()-1)).arg("WordsBook");
exec(resetsql);
}
return exec(sql);
}
QSqlDatabase *BookEdit::getdb()
{
return db;
}
int BookEdit::getRowCount()
{
QSqlQuery query("select count(*) from WordsBook", *db);
query.next();
return query.value(0).toInt();
}
QSqlTableModel *BookEdit::wordListDBModel(QWidget *parent,
const QString &tableName)
{
DBModel = new QSqlTableModel(parent,*db);
DBModel->setTable(tableName);
DBModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
DBModel->select(); //Ñ¡È¡Õû¸ö±íµÄËùÓÐÐÐ
return DBModel;
}