1
我是MySQL的新手,我想在插入記錄前檢查用戶是否存在於數據庫中,但是我得到了這個錯誤1064,SQL語法錯誤。 MySQL的聲明在使用ASP.Net插入MySQL時出錯
IF (SELECT count(*) FROM tvstream.t_users WHERE fb_id=`123456`)=0 THEN
Begin
INSERT INTO `tvstream`.`t_users` (`fb_id` ,`user_name` ,`email` ,`gender`)
Values (`123456`,`Daina`,`[email protected]`,`Female`)
End
ASP代碼
StringBuilder SQL_Add = new StringBuilder("IF (SELECT count(*) FROM tvstream.t_users WHERE fb_id=`" + fb_id +"`)=0 THEN ");
SQL_Add.Append("Begin ");
SQL_Add.Append("INSERT INTO `tvstream`.`t_users` (`fb_id` ,`user_name` ,`email` ,`gender`)");
SQL_Add.Append(" Values (`" + fb_id + "`,`" + user_name + "`,`" + email + "`,`" + gender + "`)");
SQL_Add.Append(" End");
Response.Write(SQL_Add.ToString());
MySqlConnection m_cnn = new MySqlConnection(myConnection);
m_cnn.Open();
MySqlCommand cmd = new MySqlCommand(SQL_Add.ToString());
cmd.Connection = m_cnn;
cmd.ExecuteNonQuery();
請指點,謝謝您
可能重複的[防止重複的數據庫項](https://stackoverflow.com/questions/40478342/prevent-duplicate-entries-to-database) – e4c5