我正在使用qt creator的sqlite。我有一个管理sqlite数据库连接和其他操作的类。这是构造函数代码:
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
函数opendb创建数据库,代码如下:
dbconnector::dbconnector()
{
digi_db = openDb("QSQLITE","digibill.db");
QSqlQuery qry;
// Creating table owner
qry.prepare( "CREATE TABLE IF NOT EXISTS owner (owner_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, company_name VARCHAR(30), country VARCHAR(30), address VARCHAR(100), city VARCHAR(30), state VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), website VARCHAR(30), tin VARCHAR(30), currency VARCHAR(30), additional_info VARCHAR(30), logo BLOB)" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table1 created!";
// Creating table company_details
qry.prepare( "CREATE TABLE IF NOT EXISTS company_details (company_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, company_name VARCHAR(30), contact VARCHAR(15) )" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table2 created!";
// Creating table product_details
qry.prepare( "CREATE TABLE IF NOT EXISTS product_details (product_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, product_name VARCHAR(30), price FLOAT, company_id INTEGER, FOREIGN KEY(company_id) REFERENCES company_details(company_id) )" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table3 created!";
// Creating table client_details
qry.prepare( "CREATE TABLE IF NOT EXISTS client_details (client_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, company_name VARCHAR(30),contact_name VARCHAR(40), country VARCHAR(30), address VARCHAR(50),city VARCHAR(30), state VARCHAR(30), email VARCHAR(80), phone VARCHAR(15), website VARCHAR(80), tin VARCHAR(30) )" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table4 created!";
// Creating table invoice_details
qry.prepare( "CREATE TABLE IF NOT EXISTS invoice_details (invoice_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, date_of_invoice TEXT, client_id INTEGER, product_id INTEGER, FOREIGN KEY(client_id) REFERENCES client_details(client_id), FOREIGN KEY(product_id) REFERENCES product_details(product_id) )" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table5 created!";
}
问题是每次我创建类的对象时,查询都会被执行,我得到的输出为:
QSqlDatabase dbconnector::openDb(const QString &driver, const QString &name) const
{
QSqlDatabase db;
// contains() default argument is initialized to default connection
if (QSqlDatabase::contains())
{
db = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false);
}
else
{
db = QSqlDatabase::addDatabase(driver.toUpper());
}
db.setDatabaseName(name);
if (!db.isValid())
{
// Log error (last error: db.lastError().text()) and throw exception
}
if (!db.open())
{
// Log error (last error: db.lastError().text()) and throw exception
}
qDebug() <<"Database main open";
return db;
}
我的代码出了什么问题?
答案 0 :(得分:0)
您的代码实际上没有任何问题,但您应该注意到QSqlQuery.exec()
的返回值只意味着您的查询执行成功,它并不意味着您创建了一个新表。 / p>
如果您确实要检查表是否存在,可以使用
SELECT count(*) FROM sqlite_master WHERE type='table' AND name='tablename';
(将tablename
替换为您要检查的名称)并检查查询结果。但在大多数情况下,您可以毫无顾虑地使用IF NOT EXISTS
,在您的情况下,只使用if (!qry.exec()) {}
来处理错误就可以了。