Exemplo n.º 1
0
void DataConnect::borrowInsertNew(Borrower b)
{
    QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("data.db");
    if  (mydb.open()) {
        std::cout<<"Database is open\n";
    } else {
        std::cout<<"Database is not open\n";
    }
    QString name = QString::fromStdString(b.getName());
    QString type = QString::fromStdString(b.getType());
    QString department = QString::fromStdString(b.getDepartment());
    QString mobile = QString::fromStdString(b.getMobileNumber());
    int itemBorr = b.getItemBorr();
    int itemLate = b.getItemLate();
    QString itemString = QString::fromStdString(b.getItemString());
    int id = b.getId();

    QSqlQuery comman;
    QString exc = QString("INSERT INTO borrowers (name , type, department,")
            +QString("mobileNumber, itemBorr, itemLate ,itemString, id) VALUES ")
            +QString("('")+ name +QString("','")+type+QString("','")+department
            +QString("','")+ mobile +QString("',")+ QString::number(itemBorr) +QString(",")
            +QString::number(itemLate)+QString(",'") +itemString+ QString("',")+ QString::number(id) +QString(");");
    std::cout << exc.toStdString()<< std::endl;
    if(comman.exec(exc)) {
        std::cout <<"insert success borr!\n";
    } else {
        std::cout << "insert failure borr!\n";
    }

    mydb.close();
}
Exemplo n.º 2
0
void MultimediaCollection::addBorrower(Borrower b)
{
	borrowers.push_back(b);
	try {
		sql::Driver *driver;
		sql::Connection *con;
		sql::PreparedStatement *prepareStmt;

		driver = get_driver_instance();
		con = driver->connect("tcp://127.0.0.1:3306", "root", "dhis");
		con->setSchema("test");

		prepareStmt = con->prepareStatement(
			"INSERT INTO borrower(id, name, type, department, mobile, numborrow, numreturnlate)" 
			"VALUES (?,?,?,?,?,?,?)");
		prepareStmt->setString(1, b.getId());
		prepareStmt->setString(2, b.getName());
		prepareStmt->setString(3, b.getType());
		prepareStmt->setString(4, b.getDepartment());
		prepareStmt->setString(5, b.getMobile());
		prepareStmt->setInt(6, b.getNumItemBorrow());
		prepareStmt->setInt(7, b.getNumItemLateBorrow());

		prepareStmt->execute();

		delete prepareStmt;
		delete con;

	} catch (sql::SQLException &e) {
	  cout << "# ERR: SQLException in " << __FILE__;
	  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
	  cout << "# ERR: " << e.what();
	  cout << " (MySQL error code: " << e.getErrorCode();
	  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
}