• 6165阅读
  • 1回复

micro2440上使用qt自带的sqlite数据库功能如何操作 [复制链接]

上一主题 下一主题
离线xf19890224
 
只看楼主 倒序阅读 楼主  发表于: 2012-10-24
请问在micro2440上使用qt自带的sqlite数据库功能如何操作。我把C++GUIQt4编程上的一个例子(第13章的scooters.rar (5 K) 下载次数:0)编译了一下,在PC机上可以运行,但是交叉编译的在ARM上运行有问题。在ARM上可以产生数据文件(scooters.db文件),里面是有表scooter的,表scooter里也是有数据的。但是运行到scooterwindow.cpp里的“model->setTable("scooter");//设置模型运行的数据库表的表名”这一句时便无法运行下去了,程序发生异常,提示Illegalinstruction 。请问高手如何解决。如果这么简单的例子在arm上都跑不起,那就太没天理了。

文件1:scooterwindow.h

复制代码
  1. #ifndef SCOOTERWINDOW_H
  2. #define SCOOTERWINDOW_H
  3. #include <QWidget>
  4. //class QSqlTableModel;
  5. //class QTableView;
  6. #include <QSqlTableModel>
  7. #include <QTableView>
  8. //给列索引号定义具体的名字
  9. enum {
  10. Scooter_Id = 0,
  11. Scooter_Name = 1,
  12. Scooter_MaxSpeed = 2,
  13. Scooter_MaxRange = 3,
  14. Scooter_Weight = 4,
  15. Scooter_Description = 5
  16. };
  17. class ScooterWindow : public QWidget
  18. {
  19. Q_OBJECT
  20. public:
  21. ScooterWindow();
  22. private:
  23. QSqlTableModel *model;
  24. QTableView *view;
  25. };
  26. #endif


文件2:scooterwindow.cpp

复制代码
  1. #include <QtGui>
  2. #include <QtSql>
  3. #include <QDebug>
  4. #include "scooterwindow.h"
  5. ScooterWindow::ScooterWindow()
  6. {
  7. model = new QSqlTableModel(this);//提供一个可编辑的模型,可以独立处理数据库
  8. qDebug()<<"model = new QSqlTableModel(this);";
  9. model->setTable("scooter");//设置模型运行的数据库表的表名
  10. if (model->lastError().isValid())
  11. qDebug() << model->lastError();
  12. qDebug()<<"model->setTable(""scooter)";
  13. model->setSort(Scooter_Name, Qt::AscendingOrder);//按名字的升序排列
  14. qDebug()<<"model->setSort(Scooter_Name, Qt::AscendingOrder);";
  15. //设置表头的标题,不写此句,名称就是
  16. //query.exec("INSERT INTO scooter (name, maxspeed, "里面的原始字段name,而不是这里设置的Name
  17. model->setHeaderData(Scooter_Name, Qt::Horizontal, tr("Name"));
  18. qDebug()<<"model->setHeaderData(Scooter_Name, Qt::Horizontal,tr(""Name";
  19. model->setHeaderData(Scooter_MaxSpeed, Qt::Horizontal, tr("MPH"));
  20. model->setHeaderData(Scooter_MaxRange, Qt::Horizontal, tr("Miles"));
  21. model->setHeaderData(Scooter_Weight, Qt::Horizontal, tr("Lbs"));
  22. model->setHeaderData(Scooter_Description, Qt::Horizontal,
  23. tr("Description"));
  24. model->select();//用表的信息填充模型
  25. qDebug()<<"model->select()";
  26. //创建一个视图用以显示上面的模型
  27. view = new QTableView;
  28. qDebug()<<"view = new QTableView;";
  29. view->setModel(model);//设置视图所要显示的模型
  30. view->setSelectionMode(QAbstractItemView::ContiguousSelection);//设置用户时选中的响应
  31. view->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中的行为
  32. view->setColumnHidden(Scooter_Id, true);//隐藏ID
  33. view->resizeColumnsToContents();//列大小根据内容来改变
  34. view->setEditTriggers(QAbstractItemView::DoubleClicked);//设置触发编辑的方式
  35. QHeaderView *header = view->horizontalHeader();
  36. header->setStretchLastSection(true);//设置header占用所有可用的空间
  37. QHBoxLayout *mainLayout = new QHBoxLayout;
  38. mainLayout->addWidget(view);
  39. setLayout(mainLayout);
  40. setWindowTitle(tr("Scooters"));
  41. qDebug()<<"setWindowTitle(tr(""Scooters""));";
  42. }


文件3:main.cpp

复制代码
  1. #include <QtGui>
  2. #include <QtSql>
  3. #include "scooterwindow.h"
  4. bool createConnection()
  5. {
  6. //创建QSqlDatabase对象,使用QSQLITE作为数据库的驱动程序来访问数据库
  7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//可以设置连接connection的名字
  8. db.setDatabaseName("scooters.db");//设置连接的数据库的名称,必须在open之前。注意与connection的名字的连接
  9. if (!db.open())//打开数据库
  10. {
  11. QMessageBox::warning(0, QObject::tr("Database Error"),
  12. db.lastError().text());
  13. return false;
  14. }
  15. return true;
  16. }
  17. void createFakeData()
  18. {
  19. QSqlQuery query;//提供了一种执行和操纵SQL语句的方式。
  20. query.exec("DROP TABLE scooter");//为什么要删除呢?
  21. // /******************2012.10.21******************/
  22. // bool ok1=query.lastError().isValid();
  23. // if (ok1)
  24. // qDebug() << query.lastError();
  25. // else
  26. // qDebug()<<"ok1:query.exec is ok!";
  27. // /************************************************/
  28. query.exec("CREATE TABLE scooter ("//使用SQL语句创建数据表
  29. //"id INTEGER PRIMARY KEY AUTOINCREMENT, "//主键
  30. "id INTEGER PRIMARY KEY , "//主键
  31. "name VARCHAR(40) NOT NULL, "//表示NULL允许NULL值,NOT NULL应该是不允许NULL值
  32. "maxspeed INTEGER NOT NULL, "
  33. "maxrange INTEGER NOT NULL, "
  34. "weight INTEGER NOT NULL, "
  35. "description VARCHAR(80) NOT NULL)");
  36. // /******************2012.10.21******************/
  37. // bool ok2=query.lastError().isValid();
  38. // if (ok2)
  39. // qDebug() << query.lastError();
  40. // else
  41. // qDebug()<<"ok2:query.exec is ok!";
  42. // /************************************************/
  43. query.exec("INSERT INTO scooter (name, maxspeed, "
  44. "maxrange, weight, description) "
  45. "VALUES ('Mod-Rad 1500', 40, 35, 298, "
  46. "'Speedometer, odometer, battery meter, turn signal "
  47. "indicator, locking steering column')");
  48. query.exec("INSERT INTO scooter (name, maxspeed, "
  49. "maxrange, weight, description) "
  50. "VALUES ('Rad2Go Great White E36', 22, 12, 93, "
  51. "'10\" airless tires')");
  52. query.exec("INSERT INTO scooter (name, maxspeed, "
  53. "maxrange, weight, description) "
  54. "VALUES ('X-Treme X360', 21, 14, 59, "
  55. "'Cargo rack, foldable')");
  56. query.exec("INSERT INTO scooter (name, maxspeed, "
  57. "maxrange, weight, description) "
  58. "VALUES ('Vego SX 600', 20, , 76, "
  59. "'Two interchangeable batteries, foldable')");
  60. query.exec("INSERT INTO scooter (name, maxspeed, "
  61. "maxrange, weight, description) "
  62. "VALUES ('Sunbird E Bike', 18, 30, 118, '')");
  63. query.exec("INSERT INTO scooter (name, maxspeed, "
  64. "maxrange, weight, description) "
  65. "VALUES ('Leopard Shark', 16, 12, 63, "
  66. "'Battery indicator, removable seat, foldable')");
  67. query.exec("INSERT INTO scooter (name, maxspeed, "
  68. "maxrange, weight, description) "
  69. "VALUES ('Vego iQ 450', 15, 0, 60, "
  70. "'OUT OF STOCK')");
  71. query.exec("INSERT INTO scooter (name, maxspeed, "
  72. "maxrange, weight, description) "
  73. "VALUES ('X-Treme X-11', 15, 11, 38, "
  74. "'High powered brakes, foldable')");
  75. query.exec("INSERT INTO scooter (name, maxspeed, "
  76. "maxrange, weight, description) "
  77. "VALUES ('ZZ Cruiser', 14, 10, 46, "
  78. "'Two batteries, removable seat')");
  79. query.exec("INSERT INTO scooter (name, maxspeed, "
  80. "maxrange, weight, description) "
  81. "VALUES ('X-Treme X-010', 10, 10, 14, "
  82. "'Solid tires')");
  83. query.exec("INSERT INTO scooter (name, maxspeed, "
  84. "maxrange, weight, description) "
  85. "VALUES ('Q Electric Chariot', 10, 15, 60, "
  86. "'Foldable')");
  87. query.exec("INSERT INTO scooter (name, maxspeed, "
  88. "maxrange, weight, description) "
  89. "VALUES ('X-Treme X250', 15, 12, 0, "
  90. "'Solid aluminum deck')");
  91. query.exec("INSERT INTO scooter (name, maxspeed, "
  92. "maxrange, weight, description) "
  93. "VALUES ('Go MotorBoard 2000X', 15, 0, 20, "
  94. "'Foldable and carryable')");
  95. query.exec("INSERT INTO scooter (name, maxspeed, "
  96. "maxrange, weight, description) "
  97. "VALUES ('Goped ESR750 Sport Electric Scooter', "
  98. "20, 6, 45, " "'Foldable and carryable')");
  99. }
  100. //主程序
  101. int main(int argc, char *argv[])
  102. {
  103. QApplication app(argc, argv);
  104. bool create = !QFile::exists("scooters.db");//是否进行创建的标识
  105. if (!createConnection())
  106. return 1;
  107. if (create)
  108. createFakeData();
  109. ScooterWindow window;
  110. //window.resize(600, 500);
  111. window.resize(400, 300);
  112. qDebug()<<"window.resize(400, 300);";
  113. window.show();
  114. qDebug()<<"window.show();";
  115. return app.exec();
  116. }




文件4:scooters.pro

复制代码
  1. TEMPLATE      = app
  2. QT           += sql
  3. HEADERS       = scooterwindow.h
  4. SOURCES       = main.cpp \
  5.                 scooterwindow.cpp

离线xf19890224
只看该作者 1楼 发表于: 2012-10-30
没人遇到过这个问题吗?谁能帮忙解决一下啊~~
快速回复
限100 字节
 
上一个 下一个