Sql query help
-
I'm trying to work with a simple sqlite db, 1 row, 1 table with two columns, id and name.
Creating the db works and updating the db works, but reading it does not. I've done
the query manually at the sqlite3 prompt w/o a problem. query.isValid() is never
true and query.lastError().text() is empty.@
////////////////////////////////
void createTables()
{
QSqlQuery query;
query.exec("create table device(id int primary key, name varchar(20))");
query.exec("insert into device values(1, '')");}
////////////////////////////////
void updateTables()
{
QSqlQuery query;
QString sqlstatement = "UPDATE device SET name='"+daddr+"' WHERE Id=1";
query.exec(sqlstatement);
}////////////////////////////////
void readTables()
{
QSqlQuery query;
query.exec("SELECT name FROM device");if (query.isValid()) daddr = query.value(1).toString(); else daddr = "";
}
//////////////////////////////////////////////
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ui->setupUi(this);
QFile Fout("/Applications/myprog/myprog.db");
if(!Fout.exists())
{
dbexists = false;
}
else
{
dbexists = true;
}dbstring = "/Applications/myprog/myprog.db";
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbstring);if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
), QMessageBox::Cancel);}
if (!dbexists)
createTables();
else
readTables();ui->device->setText(daddr);
}
MainWindow::~MainWindow()
{
updateTables();
db.close();
delete ui;}
@