Ubuntu 15.04 installation configuration Qt + SQLite3

sequence

Recently we needed to use in Ubuntu Qt development project, choose a simple compact SQLite database, configuration, and installation simple now recorded as follows, for future reference.

Qt installation

CMake and Qt Creator to develop C ++ programs under Linux artifact, Ubuntu 15.04 has been integrated with the latest version of Qt Creator (3.1.1).

sudo apt-get install cmake qtcreator  

Installation Sqlite

1. Install SQLite3

sudo apt-get install sqlite sqlite3  

2. Installation Kit needed to compile Sqlite3

//如果,你需要的话可以安装该工具包。只是为了体验一把,可以不安装。该项是可选项。

apt-get install libsqlite3-dev  

3. Check the installation was successful

//执行下面命令,会出现sqlite版本号,如3.6.22

sqlite3 -version  

4. Install a graphical interface

//不喜欢命令行的话,安装该项有必要。该项是可选项。

sudo apt-get install sqlitebrowser  

5. Install support for other languages

//PHP支持   
sudo apt-get install php5-sqlite  
//Ruby支持   
sudo apt-get install libsqlite3-ruby  
//Python支持   
sudo apt-get install python-pysqlite2  

6. The establishment of a database

//可以在任意目录下(如/home/yangrui/database),执行下面命令

sqlite3 test.db  

Note: After this command is executed, if there is no test.db in the current directory, it would create the file, if it already exists, then the direct use of the database file.

Use .database can view the database created

7. Create a table

Data type, you can refer to the official documentation.

create table mytable(name varchar(10),age smallint);  
//同理,使用.table可以看看自己创建的表mytable

8. insert data into the table

insert into mytable values('mark',28);  
insert into mytable values('hello',30);  

9. query data

select * from mytable;  

10. Delete table

drop table mytable;  

11. Delete Database

Very lucky, SQLite database files can not be deleted, as with other databases that DROP DATABASE test; invalid, but we can be like, like delete files delete database files directly, you can delete test.db file in / home / mark / database below.

Use SQLite under Qt

QtSql module provides an interface platform-independent database and the kind of access to SQL databases, this interface from the model view of the structure of the database using the Qt user interface and integrated set of classes to support.

QSqlDatabase objects symbolize the associated database. Qt uses an application programming interface drivers to communicate with various databases. Qt desktop (Desktop Edition) includes a number of drivers:
drive

New in Qt Creator in a test project:
main.cpp add the code below:

#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <string>

using namespace std;

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

    qDebug()<<"available drivers:";
    QStringList drivers = QSqlDatabase::drivers();
    foreach(QString driver, drivers)
    qDebug()<<driver;

    QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    //db.setHostName("localhost");
    //db.setPort(3306);

    //事先创建的数据库EasyChat.db
    db.setDatabaseName("/home/yangrui/projects/EasyChat/database/EasyChat.db");
    db.setUserName("root");
    db.setPassword("123456");
    if(!db.open()){
        qDebug()<<"Unable to open database";
    }else{
        qDebug()<<"Database connection established";
    }


    QSqlQuery query;
    query.exec("select * from User");
    while(query.next())
    {
        QString userId = query.value("userId").toString();
        qDebug()<<userId;
    }
    return a.exec();
}

Note: .pro file code is as follows:

#-------------------------------------------------
#
# Project created by QtCreator 2015-11-18T11:30:07
#
#-------------------------------------------------

QT       += core
QT       += sql
QT       -= gui


TARGET = SqliteTest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

Results of the:
result

Attached: Sqlite Basic operation statement Links

Guess you like

Origin blog.csdn.net/lovedarling/article/details/92571518