Hello :
I have the following code which connects to the Mysql database and attempts to do an insert.
#include <QApplication>
#include <QtSql/qsqldatabase>
#include <QtSql/qsqlquery>
# include <QMessageBox>
# include <QString>
# include <QVariant>
class Person
{
public:
Person()
{
};
};
int main(int argc, char *argv[])
{
db.setHostName("localhost");
db.setDatabaseName("demo");
db.setUserName("root");
db.setPassword("root");
if(!db.open())
{
m->setText("Failure Connecting to Database");
m->show();
}
else
{
m->setText("Successful connection");
m->show();
}
Person *p = new Person();
p->fname->append("John");
p->lname->append("Smith");
p->mobile->append("9980324083");
mQuery.prepare("INSERT INTO PERSON (id,first_name,last_name,phone) VALUES (?,?,?,?)");
mQuery.
addBindValue(QVariant(*(p
->fname
))) ;
mQuery.
addBindValue(QVariant(*(p
->lname
)));
mQuery.
addBindValue(QVariant(*(p
->mobile
)));
mQuery.exec();
return a.exec();
}
#include <QApplication>
#include <QtSql/qsqldatabase>
#include <QtSql/qsqlquery>
# include <QMessageBox>
# include <QString>
# include <QVariant>
class Person
{
public:
QString *fname;
QString *lname;
QString *mobile;
Person()
{
this->fname = new QString();
this->lname = new QString();
this->mobile = new QString();
};
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("demo");
db.setUserName("root");
db.setPassword("root");
if(!db.open())
{
QMessageBox *m = new QMessageBox();
m->setText("Failure Connecting to Database");
m->show();
}
else
{
QMessageBox *m = new QMessageBox();
m->setText("Successful connection");
m->show();
}
Person *p = new Person();
p->fname->append("John");
p->lname->append("Smith");
p->mobile->append("9980324083");
QSqlQuery mQuery(db);
mQuery.prepare("INSERT INTO PERSON (id,first_name,last_name,phone) VALUES (?,?,?,?)");
mQuery.addBindValue(QVariant("NULL"));
mQuery.addBindValue(QVariant(*(p->fname))) ;
mQuery.addBindValue(QVariant(*(p->lname)));
mQuery.addBindValue(QVariant(*(p->mobile)));
mQuery.exec();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
It corresponds to the following schema in mysql database:
CREATE DATABASE DEMO
CREATE TABLE PERSON
( id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(255),
last_name VARCHAR(255),
mobile_phone VARCHAR(255) );
CREATE DATABASE DEMO
CREATE TABLE PERSON
( id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(255),
last_name VARCHAR(255),
mobile_phone VARCHAR(255) );
To copy to clipboard, switch view to plain text mode
The code compiles, links and runs fine (along with showing a successfull connection to the database) but I dont see the inserted values in the database.
So for example when I go to the command line interface and type the following at the mysql command prompt:
use demo;
select * from person;
use demo;
select * from person;
To copy to clipboard, switch view to plain text mode
I get an empty set.
What am I doing wrong?
Bookmarks