2017-03-18 55 views
0

我想输入一个查询将数据插入到数据库时检查重复的数据库,以便它可能会妨碍活动名称被多次输入一个数据库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,[]); 
    }`` 
+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 
); 

这时如果名称已在该表的数据库会返回一个错误。

+0

我希望能够让用户知道在将值插入表单时让他们知道活动名称已存在的错误 – lifeishard

+0

@lifeishard。 。 。只需在'insert'失败时捕获错误。 –