sql >> Database teknologi >  >> RDS >> Mysql

Hvordan kan jeg dumpe en MySQL-database uden at bruge mysqldump i Python

Jeg har løst dette problem.

import MySQLdb
import os
import datetime

con = MySQLdb.connect(host='localhost', user='root', passwd='password', db='test')
cur = con.cursor()

cur.execute("SHOW TABLES")
data = ""
tables = []
for table in cur.fetchall():
    tables.append(table[0])

for table in tables:
    data += "DROP TABLE IF EXISTS `" + str(table) + "`;"

    cur.execute("SHOW CREATE TABLE `" + str(table) + "`;")
    data += "\n" + str(cur.fetchone()[1]) + ";\n\n"

    cur.execute("SELECT * FROM `" + str(table) + "`;")
    for row in cur.fetchall():
        data += "INSERT INTO `" + str(table) + "` VALUES("
        first = True
        for field in row:
            if not first:
                data += ', '
            data += '"' + str(field) + '"'
            first = False


        data += ");\n"
    data += "\n\n"

now = datetime.datetime.now()
filename = str(os.getenv("HOME")) + "/backup_" + now.strftime("%Y-%m-%d_%H:%M") + ".sql"

FILE = open(filename,"w")
FILE.writelines(data)
FILE.close()

Ser ud til at fungere godt ud fra en lille test.



  1. Hvad betyder Clustered og Non-Clustered indeks egentlig?

  2. Hvorfor får jeg, at MySQL-fejlforespørgslen var tom?

  3. Forbinder Visual COBOL til MySQL

  4. MySQL Trigger - Lagring af en SELECT i en variabel