How to use single transactions for multiple statements?

问题内容:

I have a function which makes N statements, the problem is that they are not in a single transaction and its really slow.

this._db.exec("BEGIN TRANSACTION;");
        for (let i = 0; i < data.length; i++) {
            let obj = data[i];
            let inserStatement = `
                INSERT INTO TableTest1236 (UID, DirectoryTreeHID, ProductDimensionUID, Active, SortOrder, LastEditOn, ColorID, PlaceHolder, TableScheduleUID) 
                VALUES ('` + obj['UID'] + `', '` + obj['DirectoryTreeHID'] + `', '` + obj['ProductDimensionUID'] + `', '` + obj['Active'] + `', '` + obj['SortOrder'] + `', '` + obj['LastEditOn'] + `', '` + obj['ColorID'] + `', '` + obj['PlaceHolder'] + `', '');
            `;
            let updateStatement = "UPDATE TableTest1236 SET Active = 5 WHERE UID = '" + obj['UID'] + "'";
            let selectStatement = "SELECT * FROM TableTest1236 WHERE UID = '" + obj['UID'] + "'";

            results.push(new Promise((resolve, reject) => {
                    self._db.get(selectStatement, (err, row) => {
                        if (row) {
                            self._db.run(updateStatement, (err) => {
                                if (err) reject(err);

                                resolve(true);
                            })
                        } else {
                            self._db.run(inserStatement, (err) => {
                                if (err) reject(err);

                                resolve(true);
                            })
                        }
                    })
                })
            )
        }
        this._db.exec("END TRANSACTION;");

How to force sqlite to use one transaction only?

问题评论:

原文地址:

https://stackoverflow.com/questions/47756410/how-to-use-single-transactions-for-multiple-statements

添加评论