Skip to content

Commit 1cbf835

Browse files
author
Ubuntu
committedMar 6, 2014
Some updates to allow for larger tables, and some more progress
1 parent c10f007 commit 1cbf835

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed
 

‎convert_db.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
msCursor.execute("SELECT * FROM sysobjects WHERE type='U'") #sysobjects is a table in MSSQL db's containing meta data about the database. (Note: this may vary depending on your MSSQL version!)
1717
dbTables = msCursor.fetchall()
1818
noLength = [56, 58, 61] #list of MSSQL data types that don't require a defined lenght ie. datetime
19-
2019
for tbl in dbTables:
20+
print 'migrating {0}'.format(tbl[0])
2121
msCursor.execute("SELECT * FROM syscolumns WHERE id = OBJECT_ID('%s')" % tbl[0]) #syscolumns: see sysobjects above.
2222
columns = msCursor.fetchall()
2323
attr = ""
@@ -34,20 +34,30 @@
3434
attr += col.name +" "+ colType + "(" + str(col.length) + "),"
3535

3636
attr = attr[:-1]
37+
38+
print 'Fetch rows from table {0}'.format(tbl[0])
3739
myCursor.execute("CREATE TABLE " + tbl[0] + " (" + attr + ");") #create the new table and all columns
3840
msCursor.execute("select * from %s" % tbl[0])
39-
tblData = msCursor.fetchall()
40-
41-
#populate the new MySQL table with the data from MSSQL
42-
for row in tblData:
43-
fieldList = ""
44-
for field in row:
45-
if field == None:
46-
fieldList += "NULL,"
47-
else:
48-
field = MySQLdb.escape_string(str(field))
49-
fieldList += "'"+ field + "',"
50-
51-
fieldList = fieldList[:-1]
52-
myCursor.execute("INSERT INTO " + tbl[0] + " VALUES (" + fieldList + ")" )
41+
tblData = msCursor.fetchmany(1000)
42+
43+
while len(tblData) > 0:
44+
cnt = 0
45+
#populate the new MySQL table with the data from MSSQL
46+
for row in tblData:
47+
fieldList = ""
48+
for field in row:
49+
if field == None:
50+
fieldList += "NULL,"
51+
else:
52+
field = MySQLdb.escape_string(str(field))
53+
fieldList += "'"+ field + "',"
54+
55+
fieldList = fieldList[:-1]
56+
myCursor.execute("INSERT INTO " + tbl[0] + " VALUES (" + fieldList + ")" )
57+
cnt += 1
58+
if cnt%100 == 0:
59+
print 'inserted 100 rows into table {0}'.format(tbl[0])
60+
db.commit()
61+
tblData = msCursor.fetchmany(1000)
62+
5363

0 commit comments

Comments
 (0)
Please sign in to comment.