473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

dump table and data with mysqldb

does anyone know how to dump the table structure and data from a mysql
database? I am connected to the database externally so "SELECT * FROM
database INTO OUTFILE file" doesn't work for me. However, external tools
have been able to do it (Mascon). I presume this might be a common thing
where people need to backup their databases programatically from MySQLdb.

Hoang Do
http://jotsite.com
Jul 18 '05 #1
4 13240
"Hoang" <tr@jotsite.com> wrote in message news:<Eh******************@newssvr14.news.prodigy. com>...
does anyone know how to dump the table structure and data from a mysql
database? I am connected to the database externally so "SELECT * FROM
database INTO OUTFILE file" doesn't work for me.


Here's a start..

def get_table_list(cursor):
cursor.execute("SHOW TABLES")
return [ table for table, in cursor ]
def get_table_schema(cursor, table):
cursor.execute("SHOW CREATE TABLE %s" % (table))
return cursor.fetchone()[1]
def get_structure_sql(db):
c = db.cursor()
c.execute("SET OPTION SQL_QUOTE_SHOW_CREATE=1")
schemas = {}

for table in get_table_list(c):
schemas[table] = get_table_schema(c, table)

return schemas
def get_db_name(db):
cursor = db.cursor()
cursor.execute("SELECT DATABASE()")
return cursor.fetchone()[0]
def dump_structure_sql(db):
print "#"
print "# Dumping schema for database", get_db_name(db)
print "#"
print
print

for table, create_def in get_structure_sql(db).iteritems():
print "#"
print "# Dumping schema for table", table
print "#"
print
print create_def
print
print

PS: You are aware of mysqldump, right?
Jul 18 '05 #2
"Hoang" <tr@jotsite.com> wrote in message
news:Eh******************@newssvr14.news.prodigy.c om...
does anyone know how to dump the table structure and data from a mysql
database? I am connected to the database externally so "SELECT * FROM
database INTO OUTFILE file" doesn't work for me. However, external tools
have been able to do it (Mascon). I presume this might be a common thing
where people need to backup their databases programatically from MySQLdb.


Take a look at

http://aspn.activestate.com/ASPN/Coo...n/Recipe/81189

That'll show you how to access the structure, though I don't know if it's
really the sort of thing you want.

regards
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/

Jul 18 '05 #3
Thanks David for the little code snippets. They help in figuring out the
table structures. Now if I can get the output of:
"SELECT * FROM database INTO OUTFILE file"
to redirect to across the network rather than the local file-system.
mysqldump also puts it into the local FS. There might be no recourse other
than having to recreate the INSERT statements in your own code.

Hoang Do
Jul 18 '05 #4
"Hoang" <tr@jotsite.com> wrote in message news:<NR***************@newssvr14.news.prodigy.com >...
"SELECT * FROM database INTO OUTFILE file"
to redirect to across the network rather than the local file-system.
mysqldump also puts it into the local FS. There might be no recourse other
than having to recreate the INSERT statements in your own code.


I think someone can't be bothered reading the documentation. :)
Delete your dump_structure_sql and replace with this code. To the best
of my knowledge this replicates the part of MySQLdump that you
require. dump_data_sql could be broken out into another function or
two, but for speed I left it as it is below.
Hope this helps,

David.
PS: it outputs in MySQL 'extended' INSERT format, which is apparently
slightly faster than multiple INSERTs, but may not work with other
databases.

def dump_sql(db, dump_data = False):
print "#"
print "# Dumping schema for database", get_db_name(db)
print "#"
print
print

for table, create_def in get_structure_sql(db).iteritems():
print "#"
print "# Dumping schema for table", table
print "#"
print
print create_def
print
print

if dump_data:
dump_data_sql(db, table)
def get_column_names(cursor, table):
cursor.execute("DESCRIBE %s" % (table))
return [ row[0] for row in cursor ]
def dump_data_sql(db, table):
cursor = db.cursor()
colnames = get_column_names(cursor, table)
colnames_sql = ', '.join(colnames)

count = cursor.execute("SELECT %s FROM %s" % (colnames_sql, table))

if count == 0:
return
print "#"
print "# Dumping data for table", table
print "#"
print

print "INSERT INTO %s(%s) VALUES" % (table, colnames_sql)
count -= 1
for index, row in enumerate(cursor):
row_sql = " (%s)" % (', '.join(db.escape(row)))

if index < count:
print row_sql + ","
else:
print row_sql + ";"

print
print
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
11
by: grumfish | last post by:
I'm trying to add a row to a MySQL table using insert. Here is the code: connection = MySQLdb.connect(host="localhost", user="root", passwd="pw", db="japanese") cursor = connection.cursor()...
2
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE...
1
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
2
by: hiroc13 | last post by:
>>import MySQLdb When I start this code I get ((15L, 'test', 1L),) on the screen but if I first execute this: ..... this write to table1 and now this:
1
by: TYR | last post by:
I have a large dump file that originated in a MySQL db; I need to get it into an SQLite file. Various options are suggested around the web; none of them seem to work (most failing to import the...
4
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.