复制代码- #include <QtGui>
- #include <QtSql>
- #include "scooterwindow.h"
- bool createConnection()
- {
- //创建QSqlDatabase对象,使用QSQLITE作为数据库的驱动程序来访问数据库
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//可以设置连接connection的名字
- db.setDatabaseName("scooters.db");//设置连接的数据库的名称,必须在open之前。注意与connection的名字的连接
- if (!db.open())//打开数据库
- {
- QMessageBox::warning(0, QObject::tr("Database Error"),
- db.lastError().text());
- return false;
- }
- return true;
- }
- void createFakeData()
- {
- QSqlQuery query;//提供了一种执行和操纵SQL语句的方式。
- query.exec("DROP TABLE scooter");//为什么要删除呢?
- // /******************2012.10.21******************/
- // bool ok1=query.lastError().isValid();
- // if (ok1)
- // qDebug() << query.lastError();
- // else
- // qDebug()<<"ok1:query.exec is ok!";
- // /************************************************/
- query.exec("CREATE TABLE scooter ("//使用SQL语句创建数据表
- //"id INTEGER PRIMARY KEY AUTOINCREMENT, "//主键
- "id INTEGER PRIMARY KEY , "//主键
- "name VARCHAR(40) NOT NULL, "//表示NULL允许NULL值,NOT NULL应该是不允许NULL值
- "maxspeed INTEGER NOT NULL, "
- "maxrange INTEGER NOT NULL, "
- "weight INTEGER NOT NULL, "
- "description VARCHAR(80) NOT NULL)");
- // /******************2012.10.21******************/
- // bool ok2=query.lastError().isValid();
- // if (ok2)
- // qDebug() << query.lastError();
- // else
- // qDebug()<<"ok2:query.exec is ok!";
- // /************************************************/
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Mod-Rad 1500', 40, 35, 298, "
- "'Speedometer, odometer, battery meter, turn signal "
- "indicator, locking steering column')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Rad2Go Great White E36', 22, 12, 93, "
- "'10\" airless tires')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('X-Treme X360', 21, 14, 59, "
- "'Cargo rack, foldable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Vego SX 600', 20, , 76, "
- "'Two interchangeable batteries, foldable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Sunbird E Bike', 18, 30, 118, '')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Leopard Shark', 16, 12, 63, "
- "'Battery indicator, removable seat, foldable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Vego iQ 450', 15, 0, 60, "
- "'OUT OF STOCK')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('X-Treme X-11', 15, 11, 38, "
- "'High powered brakes, foldable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('ZZ Cruiser', 14, 10, 46, "
- "'Two batteries, removable seat')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('X-Treme X-010', 10, 10, 14, "
- "'Solid tires')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Q Electric Chariot', 10, 15, 60, "
- "'Foldable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('X-Treme X250', 15, 12, 0, "
- "'Solid aluminum deck')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Go MotorBoard 2000X', 15, 0, 20, "
- "'Foldable and carryable')");
- query.exec("INSERT INTO scooter (name, maxspeed, "
- "maxrange, weight, description) "
- "VALUES ('Goped ESR750 Sport Electric Scooter', "
- "20, 6, 45, " "'Foldable and carryable')");
- }
- //主程序
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- bool create = !QFile::exists("scooters.db");//是否进行创建的标识
- if (!createConnection())
- return 1;
- if (create)
- createFakeData();
- ScooterWindow window;
- //window.resize(600, 500);
- window.resize(400, 300);
- qDebug()<<"window.resize(400, 300);";
- window.show();
- qDebug()<<"window.show();";
- return app.exec();
- }
|