|
16 | 16 | 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!)
|
17 | 17 | dbTables = msCursor.fetchall()
|
18 | 18 | noLength = [56, 58, 61] #list of MSSQL data types that don't require a defined lenght ie. datetime
|
19 |
| - |
20 | 19 | for tbl in dbTables:
|
| 20 | + print 'migrating {0}'.format(tbl[0]) |
21 | 21 | msCursor.execute("SELECT * FROM syscolumns WHERE id = OBJECT_ID('%s')" % tbl[0]) #syscolumns: see sysobjects above.
|
22 | 22 | columns = msCursor.fetchall()
|
23 | 23 | attr = ""
|
|
34 | 34 | attr += col.name +" "+ colType + "(" + str(col.length) + "),"
|
35 | 35 |
|
36 | 36 | attr = attr[:-1]
|
| 37 | + |
| 38 | + print 'Fetch rows from table {0}'.format(tbl[0]) |
37 | 39 | myCursor.execute("CREATE TABLE " + tbl[0] + " (" + attr + ");") #create the new table and all columns
|
38 | 40 | 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 | + |
53 | 63 |
|
0 commit comments