Last active
April 22, 2018 07:19
-
-
Save tmk815/0561b4699634b854c37dbddb3cf14413 to your computer and use it in GitHub Desktop.
Qt_SQLiteSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <QtSql/QSqlDatabase> | |
#include <QtSql/QSqlQuery> | |
#include <QDebug> | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
//Push Buttonが押されたとき | |
void MainWindow::on_button1_clicked() | |
{ | |
//変数textにTextEditの内容を取得して代入する。 | |
QString text= ui->textEdit->toPlainText(); | |
// データベースを開く。無ければ作る。 | |
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); | |
db.setDatabaseName("./sampledatabase.sqlite3"); | |
db.open(); | |
qDebug() << "Database作成"; | |
// 無い場合テーブルを作る。 | |
QSqlQuery query(db); | |
query.exec("create table sample(id INTEGER PRIMARY KEY AUTOINCREMENT, memo text)"); | |
qDebug() << "テーブル作成"; | |
// データを書き込む | |
query.prepare("insert into sample (memo) values (?)"); | |
query.bindValue(0, text); | |
query.exec(); | |
// データを出力する | |
qDebug() << "内容"; | |
//追加したmemoの内容と一致するものだけをWhereで取り出す。 | |
query.exec("select * from sample WHERE memo = '"+text+"'"); | |
//最後まで繰り返す | |
while (query.next()) { | |
int id = query.value(0).toInt(); | |
QString memo = query.value(1).toString(); | |
qDebug() << QString("id(%1),memo(%2)").arg(id).arg(memo); | |
} | |
db.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment