sqlite 60000行 插入到数据库只用不到2秒
我的数据库 名字叫dic.db 和程序放在一起
CREATE TABLE [dic]( [ID] INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE ON CONFLICT REPLACE, [单词] TEXT UNIQUE ON CONFLICT REPLACE, [英式音标] TEXT COLLATE NOCASE, [美式音标] TEXT COLLATE NOCASE, [解释] TEXT);
所以拼出来的插入sql字符串应该是这样的
insert or replace into [dic] (单词,英式音标,美式音标,解释) values (‘zooks‘,‘[zu?ks]‘,‘[zu?ks]‘,‘用作温和誓愿的感叹短语;‘);
C#代码
//批量Insert Into private void buttonInsertInto_Click(object sender, EventArgs e) { sw.Reset(); sw.Start(); string ConnectionString = "Data Source = dic.db; Version=3;"; SqliteHelper sqlite = new SqliteHelper(ConnectionString); sqlite.BatchExecuteCmd(richTextBox4.Lines); sw.Stop(); MessageBox.Show("批量插入SQL语句耗时:" + ( sw.ElapsedMilliseconds / 1000f ).ToString("0.0000 sec")); } //纯替换 Stopwatch sw = new Stopwatch(); private void buttonReplace_Click(object sender, EventArgs e) { sw.Start(); //用的是替换 速度是瞬间的 没有判断,先移除所有空行 //瞬间插入6万行 用替换的办法 //zookeeper ['zu?ki?p?(r)] ['zu?ki?p?r] n.动物园管理员; //替换成这样 //insert or replace into [dic] (单词,英式音标,美式音标,解释) //values (‘zookeeper‘,‘['zu?ki?p?(r)]‘,‘ ['zu?ki?p?r]‘,‘n.动物园管理员;‘); string s = richTextBox4.Text; //1/3最左边替换成 insert or replace into [dic] (单词,英式音标,美式音标,解释) values (‘ Regex r = new Regex("^", RegexOptions.Multiline); s = r.Replace(s, "insert or replace into [dic] (单词,英式音标,美式音标,解释) values (‘"); //2/3 tab 替换成 ‘,‘ 下面的空格并不是空格而是复制的tab空格 r = new Regex(" ", RegexOptions.Multiline); s = r.Replace(s, "‘,‘"); //3/3最右边替换成 ‘); r = new Regex("$", RegexOptions.Multiline); s = r.Replace(s, "‘);"); richTextBox4.Text = s; sw.Stop(); MessageBox.Show( "替换耗时:" + ( sw.ElapsedMilliseconds / 1000f ).ToString("0.0000 sec")); }61991行,光粘贴进来都用了2秒钟
61991行光标替换了就用了7.8秒,如果用for的话 得小时级别的,得要6个小时、得要7个小时?
批量执行SQL命令只用了1.2秒
/// <summary> /// 转换后的SQL格式语句,事物 执行语句语句 这个语句是瞬间完成的即使是1万条也是这样 /// 一句一句转换成SQL语句是相当的慢的 /// INSERT INTO 表2 (Keyword,Trans,Phrase) VALUES (‘undoubtedly‘,‘解释‘,‘短语‘); /// </summary> /// <param name="sqlArr">每一行都是完整的 INSERT INTO语句,每插入一条</param> /// <returns>返回异常的SQL语句</returns> public string BatchExecuteCmd(string[] sqlArr) { StringBuilder sb = new StringBuilder(); using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { SQLiteCommand cmd = new SQLiteCommand(conn); { conn.Open(); using (DbTransaction trans = conn.BeginTransaction()) { for (int i = 0; i < sqlArr.Length; i++) { try { cmd.CommandText = sqlArr[i]; cmd.ExecuteNonQuery(); } catch { //下面是插入失败的内容 sb.Append(sqlArr[i] + "\n"); //trans.Rollback();//回滚事务 //MessageBox.Show(EX.Message); } } trans.Commit(); //richTextBox2.Text = sb.ToString(); } } } return sb.ToString(); } }
sqlite批量插入速度是相当快的,而转换成sql语句是相当耗时(用for一行一行遍历,然后再把每行分割开来,这个过程相当耗时,因为这里有6万行,什么不做光跑完一遍就得个把小时)。
相关推荐
DAV数据库 2020-06-17
airfling 2020-05-31
zbcaicai 2020-05-26
beibeijia 2020-04-25
Rain 2020-04-16
Plant 2020-04-08
园搬家测试账号 2020-03-25
MFCJCK 2020-02-24
xiaoxiangyu 2020-02-23
Plant 2020-02-03
CharlesYooSky 2020-02-01
MFCJCK 2020-01-31
nxcjh 2020-01-29
wintershii 2020-01-18
zbcaicai 2020-01-03
MFCJCK 2019-12-30
airfling 2019-12-28
Dlanguage 2019-12-27
Dlanguage 2019-12-25
whyname 2019-12-23