#include "database.h" #include "QSqlRecord" Database::Database(QObject *parent) : QObject(parent) { } Database::~Database() { _database.close(); } void Database::SetTableName(QString table) { _tableName = table + ".db"; } bool Database::openDB() { _database = QSqlDatabase::addDatabase("QSQLITE","db"); // _database.setDatabaseName(_tableName); _database.setDatabaseName(".//qtDb.db"); return _database.open(); } bool Database::createProductDatabase() { QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); bool success = query.exec("CREATE TABLE product (" "goods_id INTEGER NOT NULL," "code_single VARCHAR NOT NULL," "quantity INTEGER NOT NULL," "batch_no VARCHAR NOT NULL," "category VARCHAR NOT NULL," "level_id INTEGER NOT NULL," "tube_number INTEGER NOT NULL," "gross_weight INTEGER NOT NULL," "net_weight INTEGER NOT NULL," "machine_no VARCHAR NOT NULL," "packing_time VARCHAR NOT NULL," "packing_type VARCHAR NOT NULL," "carton_type VARCHAR NOT NULL," "operator_id INTEGER NOT NULL," "tongbu VARCHAR(1) DEFAULT 0," "remark VARCHAR);"); // bool success = query.exec("drop table product;"); if(success) { qDebug() << QObject::tr("product数据库表创建成功!\n"); return true; } else { qDebug() << QObject::tr("product数据库表已存在!\n"); return false; } } bool Database::createDetailsDatabase() { QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); bool success = query.exec("CREATE TABLE details (" "code_single VARCHAR NOT NULL," "case_number VARCHAR NOT NULL," "box_weight INTEGER NOT NULL," "tube_number INTEGER NOT NULL," "bucket_weight INTEGER NOT NULL," "gross_weight INTEGER NOT NULL," "net_weight INTEGER NOT NULL," "sort VARCHAR NOT NULL," "remark VARCHAR);"); if(success) { qDebug() << QObject::tr("details数据库表创建成功!\n"); return true; } else { qDebug() << QObject::tr("details数据库表已存在!\n"); return false; } } bool Database::insertProduct(product_data productData) { QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); query.prepare("INSERT INTO product(goods_id," "code_single, quantity, batch_no, category,level_id, tube_number," "gross_weight, net_weight, machine_no, packing_time," "packing_type, carton_type, operator_id, remark)" "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); query.bindValue(0,productData.goods_id); query.bindValue(1,productData.code_single); query.bindValue(2,productData.quantity); query.bindValue(3,productData.batch_no); query.bindValue(4,productData.category); query.bindValue(5,productData.level_id); query.bindValue(6,productData.tube_number); query.bindValue(7,productData.gross_weight); query.bindValue(8,productData.net_weight); query.bindValue(9,productData.machine_no); query.bindValue(10,productData.packing_time); query.bindValue(11,productData.packing_type); query.bindValue(12,productData.carton_type); query.bindValue(13,productData.operator_id); query.bindValue(14,productData.remark); bool success=query.exec(); return success; } bool Database::insertDetails(details_data detailsData) { QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); query.prepare("INSERT INTO details(code_single," "case_number, box_weight, tube_number, bucket_weight," "gross_weight, net_weight, sort, remark)" "VALUES(?,?,?,?,?,?,?,?,?)"); query.bindValue(0,detailsData.code_single); query.bindValue(1,detailsData.case_number); query.bindValue(2,detailsData.box_weight); query.bindValue(3,detailsData.tube_number); query.bindValue(4,detailsData.bucket_weight); query.bindValue(5,detailsData.gross_weight); query.bindValue(6,detailsData.net_weight); query.bindValue(7,detailsData.sort); query.bindValue(8,detailsData.remark); bool success=query.exec(); return success; } bool Database::changeState(QString codesingle) { QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); bool success = query.exec("update product set tongbu=1 where code_single="+codesingle); if(success) { return true; } else { return false; } } bool Database::update(QString &table, QStringList &names, QStringList &values, QString &expression) { if (names.size() != values.size()) return false; //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 // QSqlQuery query(QSqlDatabase::database()); QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); QString sql = QString("update ")+table+QString(" set "); for (int i = 0; i < names.size(); i++) { sql += names.value(i); sql += QString(" = '"); sql = sql + values.value(i); sql = sql + QString("'"); if (i != names.size()-1) sql = sql + QString(" ,"); } sql = sql + QString(" where ") + expression; return (query.exec(sql)) ? true : false; } bool Database::del(QString &table, QString &expression) { //DELETE FROM 表名称 WHERE 列名称 = 值 QSqlQuery query(QSqlDatabase::database()); QString sql = QString("delete from ") + table + QString(" where ") + expression; return (query.exec(sql)) ? true : false; } QList> Database::GetValues(QString &table, QStringList &values) { dbmxdata.clear(); dbmxlist.clear(); QSqlDatabase db = QSqlDatabase::database("db"); QSqlQuery query(db); QString sql = QString("select * from ") + table; query.exec(sql); while (query.next()) { for(int i = 0;i <= values.size()-1;i++){ // qDebug()<