我想看看我的sqlite數據庫中是否有現有的行,如果沒有添加它。但是我的查詢出了問題。我希望有人能夠發現我的錯誤。php sqlite檢查行是否存在
我特別瘦的誤差在查詢行:
$query = 'SELECT * FROM plant WHERE common_Name = '. $commonName . ' AND latin_Name = '. $latinName . 'AND url = '. $URL ;
謝謝 託德
// set path of database file
$file = "db/plants.db";
// create database object
$db = new SQLiteDatabase($file) or die("Could not open database");
// see if the EXACT same tag exists
$query = 'SELECT * FROM plant WHERE common_Name = '. $commonName . ' AND latin_Name = '. $latinName . 'AND url = '. $URL ;
$result = $db->query($query) or die("Error in query");
$rows = sqlite_num_rows($result);
if($rows>0) return; //do not add it
// generate query string
$query = 'INSERT INTO plant (common_Name, latin_Name, url) VALUES("'.$commonName.'","'. $latinName.'","'.$URL.'")';
// execute query
// return result object
$result = $db->query($query) or die("Error in query");
// destroy database object
unset($db);
這應該回答你的問題:http://stackoverflow.com/questions/531035/how-to-do-if-not-exists-in-sqlite –
你試過對象 - 面向風格?即$ rows = $ result-> numRows();不知道它是否會有所作爲... –
@ Inner-Design:我是新來的sqllite,並且正在慢慢地學習OOP sqlite api。 – maddogandnoriko