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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include "DBConnection.h"
#include <QtGui>
#include <sqlite3.h>
bool LastError;
DBConnection::DBConnection()
{
}
void DBConnection::createConnection()
{
cn= QSqlDatabase::addDatabase(DB_DRIVER);
initializeConnections();
}
void DBConnection::openConnection()
{
cn=QSqlDatabase::database();
}
void DBConnection::closeConnection()
{
//cn.close();
//cn.~QSqlDatabase();
/*if(QSqlDatabase::contains(ConName))
{
qDebug()<<QSqlDatabase::contains(ConName);
if(QSqlDatabase::contains(ConName))
{
qDebug()<<ConName;
}
//QSqlDatabase::removeDatabase(ConName);
}*/
}
QSqlQuery DBConnection::SelectQuery(QString Query)
{
LastError=false;
//QSqlQuery rs(QString::null,cn);
if(cn.isOpen())
{
if(!(q.exec(Query)))
{
QMessageBox::critical(0,"Error", q.lastError().text());
LastError=true;
}
}
else
{
QMessageBox::critical(0,"Error", "Connection is Closed" );
LastError=true;
}
return q;
}
bool DBConnection::initializeConnections()
{
try
{
LastError=false;
cn.setDatabaseName(DB_DBNAME);
if ( ! cn.open() ) {
LastError=true;
QMessageBox::critical(0,"Error", "Cannot open database:"+ cn.lastError().text());
return FALSE;
}
return TRUE;
}
catch(...)
{
LastError=true;
QMessageBox::critical(0,"Error", "Query Execution Error" );
return FALSE;
}
}
bool DBConnection::DataBaseCommand(QString StrQuery)
{
LastError=false;
//QSqlQuery qrs(cn);
if(cn.isOpen())
{
q.clear();
if(!(q.exec(StrQuery)))
{
LastError=true;
if(q.lastError().number()!=19)
{
QMessageBox::critical(0,"Error", q.lastError().text());
q.clear();
return false;
}
// false;
else
{
QVariant v = q.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
{
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0)
{
qDebug("Error: %s", sqlite3_errmsg(handle));
QMessageBox::about(0,"Trigger",sqlite3_errmsg(handle));
}
}
q.clear();
return false;
}
}
if(q.isActive()==false)
{
QVariant v = q.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
{
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
if (handle != 0)
{
qDebug("Error: %s", sqlite3_errmsg(handle));
QMessageBox::about(0,"Trigger",sqlite3_errmsg(handle));
}
}
QMessageBox::about(0,"err",q.lastError().text());
}
}
else
{
LastError=true;
QMessageBox::critical(0,"Error", "Connection is Closed" );
}
q.clear();
return true;
}
bool DBConnection::DataBaseCommand(QSqlQuery SqlQuery)
{
LastError=false;
if(cn.isOpen())
{
if(!(SqlQuery.exec()))
{
LastError=true;
QMessageBox::critical(0,"Error", SqlQuery.lastError().text());
SqlQuery.clear();
}
}
else
{
SqlQuery.clear();
LastError=true;
QMessageBox::critical(0,"Error", "Connection is Closed" );
}
SqlQuery.clear();
return true;
}
bool DBConnection::IsLastError()
{
if(LastError) return true;
else return false;
}
int DBConnection::Size(QSqlQuery query)
{
int size=0;
while(query.next())
{
size++;
}
query.clear();
return size;
}
void DBConnection::clearAll()
{
q.clear();
}
|