2

我使用 python 脚本mysqldump以格式导出数据库。sql.gz如何sql.gz使用此 python 脚本导入该文件?

我编写了适用于.sql.txt文件的代码。

import MySQLdb


filename = self.ui.txtFileLocationImport.text()

# Open database connection
db = MySQLdb.connect(dbHostName, dbUser, dbPassword, databaseName)

# prepare a cursor object using cursor() method
cursor = db.cursor()

#Prepare SQL query to INSERT a record into the database.
f = open(filename , 'r')
try:
    cursor.execute('SET foreign_key_checks = 0')
    for sql in f:
        if sql == '\n' or sql[0] == '/':
            pass
        else:
            # Execute the SQL command
            cursor.execute(str(sql))

    #Commit your changes in the database
    cursor.execute('SET foreign_key_checks = 1')
    db.commit()
except Exception as e:
   print e
   # Rollback in case there is any error
   db.rollback()

#close file
f.close()
# disconnect from server
db.close()

这仅适用于.txt.sql文件。我怎样才能让它读取一个sql.gz文件?

4

1 回答 1

1

gzip模块的内容。

于 2012-07-04T14:20:56.587 回答