2020-08-27

初识QT 常见问题

1.问题描述如截图所示:
在这里插入图片描述
2.解决方案:
找不到 -lGL
sudo apt-get install libgl1-mesa-dev

collect2:error:ld returned 1 exit status
报错原因源于代码(自己所遇情况)
问题:处在管理数据库的类,
单列的静态类指针的初始化错误,
初始化: 数据类型 类名:: 指针 = nullptr

例子: qt连接数据库测试
mysql.h 文件

#ifndef MYSQL_H
#define MYSQL_H
#include "qsqlmanage.h"

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QSqlRecord>

class MySql
{
public:
    QSqlQuery query_p;
public:
    MySql();
    //insert into table()values();
    bool insertInto_table(const QString&pname,const QString &_paswrod,const QString&psubmission);
    //select * from tablename;
    void selecttableShow();
    //update tablename set file1 = newvalue where primary key = value
    bool UpdateTable_data(const QString & pvalue,const QString & pvalue2);
    //delete from tablename where primary key = value
    bool DeleteTable_data(const QString & pvalue);
    //select *from tablename roder by  primary key desc
    void OrderBy_desc();
    //select *from taablename where file like '%xxx';
    void selectTableshow_X(const QString & pchoice);

};

#endif // MYSQL_H

mysql.cpp 文件

#include "mysql.h"

MySql::MySql()
{
    qDebug()<<"Mysql()";
}

//insert into table()values();
bool MySql::insertInto_table(const QString &pname, const QString &_paswrod,const QString & psubmission)
{
    //1 open database my2020715
    if(!QSqlManage::getInstance().ifopen()){
        QSqlManage::getInstance();
    }
    //2
    query_p.prepare("insert into my716(name,paswrod,submission)values(:name,:paswrod,:submission);");
    //3.
    query_p.bindValue(":name",pname);
    query_p.bindValue(":paswrod",_paswrod);
    query_p.bindValue(":submission",psubmission);
    //4.
    if(!query_p.exec()){
        qDebug()<<"query_p.exec() fail"<<query_p.lastError().text();
        return false;
    }else{
        return true;
    }
    //return query_p.exec();
}

//select * from tablename;
void MySql::selecttableShow()
{
    //1 open database my2020715
    if(!QSqlManage::getInstance().ifopen()){
        QSqlManage ::getInstance();
    }
    //2
    query_p.prepare("select *from my716;");
    //3
    if(!query_p.exec()){

       qDebug()<<"query_p.exec() -- select * from my716 fail"<<query_p.lastError().text();
    }else{
        while(query_p.next()){
            qDebug()<<query_p.value(0).toString();
            qDebug()<<query_p.value(1).toString();
            qDebug()<<query_p.value(2).toString();
            qDebug()<<"";
        }
    }
}

//update tablename set file1 = newvalue where primary key = value

bool MySql::UpdateTable_data(const QString &pvalue,const QString & pvalue2)
{
    //1.
    if(!QSqlManage :: getInstance().ifopen()){
        QSqlManage :: getInstance();
    }
    //2
    query_p.prepare("update my716 set paswrod =:paswrod where name =:name;");
    //3
    query_p.bindValue(":paswrod",pvalue);
    query_p.bindValue(":name",pvalue2);
    //4
    if(!query_p.exec()){
        qDebug()<<""<<query_p.lastError().text();
        return false;
    }
    return query_p.exec();
}

//delete from tablename where primary key = value
bool MySql::DeleteTable_data(const QString &pvalue)
{

    if(!QSqlManage :: getInstance().ifopen()){
        QSqlManage :: getInstance();
    }

    query_p.prepare("delete from my716 where name =:name;");

    query_p.bindValue(":name",pvalue);

    if(!query_p.exec()){
       qDebug()<<""<<query_p.lastError();
       return false;
    }
    return query_p.exec();
}

//select *from tablename roder by  primary key desc
void MySql::OrderBy_desc()
{
    if(!QSqlManage :: getInstance().ifopen()){
        QSqlManage ::getInstance();
    }
    query_p.prepare("select *from my716 order by name desc;");
    if(!query_p.exec()){
        qDebug()<<""<<query_p.lastError();
    }else{
        this ->selecttableShow();
    }
}

//select *from taablename where file like '%xxx';
void MySql::selectTableshow_X(const QString &pchoice)
{
    if(!QSqlManage :: getInstance().ifopen()){
        QSqlManage :: getInstance();
    }
    query_p.prepare("select *from my716 where paswrod like '%02';");
    //query_p.bindValue(":pchoice",pchoice);
    if(!query_p.exec()){
        qDebug()<<"select %xxx fail"<<query_p.lastError();
    }else{
        //qDebug()<< query_p.value( query_p.record()).toString();
    }
}

管理数据库的类
qsqlmanager.h 文件

#ifndef QSQLMANAGE_H
#define QSQLMANAGE_H

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>

class QSqlManage
{
private:
    QSqlManage();                      //create connect
    static QSqlManage* getinstance_p;  // class pointor
    QSqlDatabase db;                    //database
public:
    static QSqlManage & getInstance();    //QSqlManage api
    bool Create_Table();                  // create table
    void close();                         //close database
    bool ifopen();                       //open database
    class Destroy{                       // by destroy Qsqldatabase signal
      public:
        Destroy();
        ~Destroy();
    };
    static Destroy destroy_qm;
};

#endif // QSQLMANAGE_H

qsqlmanager.cpp 文件

#include "qsqlmanage.h"
#include <QDebug>

//init  class pointor
QSqlManage* QSqlManage :: getinstance_p = nullptr;

//connect  database (QMysql)
QSqlManage::QSqlManage()
    :db(QSqlDatabase::addDatabase("QMYSQL"))
{
    this ->db.setHostName("192.168.10.175");
    this ->db.setPort(3306);
    this ->db.setDatabaseName("my2020715");
    this ->db.setUserName("root");
    this ->db.setPassword("123456");
    if(!db.open()){
        qDebug()<<"database my2020715 open fail";
    }else{
        qDebug()<<"database my2020715 open sucessed";
    }
}

//class api
QSqlManage & QSqlManage::getInstance()
{
    if(!getinstance_p){
        getinstance_p = new QSqlManage;
    }
    return *getinstance_p;
}

//create table

bool QSqlManage::Create_Table()
{
    //1. isopen()

    if(!db.open()){
        qDebug()<<"opne my2020715 fail"<<this ->db.lastError();
    }
    //2.create query object

    QSqlQuery query;

    //3.input terminal create table commed
     query.prepare("create table if not exists my716("
                      "name varchar(20) primary key not null,"
                      "paswrod varchar(20) not null,"
                      "submission varchar(20) not null);");

    //4.exec()
    if(!query.exec()){
        qDebug()<<"create table fail"<<query.lastError().text();
        return false;
    }
    return query.exec();
}

void QSqlManage::close()
{
    this ->db.close();
    this ->db.removeDatabase("my2020715");
}

bool QSqlManage::ifopen()
{
    return db.isOpen();
}

QSqlManage::Destroy::Destroy()
{
    if(QSqlManage ::getinstance_p){
       delete QSqlManage :: getinstance_p;
        qDebug()<<"create destory object by delete getinstance_p";
    }
}

QSqlManage::Destroy::~Destroy()
{
   qDebug()<<"~Destroy()";
}

main.cpp 文件

#include "mainwindow.h"
#include <QApplication>
#include "qsqlmanage.h"
#include "mysql.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    //w.show();

    //open database --> my2020715
    QSqlManage mydb = QSqlManage ::getInstance();
    //create tablename --->my716
    qDebug()<<mydb.Create_Table();

    // table
    MySql mysqltable;

    //insert into table()values();
    qDebug()<< mysqltable.insertInto_table("may","1012","pxt");
    qDebug()<< mysqltable.insertInto_table("pop","1002","pxt");
    qDebug()<< mysqltable.insertInto_table("yy","1082","pxt");
    qDebug()<< "insert into ";

    //select *from tablename
    mysqltable.selecttableShow();

    //update
    mysqltable.UpdateTable_data("xxxx","may");
    qDebug()<<"update";
    mysqltable.selecttableShow();

    //delete from where
    mysqltable.DeleteTable_data("may");
    qDebug()<<"delete";
    mysqltable.selecttableShow();

    //select *from tablename roder by  primary key desc
    mysqltable.OrderBy_desc();

    qDebug()<<"XXXXXXXXXXXXXXXXXXX";
    mysqltable.selectTableshow_X("%02");

    mydb.close();
    mydb.destroy_qm;

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值