#define DBHOST "127.0.0.1" //for localhost loop
#define DBDRIVER "QSQLITE" //if use other DB need the driver with dll files
#define DBNAME "DB.db3" //name used for the database file
//#define
//constructor
Database::Database() {
createConnectiontoSqlite();
}
....
bool Database::createConnectiontoSqlite() {
db.setHostName(DBHOST);
db.setDatabaseName(DBNAME); //if the dababase does not exist he will create it
//the database name is also the name of the file in the working directory
// db.setUserName("user");//username and password does not affect SQLITE db
// db.setPassword("pass");
if (!db.open()) {
db.lastError().text());
return false;
}
return true;
}
void Database::createTables() {
//main Tables
// table
query.exec(
"create table table1(id int primary key, name varchar(20), variable int, company int)");
query.exec("insert into table1 values(1, 'Default', 1, 1)");
query.exec("insert into table1 values(2, 'blabla', 2, 1)");
.....
qDebug() << "dB created "; //need to set up debug in the .pro file
#define DBHOST "127.0.0.1" //for localhost loop
#define DBDRIVER "QSQLITE" //if use other DB need the driver with dll files
#define DBNAME "DB.db3" //name used for the database file
//#define
//constructor
Database::Database() {
createConnectiontoSqlite();
}
....
bool Database::createConnectiontoSqlite() {
QSqlDatabase db = QSqlDatabase::addDatabase(DBDRIVER);// changed "QSQLITE" with define DBDRIVER
db.setHostName(DBHOST);
db.setDatabaseName(DBNAME); //if the dababase does not exist he will create it
//the database name is also the name of the file in the working directory
// db.setUserName("user");//username and password does not affect SQLITE db
// db.setPassword("pass");
if (!db.open()) {
QMessageBox::critical(0, QObject::tr("DB Error - CreateConnection()"),
db.lastError().text());
return false;
}
return true;
}
void Database::createTables() {
QSqlQuery query;
//main Tables
// table
query.exec(
"create table table1(id int primary key, name varchar(20), variable int, company int)");
query.exec("insert into table1 values(1, 'Default', 1, 1)");
query.exec("insert into table1 values(2, 'blabla', 2, 1)");
.....
qDebug() << "dB created "; //need to set up debug in the .pro file
To copy to clipboard, switch view to plain text mode
Bookmarks