Python3 实现 从MySQL数据库中将记录转移到Sqlite中

#导入数据库驱动
import mysql.connector
import sqlite3
import pymysql
import re
'''
配置mysql数据库
'''

replacement_patterns = [
(r'won\'t', 'will not'),
(r'can\'t', 'cannot'),
(r'i\'m', 'i am'),
(r'ain\'t', 'is not'),
(r'(\w+)\'ll', '\g<1> will'),
(r'(\w+)n\'t', '\g<1> not'),
(r'(\w+)\'ve', '\g<1> have'),
(r'(\w+)\'s', '\g<1> is'),
(r'(\w+)\'re', '\g<1> are'),
(r'(\w+)\'d', '\g<1> would'),
    (r"s\'",'s'),
    (r"\'", ''),
(r"O\'", ''),
    (r"o\'", '')]

class RegexpReplacer(object):
    def __init__(self, patterns=replacement_patterns):
        self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
    def replace(self, text):
        s = text
        for (pattern, repl) in self.patterns:
            (s, count) = re.subn(pattern, repl, s)
        return s



mydb = mysql.connector.connect(
    host="localhost",  # 数据库主机地址
    user="root",  # 数据库用户名
    passwd="root",  # 数据库密码
    database = "suibian"
)
#创建mysql游标
mycursor = mydb.cursor()
#创建sqlite游标
#一定要填写绝对路径
conn = sqlite3.connect("D:/桌面/VOA/0.VOA常速1 (3)/VOA常速/bin/Debug/ls.sqlite")
cursor = conn.cursor()
#插入一条记录
sql = "INSERT INTO bbc (Id,Pic,Title,Title_cn,Sound,Type,Category,CreateTime,Flag,DescCn,CategoryName) VALUES "
mycursor.execute("select * from  newstitle")
myresult = mycursor.fetchall()
for x in myresult:
    replacer = RegexpReplacer()
    pic = replacer.replace(x[10])
    title = replacer.replace(x[1])
    title_cn = replacer.replace(x[5])
    creatTime = replacer.replace(x[12])
    DescCn = replacer.replace(x[2])
    CategoryName = replacer.replace(x[6])
    print("x[0]:",x[0])
    print("pic:",pic)
    print("title:",title)
    print("title_cn:",title_cn)
    print("x[8]:",x[8])
    print("creatTime:",creatTime)
    print("x[16]:",x[16])
    print("DescCn:",DescCn)
    print("CategoryName:",CategoryName)
    sql = "INSERT INTO bbc(Id,Pic,Title,Title_cn,Type,      Category,  CreateTime,Flag,DescCn,CategoryName,Sound) " \
                  "VALUES (%s,'%s','%s',  '%s'  , '%s',        %s      ,'%s'       ,%s , '%s',     '%s'   ,'%s')" %\
                         (x[0],pic,title,title_cn,"LiveScience",1,     creatTime,x[16],DescCn,CategoryName,x[8])
    try:
        cursor.execute(sql)
        print("\033[0;36;40m", "插入新闻内容成功", "\033[0m")
        conn.commit()
    except Exception as e:
        print("\033[0;31;40m", "插入失败:", e, "\033[0m","打印:::::",  sql)
        conn.rollback()




sql = "select * from bbc"
cursor.execute(sql)
values = cursor.fetchall()
for value in values:
    print(value)

conn.close()
# cursor.close()
mycursor.close()


#导入数据库驱动
import mysql.connector
import sqlite3
import pymysql
import re
'''
配置mysql数据库
'''
replacement_patterns = [
(r'won\'t', 'will not'),
(r'can\'t', 'cannot'),
(r'i\'m', 'i am'),
(r'ain\'t', 'is not'),
(r'(\w+)\'ll', '\g<1> will'),
(r'(\w+)n\'t', '\g<1> not'),
(r'(\w+)\'ve', '\g<1> have'),
(r'(\w+)\'s', '\g<1> is'),
(r'(\w+)\'re', '\g<1> are'),
(r'(\w+)\'d', '\g<1> would'),
(r"s\'",'s'),(r"\'", ''),
(r"O\'", ''),(r"o\'", '')]

class RegexpReplacer(object):
    def __init__(self, patterns=replacement_patterns):
        self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
    def replace(self, text):
        s = text
        for (pattern, repl) in self.patterns:
            (s, count) = re.subn(pattern, repl, s)
        return s

mydb = mysql.connector.connect(
    host="localhost",  # 数据库主机地址
    user="root",  # 数据库用户名
    passwd="root",  # 数据库密码
    database = "suibian"
)
#创建mysql游标
mycursor = mydb.cursor()
#创建sqlite游标
#一定要填写绝对路径
conn = sqlite3.connect("D:/桌面/VOA/0.VOA常速1 (3)/VOA常速/bin/Debug/ls.sqlite")
cursor = conn.cursor()
#插入一条记录
sql = "INSERT INTO bbc (Id,Pic,Title,Title_cn,Sound,Type,Category,CreateTime,Flag,DescCn,CategoryName) VALUES "
sql2 = "INSERT INTO bbc (NewsID,ParaId,IdIndex,Timing,EndTiming,Sentence,ImgWords,ImgPath,ImgDesc,Sentence_cn,Sentence_jp,primary key, AudioSrc)"

# mycursor.execute("select * from  newstitle")
mycursor.execute("select * from newstext")

myresult = mycursor.fetchall()
for x in myresult:
    rep = RegexpReplacer()
    Sentence = rep.replace(x[5])
    ImgPath = rep.replace(x[7])
    print("ImgPath:", ImgPath)
    ImgDesc = rep.replace(x[8])
    sql = "INSERT INTO newstext(NewsID,ParaId,IdIndex,Timing,EndTiming,Sentence,ImgWords,ImgPath,ImgDesc,Sentence_cn,Sentence_jp, AudioSrc) " \
                         "VALUES ('%s'   ,'%s',      '%s',    '%s'  , '%s',    '%s'   ,   '%s'  ,  '%s' , '%s',     '%s'    ,  '%s',  '%s')" % \
          (x[0], x[1],   x[2],  x[3] , x[4] , Sentence, x[6], pymysql.escape_string(ImgPath), ImgDesc, x[9], x[10] , x[12])
    try:
        cursor.execute(sql)
        print("\033[0;36;40m", "插入新闻内容成功", "\033[0m")
        conn.commit()
    except Exception as e:
        print("\033[0;31;40m", "插入失败:", e, "\033[0m","打印:::::\n",  sql)
        conn.rollback()
sql = "select * from newstext"
cursor.execute(sql)
values = cursor.fetchall()
for value in values:
    print(value)

conn.close()
cursor.close()
mycursor.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Philtell

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值