sqlite - 在Genie + SQLite中查询

标签 sqlite genie

我正在与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)


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

最佳答案

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

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)

关于sqlite - 在Genie + SQLite中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337440/

相关文章:

ios - 更新应用程序后,应用程序在 sqlite 上崩溃

php - 拉维尔 5.1 : Enable SQLite foreign key constraints

sqlite - 使用 sqlite 作为 angular v7 的数据库

lambda - Genie 中 vala lambda 的替代品

compiler-errors - 转让所有权(Genie/Vala)

lambda - Valas 闭包如何映射到 Genie?

vala - 我如何将该程序从 Vala 翻译为 Genie?

java - Android 批量插入或更新而不是 insertOrReplace

Python SQLite3 SELECT 中的多个变量