test_database.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <QDebug>
  4. #include <QSqlDatabase>
  5. #include <QSqlError>
  6. #include <QSqlQuery>
  7. #include "databasemanager.h"
  8. #include "logger.h"
  9. int main(int argc, char* argv[]) {
  10. QCoreApplication app(argc, argv);
  11. try {
  12. // 初始化数据库
  13. DatabaseManager& dbManager = DatabaseManager::getInstance();
  14. QSqlDatabase& db = dbManager.getDatabase();
  15. qDebug() << "Database connected successfully!";
  16. qDebug() << "Database file path:" << db.databaseName();
  17. // 测试表是否创建成功
  18. QSqlQuery query(db);
  19. // 检查表是否存在
  20. if (query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='h_project'")) {
  21. if (query.next()) {
  22. qDebug() << "Table h_project exists!";
  23. } else {
  24. qDebug() << "Table h_project does not exist!";
  25. }
  26. }
  27. // 测试插入一条数据
  28. query.prepare(R"(
  29. INSERT INTO h_project (uuid, name, company_code, blast_status, created_at)
  30. VALUES (:uuid, :name, :company_code, :blast_status, :created_at)
  31. )");
  32. query.bindValue(":uuid", "test-uuid-123");
  33. query.bindValue(":name", "Test Project");
  34. query.bindValue(":company_code", "TEST001");
  35. query.bindValue(":blast_status", "1");
  36. query.bindValue(":created_at", QDateTime::currentDateTime().toString(Qt::ISODateWithMs));
  37. if (query.exec()) {
  38. qDebug() << "Test data inserted successfully!";
  39. } else {
  40. qDebug() << "Failed to insert test data:" << query.lastError().text();
  41. }
  42. // 测试查询数据
  43. if (query.exec("SELECT uuid, name, company_code FROM h_project WHERE uuid = 'test-uuid-123'")) {
  44. if (query.next()) {
  45. qDebug() << "Test data retrieved:";
  46. qDebug() << " UUID:" << query.value("uuid").toString();
  47. qDebug() << " Name:" << query.value("name").toString();
  48. qDebug() << " Company Code:" << query.value("company_code").toString();
  49. }
  50. }
  51. // 清理测试数据
  52. query.exec("DELETE FROM h_project WHERE uuid = 'test-uuid-123'");
  53. qDebug() << "Database migration test completed successfully!";
  54. } catch (const std::exception& e) {
  55. qDebug() << "Database error:" << e.what();
  56. return 1;
  57. }
  58. return 0;
  59. }