Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS中SQLite的竞争条件_Node.js_Sqlite_Node Sqlite3 - Fatal编程技术网

Node.js NodeJS中SQLite的竞争条件

Node.js NodeJS中SQLite的竞争条件,node.js,sqlite,node-sqlite3,Node.js,Sqlite,Node Sqlite3,我是NodeJS和数据库的新手。我对我的程序的行为感到困惑。我只是想创建一个表,然后删除它。但我的代码有点问题。结果真的很奇怪。我运行了几次代码,结果不同。示例结果: First execution [ { name: 'first' } ] createTable error SQLITE_ERROR: table first already exists [ { name: 'first' } ] table first dropped Second execution [] [] tab

我是NodeJS和数据库的新手。我对我的程序的行为感到困惑。我只是想创建一个表,然后删除它。但我的代码有点问题。结果真的很奇怪。我运行了几次代码,结果不同。示例结果:

First execution
[ { name: 'first' } ]
createTable error SQLITE_ERROR: table first already exists
[ { name: 'first' } ]
table first dropped

Second execution
[]
[]
table first(name text) created succesfully

Third execution
createTable error SQLITE_ERROR: table first already exists
[ { name: 'first' } ]
[ { name: 'first' } ]
table first dropped
代码如下所列。如果有人能指出我的错误,我将不胜感激

import { Database } from 'sqlite3';

class DBWrapper {
    private db: Database;
    constructor(dbName: string) {
        this.db = new Database(dbName, (error) => {
            if (error) {
                console.log('Database construction failed');
            } else {
                console.log('Database created successfully');
            }
        });
    }

    public createTable(tableName: string): void {
        this.db.serialize(() => {
            this.db.run('CREATE TABLE ' + tableName, (error) => {
                if (error) {
                    console.log('createTable error ' + error.message);
                } else {
                    console.log('table ' + tableName + ' created succesfully');
                }
            });
        });
        this.db.close();
        this.db = new Database('sqlitest');
    }

    public printTables(): void {
        this.db.serialize(() => {
            this.db.all('select name from sqlite_master where type=\'table\'', (error, tables) => {
                console.log(tables);
            });
        });
        this.db.close();
        this.db = new Database('sqlitest');
    }

    public clear(): void {
        this.db.serialize(() => {
            this.db.all('select name from sqlite_master where type=\'table\'', (error, tables) => {
                if (error) {
                    console.log('error in select all tables');
                } else {
                    tables.forEach((table) => {
                        this.db.run('DROP TABLE ' + table.name, (innerError) => {
                            if (innerError) {
                                console.log('drop table ' + table.name + ' error ' + innerError.message);
                            } else {
                                console.log('table ' + table.name + ' dropped');
                            }
                        });
                    });
                }
            });
        });
        this.db.close();
        this.db = new Database('sqlitest');
    }
}

const testDB = new DBWrapper('sqlitest');
testDB.createTable('first(name text)');
testDB.printTables();
testDB.clear();
testDB.printTables();

在启动下一个命令之前,需要等待每个命令完成

通常,db.serialize将为您执行此操作,但您在每个命令之后创建一个新的db实例,而该实例不知道来自前一个实例的挂起操作


您应该重新使用db实例。

不幸的是,这没有帮助。我已经在所有方法中删除了this.db.close和this.db=new数据库'sqlitest',但没有任何更改。