【问题标题】:Query in Genie + SQLiteGenie + SQLite 中的查询
【发布时间】:2016-12-27 00:01:29
【问题描述】:

我正在使用 Genie + SQLite 进行练习,但当我尝试进行查询时却被阻止了。

uses Sqlite

init    
    db : Sqlite.Database
    Sqlite.Database.open ("agenda.db3", out db)

    db.exec ("CREATE TABLE Contactos (pkiD INTEGER PRIMARY KEY, nombre TEXT UNIQUE, phone INTEGER)")

    stdout.printf( "Nuevo contacto: " )
    contacto_nombre:string = stdin.read_line()

    stdout.printf( "Teléfono: " )
    contacto_phone:string = stdin.read_line()

    enter:string = @"INSERT INTO Contactos (nombre, phone) VALUES ('$contacto_nombre', $contacto_phone)"

    // if contacto_nombre exists  ????????????
        //"SELECT nombre FROM Contactos WHERE nombre = 'contacto_nombre'"   ????
        //stdout.printf("%s ya está en la Agenda.\n", contacto_nombre)
    //else
        db.exec (enter)

如何检查某个值是否已存在?谢谢。

【问题讨论】:

    标签: sqlite genie


    【解决方案1】:

    好吧,我不知道这是否是最好的方法,但我找到了这个解决方案。

    uses
        Sqlite
        Gee
    
    init    
        db : Sqlite.Database
        Sqlite.Database.open ("agenda.db3", out db)
    
        db.exec ("CREATE TABLE Contactos (pkiD INTEGER PRIMARY KEY, nombre TEXT UNIQUE, phone INTEGER)")
    
        stdout.printf( "Nuevo contacto: " )
        contacto_nombre:string = stdin.read_line()
    
        statement:Statement
        db.prepare_v2("SELECT nombre FROM Contactos", -1, out statement)
    
        cols:int = statement.column_count ()
    
        var row = new dict of string, string
        item:int = 1
    
        var lista = new list of string
    
        while statement.step() == ROW
            for i:int = 0 to (cols - 1)
                row[ statement.column_name( i ) ] = statement.column_text( i )
                lista.add(row[ "nombre" ])
            item++
        if lista.contains(contacto_nombre) == true
            stdout.printf("%s ya está en la Agenda.\n", contacto_nombre)    
        else
            stdout.printf( "Teléfono: " )
            contacto_phone:string = stdin.read_line()
            enter:string = @"INSERT INTO Contactos (nombre, phone) VALUES ('$contacto_nombre', $contacto_phone)"
            db.exec (enter)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多