赞
踩
- int ServerBase::DBConnection()
- {
- m_sqldb = QSqlDatabase::addDatabase(DB_SALES_DRIVER);
- m_sqldb.setHostName(DB_SALES_HOST);
- m_sqldb.setDatabaseName(DB_SALES_DBNAME);
- m_sqldb.setUserName(DB_SALES_USER);
- m_sqldb.setPassword(DB_SALES_PASSWD);
- if( !m_sqldb.open())
- {
- m_dbconnected = false;
- qDebug()<<" [Error] "<<"Database open error";//<<m_sqlquery.lastError();
- return R_ERROR;
- }
- else
- {
- m_dbconnected = true;
- qDebug()<<" [OK] "<<"Database opened";
- }
- return R_OK;
- }
-
- int result = this->DBConnection();
- m_sqlquery = new QSqlQuery("",m_sqldb);//statement 1:connect db with sqlquery
- if(result == R_OK)
- {
- result = m_sqlquery->exec("INSERT INTO children(fname,age) VALUES('Ann2',13)");
- if(!result)
- qDebug()<<" [OK] "<<"EXEC successed";
- m_sqlquery->exec("SELECT * FROM children c LIMIT 0,1000");
- while(m_sqlquery->next())
- {
- qDebug()<<m_sqlquery->value(0).toString()<<m_sqlquery->value(1).toString();
- }
- }
-
- //Debug输出:
- [OK] Database opened
- "1" "Ann"
- "2" "Ann"
- "3" "Ann2"
- "4" "Ann2"
- [OK] Database closed
- m_sqldb = QSqlDatabase::addDatabase(DB_SALES_DRIVER);
- m_sqldb.setHostName(DB_SALES_HOST);
- m_sqldb.setDatabaseName(DB_SALES_DBNAME);
- m_sqldb.setUserName(DB_SALES_USER);
- m_sqldb.setPassword(DB_SALES_PASSWD);
- if( !m_sqldb.open())
- {
- m_dbconnected = DB_UNCONNECTED;
- qDebug()<<" [Error] "<<"Database open error";//<<m_sqlquery.lastError();
- return R_ERROR;
- }
- else
- {
- m_sqlquery = new QSqlQuery("",m_sqldb);
- m_dbconnected = DB_CONNECTED;
- qDebug()<<" [OK] "<<"Database opened";
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。