Problem with SQLite database -> unable to open database file
-
wrote on 4 Jun 2012, 10:03 last edited by
Hello,
my sqlite database worked fine for a while
and now i cant write or read something.init of the database:
@ _data= QSqlDatabase::addDatabase("QSQLITE","data");
_data.setDatabaseName(DAODatabaseFilename);//Erzeugen der Tabellen in Datenbank
QFileInfo info ("/home/data.db");
if(!info.exists()){
if(_DAODatabase.open()) {
QSqlQuery* cmd = new QSqlQuery(_data);
cmd->exec(QString("PRAGMA journal_mode = OFF"));cmd->exec(QString(DataTableCreation).arg(TableName));
delete cmd;
_data.close();
}
}
@And this is how I use the database:
@
bool DB::setWertDataObject(DataObject* data)
{
if(!data){
qCritical() << "Null-Pointer bei Wert-Set";
return false;
}
if(_data.open()) {
QSqlQuery* cmd = new QSqlQuery (_data);
cmd->exec(QString(SelectData).arg(TableName).arg(data->ID));bool bResult;
//Kein Eintrag in Datenbank gefunden
if(!cmd->first()){
//Insert
bResult = cmd->exec(QString(InsertData).arg(data->ID).arg(data->fValue));
}
else{
//Update
bResult = cmd->exec(QString(UpdateData).arg(data->fValue).arg(data->ID));
}
if(bResult == false){
qCritical()
<< "Database Error : ID "
<< data->ID
<< ", Error: "
<< cmd->lastError().databaseText();}
delete cmd;
_data.close();
return bResult;
}
else{
qWarning()
<<"Cant open database";return false;
}
}@I always get the error: unable to open database file!
I have tried to delete the file, change permissions, etc.
-
wrote on 5 Jun 2012, 08:01 last edited by
this code is working right
@
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName(/your sqlite database file/);
if (mydb.open())
{
QMessageBox::information(this,"success","success");
}
@i always use this.
and if your directory is protected , change the db file directory for testing -
wrote on 5 Jun 2012, 16:52 last edited by
I get a successful connection from this:
@
#define DBFILEPATH "/Volumes/Inventory/inventory.db"
(...)
db = new QSqlDatabase();
*db = QSqlDatabase::addDatabase("QSQLITE");
QFileInfo info1(DBFILEPATH);
if (info1.isFile())
{
qDebug("DBFILEPATH points to a valid file.");
}
else
qDebug("DBFILEPATH does not point to a valid file");
db->setDatabaseName(DBFILEPATH);
db->open();
(...)
@Just FYI abbas, this code doesn't really check if the database is open:
@
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName(/your sqlite database file/);
if (mydb.open())
{
(...)
}
@
the .open() will cause the database to open at the location specified regardless of its previous existence. It will create a new database at the path if an old one by that name doesn't exist, without throwing an error.
1/3