PDA

View Full Version : unable to open the database



kulsekarr
8th November 2013, 10:54
hi , hope every one are doing good...
I am trying to create a table from qt on my sql server 2008, the code which i have tried throws a error, that "unable to open the database". kindly do anyone please give me solutions and i have pasted my code below..

Thanks

Code:


void sas_config::on_cLB_save_clicked()
{
addDataBase();
}


void sas_config::addDataBase()
{

QString driverName="QODBC",databaseName="DRIVER={SQL Server};SERVER=KULASEKARAN;DATABASE=test",hostName="KULASEKARAN",
userName="kulasekaran",password="1234";
QSqlError err = addConnection(driverName,databaseName,hostName,
userName, password);
if (err.type() != QSqlError::NoError)
QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
"opening the connection: ") + err.text());

}


SqlError sas_config::addConnection(const QString &driver, const QString &dbName, const QString &host,
const QString &user, const QString &passwd)
{
QSqlError err;
bool found =false;
QSqlDatabase db = QSqlDatabase::addDatabase(driver);
db.setDatabaseName(dbName);
db.setHostName(host);
if (!db.open(user, passwd))
{
err = db.lastError();
qDebug()<<"Error"<<err;
db = QSqlDatabase();
QSqlDatabase::removeDatabase(QString("test"));
return err;
}

if (db.isOpen()) {
foreach (QString table, db.tables())
{
if(table == "kul")
{
found = true;
qDebug()<<"Table Found";
break;
}
}
if(!found)
{

query.exec("CREATE TABLE kul (Name VARCHAR(32), ID VARCHAR(16), Height NUMERIC, Device NUMERIC)");
qDebug()<<"Created the table";
}
QTreeView *view = ui->treeView;
Q_CHECK_PTR(view);
model = new QSqlTableModel(this,db);
model->setTable("kul");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
view->setModel(model);
}
}

stampede
8th November 2013, 14:19
Does the database specified by connection parameters exists ? What's the output of
QSqlDatabase::drivers()?

kulsekarr
10th November 2013, 04:56
hi stampede ,
The database connection specified is exists and I am trying to connect it with sql server
The output of QSqlDatabase::drivers().. gives me the driver which I have connected that I have mentioned in the addDatabase().

stampede
10th November 2013, 07:54
Can you try to open that database with "sqlbrowser" application from qt examples ?