简体   繁体   English

nodejs中的变量sqlite3查询

[英]Variable sqlite3 query in nodejs

I'm building a simple form using node.js that asks for the things you'd expect: - First Name - Last Name - Address 我正在使用node.js构建一个简单的表单,询问你期望的东西: - 名字 - 姓氏 - 地址

and doing a query against a sqlite3 database with fields named firstName, lastName, address. 并使用名为firstName,lastName,address的字段对sqlite3数据库进行查询。 I would like to mix and match queries, though — you can search for just lastName OR firstName along with lastName OR lastName with portion of street address, etc. 我想混合和匹配查询,但是你可以搜索lastName OR firstName以及lastName或lastName以及街道地址的一部分等。

Writing separate queries for each case would be annoying, but I'm not sure how else to do it. 为每个案例编写单独的查询会很烦人,但我不确定如何做到这一点。 In code, the following works if I enter first name or last name: 在代码中,如果我输入名字或姓氏,则以下内容有效:

db.serialize(function() {
   db.each("SELECT rowid AS id, lastName, firstName FROM county WHERE firstName = ? OR lastName = ?", firstName, lastName, function(err, row) {
   console.log(row.id + ": " + row.firstName + ' ' + row.lastName);  
   }
}

but, if I enter first name and last name, I want that to be AND instead of only return rows where both match. 但是,如果我输入名字和姓氏,我希望它是AND而不是仅返回两者匹配的行。 If I do an AND in that clause, it returns nothing if I leave one of the fields blank. 如果我在该子句中执行AND,如果我将其中一个字段留空,则不返回任何内容。

Am I just missing a trick in constructing my query? 我只是错过了构建查询的技巧吗?

Thanks! 谢谢!

I think it's best to implement this in code. 我认为最好在代码中实现它。 Something like this: 像这样的东西:

var sqlite3 = require('sqlite3').verbose(),
    db = new sqlite3.Database('peeps.db');

var runQuery = function(request) {
    var select = 'select rowid AS id, firstName, lastName from country where ',
        query,
        params = []; 

    if (request.first && request.last) {
        query = select + "firstName=? and lastName=?"
        params.push( request.first, request.last );
    } else if (request.first) {
        query = select + "firstName=?"
        params.push( request.first );
    } else if (request.last) {
        query = select + "lastName=?"
        params.push( request.last );
    }   

    if (request.address) {
        params.push( request.address );

        if (!query) {
            query = select + " address like ?"; 
        } else {
            query = query + " and address like ?"; 
        }   
    }   

    db.each( query, params, function(err, row) {
        console.log( 'id:', row.id, row.firstName, row.lastName );
    }); 
};

var createTestData = function() {

    var stmt = db.prepare('insert into country values (?, ?, ?)');

    stmt.run('john', 'smith', '123 state street');
    stmt.run('jane', 'doe', '400 doe street');
    stmt.run('jeff', 'lebowski', 'dude ave, dudeville');

    stmt.finalize();
};

db.serialize(function() {
    db.run('create table if not exists country( firstName text, lastName text, address text )');
    // createTestData();

    var searches = [ 
        { first:'john', last:'smith' },
        { first:'jane' },
        { last:'lebowski' },
        { address:'%street%' }
    ];  

    searches.forEach(function(request) {
        runQuery( request );
    }); 
});

db.close();

This isn't as object-oriented as I would like but it demonstrates how to construct SQL statements for various query parameters... 这不像我想的那样面向对象,但它演示了如何为各种查询参数构造SQL语句......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM