我有一个以Id作为主键的主表。还有另一个表,其中一个字段是外键 - “Id”,它指的是主表的主键“Id”。但是,当我从主表中删除Id时,不会从辅助表中删除Id。我有“ON DELETE CASCADE”。这是代码 -
要看的重要部分是以下内容 -
我已经在QT编写了这个程序。我正在使用sqlite数据库引擎。这是整个计划。我是Sql的新手,我很高兴,如果有人能给我一些解决方案,比如 - 1.基于级联。 2.基于触发器。 解释,为什么现在不工作?
感谢您的回复。
#include <QCoreApplication>
#include <QtSql>
#include <QtDebug>
#include <QDir>
const QString dbPathName = "/export/home/hello/demo.db";
class sqlExample
{
public:
sqlExample();
void insertDemoData();
void replaceDemoData();
private:
QSqlDatabase m_demo_db;
void createDb();
};
sqlExample::sqlExample()
{
createDb();
}
void sqlExample::replaceDemoData()
{
/** associate db with query **/
QSqlQuery query( m_demo_db );
unsigned int chan = 555;
unsigned int uqId = 32151;
qDebug()<<__PRETTY_FUNCTION__;
/** To ensure atomic operations - failure defensive **/
QSqlDatabase::database().transaction();
//QString queryString = "update dttServiceList SET channelNum = " + QString::number(chan) + " where Id = " + QString::number(uqId);
QString queryString = "DELETE FROM dttServiceList where Id = " + QString::number(uqId);
query.prepare(queryString);
if ( query.exec() == false)
{
qDebug()<<"Query replaceDemoData Failed!!";
}
QSqlDatabase::database().commit();
}
void sqlExample::insertDemoData()
{
/** associate db with query **/
QSqlQuery query ( m_demo_db );
qDebug()<<__PRETTY_FUNCTION__;
qDebug()<<"Insert or replace Data here";
/** To ensure atomic operations - failure defensive **/
QSqlDatabase::database().transaction();
for( int i= 100, j = 32131; i<= 120; i++,j++)
{
query.prepare("INSERT OR REPLACE INTO dttServiceList values (?, ?, ?)");
query.bindValue(0, j);
query.bindValue(2, i);
query.bindValue(1, "Action");
if ( query.exec() == false)
{
qDebug()<<"Query Insert or replace dttServiceList Failed!!";
}
query.prepare("INSERT OR REPLACE INTO dttEventList values (?, ? , ?)");
query.bindValue(0, 5);
query.bindValue(1, "Jackie-Cheng");
/** foreign key **/
query.bindValue(2, j);
if ( query.exec() == false)
{
qDebug()<<"Query Insert or replace dttEventList Failed!!";
}
}
QSqlDatabase::database().commit();
qDebug()<<"Insert or replace data exit";
}
void sqlExample::createDb()
{
#if 1
QDir dir;
qDebug()<<"Creating demo.db";
/** create db here **/
m_demo_db = QSqlDatabase::addDatabase("QSQLITE", dbPathName);
m_demo_db.setConnectOptions("QSQLITE_BUSY_TIMEOUT=100000");
m_demo_db.setDatabaseName(dbPathName);
/** return 0 is error here **/
if(! dir.exists(dbPathName) )
{
qDebug()<<"Start transaction demo.db";
if(!m_demo_db.open())
{
qDebug()<<"Error in opening demodb";
}
/** In DDL no transaction is required **/
/** associate db with query **/
QSqlQuery query ( m_demo_db );
/** Create Table for storing user preference LCN for DTT **/
qDebug()<<"Create Table postcode.db";
query.prepare(" CREATE TABLE dttServiceList (Id INTEGER PRIMARY KEY, attrib varchar(20), channelNum integer )" );
if ( false == query.exec())
{
qDebug()<<"Create dttServiceList table failed";
}
/** The Id in the dttEventList is the foreign key and is associated with the table dttServiceList attribute Id **/
query.prepare(" CREATE TABLE dttEventList (EventId INTEGER PRIMARY KEY, programName varchar(20), \
Id INTEGER, FOREIGN KEY(Id) REFERENCES dttServiceList(Id) ON DELETE CASCADE )" );
if ( false == query.exec())
{
qDebug()<<"Create dttEventList table failed";
}
/** Do Indexing on ChannelId **/
qDebug()<<"Create Indexing demo.db -dttEventList";
query.prepare(" CREATE INDEX pEventId ON dttEventList (EventId)" );
if ( false == query.exec())
{
qDebug()<<"Indexing dttEventList failed";
}
/** Do Indexing on ChannelId **/
qDebug()<<"Create Indexing demo.db";
query.prepare(" CREATE INDEX pIndex ON dttServiceList (Id)" );
if ( false == query.exec())
{
qDebug()<<"Indexing dttServiceList failed";
}
/* Transaction complete **/
/** In DDL no transaction is required **/
}
else
{
qDebug()<<"Existing db..open!";
if(!m_demo_db.open())
{
qDebug()<<"Error in opening demoDb..already exists!";
}
}
#endif
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
sqlExample *p_sqlInstance = new sqlExample;
p_sqlInstance->insertDemoData();
p_sqlInstance->replaceDemoData();
qDebug()<<"Sql Function Executed";
delete p_sqlInstance;
return a.exec();
}