PhoneGap sql检查重复项

时间:2017-03-18 18:02:23

标签: sql forms cordova duplicates

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

    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,[]);
    }``

1 个答案:

答案 0 :(得分:1)

创建名称为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
);

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