我在QT-SQL中创建了一个有两个表的程序。感兴趣的领域是查看DDL - 数据定义语言部分。这是在createDb()中。但是,为了编译,我已经更新了整个文件。我的目标是当更新表dttServiceList时,应更新dttEventList的Id。我在这里使用过级联。但是,我看到在dttServiceList中插入记录时,记录未插入dttEventList中。我在这里错过了什么?级联规则就在这里。我正在使用qt-sqlite。我也试过在这里添加触发器,但是我的sql浏览器挂了。
CREATE or REPLACE TRIGGER up1
AFTER INSERT ON dttServiceList
BEGIN
INSERT INTO dttEventList values (0,"action",1);
END;
另一部分是,那里的外键有什么意义,级联部分?
#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 = 32135;
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);
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 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 UPDATE 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();
}
答案 0 :(得分:1)
ON UPDATE CASCADE
仅适用于实际的UPDATE
语句。
(INSERT OR REPLACE是DELETE和INSERT的组合。)
没有级联INSERT
的功能。
如果要插入多个相关记录,则必须插入所有相关记录。