PhoneGap sql 检查重复项



我想输入一个查询,以便在将数据插入数据库时检查数据库是否重复,这样就可以防止在数据库中多次输入活动名称

    function insertQueryDB(tx) {
    var myDB = window.openDatabase("test", "1.0", "Test DB", 1000000);
    tx.executeSql('CREATE TABLE IF NOT EXISTS dataEntryTb (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, activityName TEXT NOT NULL, location TEXT NOT NULL, time NOT NULL, date NOT NULL, reporter NOT NULL)');
        var an = document.forms["myForm"]["activityName"].value;
        var l = document.forms["myForm"]["location"].value;
        var t = document.forms["myForm"]["time"].value;
        var d = document.forms["myForm"]["date"].value;
        var r = document.forms["myForm"]["reporter"].value;
        var query = 'INSERT INTO dataEntryTb ( activityName, location, time, date, reporter) VALUES ( "'+an+'", "'+l+'", "'+t+'", "'+d+'", "'+r+'")';
        navigator.notification.alert("Retrieved the following: Activity Name="+an+" and Location="+l);
        tx.executeSql(query,[]);
    }``

创建名称unique的表:

CREATE TABLE IF NOT EXISTS dataEntryTb (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    activityName TEXT NOT NULL UNIQUE,
    location TEXT NOT NULL,
    time NOT NULL, date NOT NULL,
    reporter NOT NULL
);

然后,如果名称已在表中,则数据库将返回错误。

最新更新