//从数据库去除掉当前关键字的doing状态。一般用于关闭项目。 public void ClearDoingFromDatabase() { try { String dictionary_table = projectInfo.db_dictionary_table.Trim() == "" ? "tb_keywords" : projectInfo.db_dictionary_table.Trim(); HunterDatabaseHelper hdh = new HunterDatabaseHelper(projectInfo); Database db = hdh.GetDatabaseInstance(); String keyword = GetCurrentKeyword(CurrentKeywordProgress); db.DbOpen(); StringBuilder keyword_condition = new StringBuilder(); keyword_condition.Append("SELECT key_typeandengine FROM " + dictionary_table + " WHERE key_value = \"" + GetKeyword(CurrentKeywordProgress).Replace("'", "\\'") + "\" LIMIT 1;"); MySqlCommand cmd = new MySqlCommand(keyword_condition.ToString(), db.mysql_connection); String result = cmd.ExecuteScalar().ToString(); StringBuilder sb_typeandengine = new StringBuilder(result); if (result != null) { sb_typeandengine.Replace("(" + Filetype + ":" + StrategyData.information.Uri + ":" + DictionaryState.doing + ");", ""); String str_cmd = String.Format("UPDATE {2} SET key_typeandengine = '{0}' WHERE key_value = '{1}'", sb_typeandengine.ToString(), keyword.Replace("'", "\\'"), dictionary_table); cmd = new MySqlCommand(str_cmd, db.mysql_connection); cmd.ExecuteNonQuery(); } projectInfo.mHunterConsole.WriteDetails("辞典数据库已关闭。"); } catch (Exception ex) { projectInfo.mHunterConsole.WriteException(ex); } }
public void UpdateKeywordToDatabase(String keyword, DictionaryState state, bool replace = false) { if (projectInfo.remote_dictionary) { String dictionary_table = projectInfo.db_dictionary_table.Trim() == "" ? "tb_keywords" : projectInfo.db_dictionary_table.Trim(); //网络辞典模式 HunterDatabaseHelper hdh = new HunterDatabaseHelper(projectInfo); Database db = hdh.GetDatabaseInstance(); try { db.DbOpen(); StringBuilder keyword_condition = new StringBuilder(); keyword_condition.Append("SELECT key_typeandengine FROM " + dictionary_table + " WHERE key_value = \"" + GetKeyword(CurrentKeywordProgress).Replace("'", "\\'") + "\" LIMIT 1;"); MySqlCommand cmd = new MySqlCommand(keyword_condition.ToString(), db.mysql_connection); String result = cmd.ExecuteScalar().ToString(); StringBuilder sb_typeandengine = new StringBuilder(result); if (result != null) { if (!replace) { sb_typeandengine.Append("(" + Filetype + ":" + StrategyData.information.Uri + ":" + state + ");"); } else { sb_typeandengine.Replace("(" + Filetype + ":" + StrategyData.information.Uri + ":" + DictionaryState.doing + ");", "(" + Filetype + ":" + StrategyData.information.Uri + ":" + state + ");"); } String str_cmd = String.Format("UPDATE {2} SET key_typeandengine = '{0}' WHERE key_value = '{1}'", sb_typeandengine.ToString(), keyword.Replace("'", "\\'"), dictionary_table); cmd = new MySqlCommand(str_cmd, db.mysql_connection); cmd.ExecuteNonQuery(); } else { String str_cmd = String.Format("INSERT INTO {2} (key_value, key_typeandengine) VALUES ('{0}','{1}')", keyword.Replace("'", "\\'"), "(" + Filetype + ":" + StrategyData.information.Uri + ":" + state + ");", dictionary_table); cmd = new MySqlCommand(str_cmd, db.mysql_connection); cmd.ExecuteNonQuery(); } projectInfo.mHunterConsole.WriteDetails("辞典中的关键字信息成功添加到数据库中。"); } catch (Exception ex) { projectInfo.mHunterConsole.WriteException(ex); projectInfo.mHunterConsole.WriteException(new Exception("上传关键字信息到数据库失败。")); } finally { try { db.DbClose(); } catch { } } } }
public void LoadKeywords() { Keywords.Clear(); if (projectInfo.remote_dictionary) { String dictionary_table = projectInfo.db_dictionary_table.Trim() == "" ? "tb_keywords" : projectInfo.db_dictionary_table.Trim(); //网络辞典模式 HunterDatabaseHelper hdh = new HunterDatabaseHelper(projectInfo); Database db = hdh.GetDatabaseInstance(); CurrentKeywordProgress = 0; try { db.DbOpen(); StringBuilder keyword_condition = new StringBuilder(); String condition_type_engine_doing = "%(" + projectInfo.filetype + ":" + StrategyData.information.Uri + ":" + DictionaryState.doing + ");%"; String condition_type_engine_done = "%(" + projectInfo.filetype + ":" + StrategyData.information.Uri + ":" + DictionaryState.done + ");%"; keyword_condition.Append("SELECT * FROM " + dictionary_table + " WHERE (key_typeandengine NOT LIKE \"" + condition_type_engine_doing + "\" AND key_typeandengine NOT LIKE \"" + condition_type_engine_done + "\") OR key_typeandengine IS NULL LIMIT 1;"); MySqlCommand cmd = new MySqlCommand(keyword_condition.ToString(), db.mysql_connection); MySqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); Keywords.Add(dr["key_value"].ToString()); //读取一个词 UpdateKeywordToDatabase(Keywords[0], DictionaryState.doing); } else { //添加一个空词 projectInfo.mHunterConsole.WriteMessage("服务器上已经没有可用的关键字了。"); } dr.Close(); } catch (Exception ex) { projectInfo.mHunterConsole.WriteException(ex); projectInfo.mHunterConsole.WriteException(new Exception("连接远程辞典失败,因此开始重试连接远程辞典数据库。")); //MessageBox.Show("连接远程辞典失败,以后将采用本地辞典进行下载。若要调整回去,请修改项目文件的remote_dictionary值。", "Hunter 3", MessageBoxButtons.OK, MessageBoxIcon.Information); //projectInfo.remote_dictionary = false; try { db.DbClose(); } catch { } LoadKeywords(); }finally{ try{ db.DbClose(); }catch {} } } else { //手动辞典模式 try { FileStream fs = new FileStream(projectInfo.dictionary, FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.UTF8); Keywords.Clear(); string k = sr.ReadLine(); try { while (k != null) { Keywords.Add(k); k = sr.ReadLine(); } if (Keywords.Count == 0) { Keywords.Add(""); } } catch (Exception) { throw; } finally { fs.Close(); sr.Close(); } } catch (FileNotFoundException) { Keywords.Add(""); throw; } catch { throw; } } }