Hi,
I was following a tutorial to learn something about Qt and C++ but at one point my code does not work. I try to enter some data in a fresh created SQLite3 database. The database was create with the FireFox add-on and is empty at the moment. I double checked that all names are correct and watched the tutorial three time to figure out what I did wrong.
Here is the code for inserting. I am quite sure that I am connected to the data base be cause I can read from a different table.
Settings
::Settings(QWidget *parent
) : ui(new Ui::Settings)
{
ui->setupUi(this);
MainWindow connect;
if(connect.conOpen()){
qDebug()<<("Connected...");
}else{
qDebug()<<("Not connected!");
}
}
void Settings::on_pushButton_ok_clicked(){
// Save data to db
MainWindow connect; // here are the functions conOpen() and conClose() defined
str1=ui->editName->text();
str2=ui->editPassword->text();
qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
if(qry.exec()){
qDebug()<<("Data was saved");
// Sent result to log on MainWindow
connect.conClose();
this->hide();
}else{
qDebug()<<(qry.lastError().text());
}
}
Settings::Settings(QWidget *parent) :
QDialog(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
MainWindow connect;
if(connect.conOpen()){
qDebug()<<("Connected...");
}else{
qDebug()<<("Not connected!");
}
}
void Settings::on_pushButton_ok_clicked(){
// Save data to db
MainWindow connect; // here are the functions conOpen() and conClose() defined
QString str1, str2;
QDateTime date(QDateTime::currentDateTime());
str1=ui->editName->text();
str2=ui->editPassword->text();
QSqlQuery qry;
qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
if(qry.exec()){
qDebug()<<("Data was saved");
// Sent result to log on MainWindow
connect.conClose();
this->hide();
}else{
qDebug()<<(qry.lastError().text());
}
}
To copy to clipboard, switch view to plain text mode
Before I try to write to the data base I closed the old connection and opened a new one. Here is the output:
Connected to database...
Connected...
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Connected to database...
" Parameter count mismatch"
Connected to database...
Connected...
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Connected to database...
" Parameter count mismatch"
To copy to clipboard, switch view to plain text mode
I have read that there are issues if you create a SQLite DB with Qt but I did not. Does anyone see what I did wrong?
Bookmarks