美文网首页
2020-06-09 快速插入 mysql Oracle

2020-06-09 快速插入 mysql Oracle

作者: 早起早起早起up | 来源:发表于2020-06-09 11:03 被阅读0次

    1.对于插入最好方法是 1000条一次执行

    2.对于mysql INSERT INTO TBL_TEST (id) VALUES (1), (2), (3)

    3.对于oracle

            insert into Student(id, name, sex, age, tel)
    select '27', 'jack', '男', 22, '13345674567' from dual
    
          union select '35', 'jack', '男', 22, '13345674567' from dual
      union select '36', 'jack', '男', 32, '13345674567' from dual
      union select '37', 'jack', '男', 22, '13345674567' from dual
      union select '38', 'jack', '男', 32, '13345674567' from dual
      union select '39', 'jack', '男', 22, '13345674567' from dual
      union select '40', 'jack', '男', 32, '13345674567' from dual
      union select '41', 'jack', '男', 22, '13345674567' from dual
      union select '42', 'jack', '男', 32, '13345674567' from dual
    
      union select '43', 'jack', '男', 22, '13345674567' from dual
      union select '44', 'jack', '男', 32, '13345674567' from dual
      union select '45', 'jack', '男', 22, '13345674567' from dual
      union select '46', 'jack', '男', 32, '13345674567' from dual
    
      union select '47', 'jack', '男', 22, '13345674567' from dual
      union select '48', 'jack', '男', 32, '13345674567' from dual
      union select '49', 'jack', '男', 22, '13345674567' from dual
      union select '50', 'jack', '男', 32, '13345674567' from dual
    
      union select '60', 'jack', '男', 32, '13366676667' from dual
      union select '61', 'jack', '男', 22, '13366676667' from dual
      union select '62', 'jack', '男', 32, '13366676667' from dual
    
      union select '63', 'jack', '男', 22, '13366676667' from dual
      union select '64', 'jack', '男', 32, '13366676667' from dual
      union select '65', 'jack', '男', 22, '13366676667' from dual
      union select '66', 'jack', '男', 32, '13366676667' from dual
    
      union select '67', 'jack', '男', 22, '13366676667' from dual
      union select '68', 'jack', '男', 32, '13366676667' from dual
      union select '69', 'jack', '男', 22, '13366676667' from dual
    

    python 处理 mysql 文件

    # -*- coding:utf-8 -*-
    from tkinter import *
    import tkinter.filedialog
    import pymysql.cursors
    
    def xz():
        filename1=tkinter.filedialog.askopenfilename()
        if filename1 != '':
             lb.config(text=u'您选择的文件是'+filename1)
             return  filename1
        else:
             lb.config(text=u'您没有选择任何文件')
    
    
    def demo1(res):
        # print(11)
        with open(res, encoding='utf-8', mode='r') as f:
            # 读取整个sql文件,以分号切割。[:-1]删除最后一个元素,也就是空字符串
            sql_list = f.read().split(';')[:-1]
            print(len(sql_list))
            sql_item=''
            num=1
            str1=''
            list=[]
            for i in range(0,len(sql_list)):
                if num<1000:
                    if str1=='':
                        str1=sql_list[i]
                        num+=1
                    else:
                        str1+=','+sql_list[i].split('VALUES')[1]
                        num+=1
                else:
                    str1+=';'
                    runsq(str1)
                    num=1
                    str1=""
    
            runsq(str1)
            print(str1)
            lb = Label(root, text='')
            lb.config(text=u'插入完毕')
            lb.pack()
    
    
            # for x in sql_list:
            #     print(x)
                # 判断包含空行的
            #     if '\n' in x:
            #         # 替换空行为1个空格
            #         x = x.replace('\n', ' ')
            #         x=x+';'
            #     # sql语句添加分号结尾
            #         sql_item +=x
            #     sql_item += x
            # print(sql_item)
    
    
    
    
    
    
    
    def runsq(sql):
        print(123)
        db = pymysql.connect("localhost", "user", "passwd", "database")
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 插入语句
        print(333333)
    
    
    
        try:
            # 执行sql语句
            cursor.execute(sql)
            # 提交到数据库执行
            db.commit()
            print(22222)
        except:
            # 如果发生错误则回滚
            db.rollback()
        # 关闭数据库连接
        db.close()
    
    root = Tk()
    lb = Label(root,text='')
    lb.pack()
    btn=Button(root,text=u'选择第一个文件',command= xz)
    btn.pack()
    
    res=xz()
    
    button1 = Button(root,text=u"下载", bg="lightblue", width=10,
                                          command=lambda :demo1(res))  # 调用内部方法  加()为直接调用
    button1.pack()
    root.mainloop()
    
    
    

    相关文章

      网友评论

          本文标题:2020-06-09 快速插入 mysql Oracle

          本文链接:https://www.haomeiwen.com/subject/kxhstktx.html