วันอาทิตย์ที่ 24 มกราคม พ.ศ. 2559

sqlite3 Backup database and Restore database



Backing up the database

To make a backup copy of the database, simply do a "dump" and redirect the results to a file.
cd /home/sqlite
sqlite3 sample.db .dump > sample.bak

Restoring the database

Restoring the database from a backup is just as easy as backing up, except we must make sure the destination database is empty first. Alternatively you may want to delete or rename the destination database and let sqlite create a new one for you.
cd /home/sqlite
mv sample.db sample.db.old
sqlite3 sample.db < sample.bak

ที่มา
http://www.ibiblio.org/elemental/howto/sqlite-backup.html

วันอาทิตย์ที่ 8 พฤศจิกายน พ.ศ. 2558

qt thread

https://youtu.be/JaGqGhRW5Ks
https://www.youtube.com/watch?v=yazMHbIew0Q
https://www.youtube.com/watch?v=yazMHbIew0Q&t=444s

วันจันทร์ที่ 19 ตุลาคม พ.ศ. 2558

วันศุกร์ที่ 2 ตุลาคม พ.ศ. 2558

ต่อข้อมูล string (qt append string)

การเชื่อมต่อ string เป็นเรื่องยากสำหรับผมมาก เพราะว่าหาตัวอย่างยังไงก็หาไม่เจอ
จริงๆแล้วมันไม่ได้ยากหรอก แต่เราใช้คำในการค้นหา ไม่ตรงกับความต้องการ(อีกแล้ว)

การเพิ่มข้อมูล string หรือการต่อข้อมูล string (Append string) สามารถทำได้ดังนี้

#include <QDebug>
#include <QString>
.
.
.
QString your_name = "Niran";
QString message = "Your name is " + your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message += your_name;
qDebug() << message;

หรือ
QString message = "Your name is ";
message.append(your_name);
qDebug() << message;

ที่มา
http://www.qtcentre.org/threads/33603-how-to-concatenate-a-variable-with-a-string

convert int to string

วันนี้เขียนโปรแกรม ที่จำเป็นต้อง convert จาก int เป็น string
ตอนแรกก็งงอยู่พักใหญ่ ทำยังไงหว่า เลยถาม google เหมือนเดิม
แต่ถามครั้งแรกมันแสดงผลไรมาไม่รู้เยอะแยะ ไม่ตรงกับที่เราต้องการ
ก็ต้องเปลี่ยนคำถามใหม่ (สอนให้รู้ว่า ต้องตั้งคำถามให้ตรงคำตอบ) ก็จะได้คำตอบที่ต้องการ 555

วิธีเปลี่ยนจาก int เป็น string คือ
int i=42;
QString s = QString::number(i);

ที่มา
http://stackoverflow.com/questions/3211771/how-to-convert-int-to-qstring

วันพฤหัสบดีที่ 17 กันยายน พ.ศ. 2558

qt database ตัวอย่าง 2

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql




#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("/home/kong/test-console-application/test");
    if (!db.open()){
        qDebug() << db.lastError() << endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    qDebug() << db.tables();
    QSqlQuery query;
    query.exec("select * from hose");

    while(query.next()){
        int hose_id = query.value(0).toInt();
        qDebug() << "hose_id : " << hose_id << endl;
        int pump_id = query.value(1).toInt();
        qDebug() << "pump_id : " << pump_id << endl;
        int hose_number = query.value(2).toInt();
        qDebug() << "hose_number : " << hose_number << endl;
    }

    return a.exec();
}

qt sqlite ตัวอย่าง 1

ก่อนเขียน code ต้องแก้ไข .pro ด้วยนะ
QT += sql


#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    serial = new QSerialPort();
    serial->setPortName("ttyS0");

    if (!QSqlDatabase::drivers().contains("QSQLITE")){
        qDebug()<< "not support database" << endl;
    }
    else{
        qDebug() << "support QSQLITE"<< endl;
    }

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test");
    if (!db.open()){
        qDebug()<<"can't open database"<< endl;
    }
    else{
        qDebug()<<"open database" << endl;
    }

    return a.exec();
}