Import mdb database files to postgresql database using python

Source: Internet
Author: User
Tags mdb database psql

You can use the mdbtools tool to export each table contained in an mdb file to a csv file. The format of the access database and postgresQL database may be different. Therefore, you can use python File Processing to modify the csv file to a correct and recognizable format.

Description of the import script (this script runs on linux ):

1. apt-get install mdbtools and install mdbtools

2. Copy the mdb file to the linux Virtual Machine and modify the mdb file directory 'dir' in the script'

3. Modify server and database configurations

4. Execute the script

Copy codeThe Code is as follows:
#-*-Encoding: UTF-8 -*-
Import OS
Import re
Import psycopg2
Import csv

# Mdb file directory
Dir = R'/home/kotaimen/mdb_file /'
Mdb_tbl_dic = {}


Def make_create_ SQL ():
If OS. path. isfile (dir + 'create. SQL '):
OS. remove (dir + 'create. SQL ')


For mdb_file in OS. walk (dir ):
If len (mdb_file [2])> 0:
For file_p in mdb_file [2]:
If file_p [-3:] = 'mdb ':
Print file_p
Cmd = 'mdb-schema % s>/home/kotaimen/mdb_file/create. SQL'
Cmd = cmd % (dir + file_p)
Print cmd
OS. system (cmd)
Cmd = 'mdb-tables-1% s' % (dir + file_p)
Val = OS. popen (cmd). read ()
Mdb_tbl_dic [file_p] = val. split ('\ n ')
Print mdb_tbl_dic

Def modefy_create_ SQL ():
SQL _file_name = dir + 'create. SQL'
SQL _file_name_des = SQL _file_name + '_ new'
Fobj = open (SQL _file_name, 'R ')
Fobj_des = open (SQL _file_name_des, 'w ')
For eachline in fobj:
# Check whether the table name contains spaces
If eachline. find ('table')> = 0:
If eachline. find (';')> = 0:
Start_loc = eachline. find ('table') + 6
End_loc = eachline. find (';')
Tbl_name = eachline [start_loc: end_loc]
Eachline = eachline. replace (tbl_name, '"' + tbl_name + '"')
Else:
Start_loc = eachline. find ('table') + 6
End_loc = eachline. find ('\ n ')
Tbl_name = eachline [start_loc: end_loc]
Eachline = eachline. replace (tbl_name, '"' + tbl_name + '"')

If eachline. find ('drop table')> = 0:
Eachline = eachline. replace ('drop table', 'drop table if exists ')
If eachline. find ('table')> = 0:
Eachline = eachline. replace ('table', '"Table "')
# Create statement. The last line does not contain commas.
If eachline. find ('text')> = 0 and eachline. find (',')> 0:
Loc = eachline. find ('text ')
Eachline = eachline [0: loc] + 'text, \ N'
Elif eachline. find ('text')> = 0 and eachline. find (',') <0:
Loc = eachline. find ('text ')
Eachline = eachline [0: loc] + 'text \ N'
Fobj_des.writelines (eachline)
Fobj. close ()
Fobj_des.close ()
OS. remove (SQL _file_name)
OS. rename (SQL _file_name_des, SQL _file_name)

Def make_insert_csv ():
For file_p in mdb_tbl_dic.keys ():
For tbl in mdb_tbl_dic [file_p]:
If len (tbl)> 0:
Cmd = 'mdb-export % s> export s.csv '% (dir + file_p,' "'+ tbl + '"', dir + '"' + tbl + '"') # tbl. replace ('','_'). replace ('&','_'))
OS. system (cmd)

Def modefy_insert_CSV ():
For SQL _file in OS. walk (dir ):
If len (SQL _file [2])> 0:
For file_p in SQL _file [2]:
If file_p [-3:] = 'csv ':
SQL _file_name = dir + file_p
SQL _file_name_des = SQL _file_name + '_ new'
Fobj = open (SQL _file_name, 'R ')
Fobj_des = open (SQL _file_name_des, 'w ')
For (num, val) in enumerate (fobj ):
Eachline = val
If num = 0:
Col_list = eachline. split (',')
Stat = 'copy' + '"' + (file_p [0:-4]) + '"' + '(' # + ('% s, '* len (line) [:-1] + ')'
For col in col_list:
If col = 'table ':
Col = '"' + 'table' + '"'
If col. find ('\ n')> = 0:
Col. replace ('\ n ','')
Stat = stat + col + ','
Stat = stat [:-2] + ')' + 'from stdin with csv; \ N'
Eachline = stat

Fobj_des.writelines (eachline)
Fobj. close ()
Fobj_des.close ()
OS. remove (SQL _file_name)
OS. rename (SQL _file_name_des, SQL _file_name)


Def insert_pai_database ():

Cmd = 'psql-h 172.26.11.205-d ap_MapMyIndia_full_Sample-U postgres-f % s 2> log.txt '% (dir + 'create. SQL ')
OS. system (cmd)

For SQL _file in OS. walk (dir ):
If len (SQL _file [2])> 0:
For file_p in SQL _file [2]:
Print file_p
If file_p [-3:] = 'csv ':
Cmd = 'psql-h 172.26.11.205-d ap_MapMyIndia_full_Sample-U postgres-f % s 2> log.txt '% (dir +' "'+ file_p + '"')
OS. system (cmd)


If _ name _ = "_ main __":
#1. create a script for creating the TABLE contained in the mdb File
Make_create_ SQL ()
#2. Modify invalid characters in the create script
Modefy_create_ SQL ()
#3. Export tables in mdb to csv files
Make_insert_csv ()
#4. Change the first line of the csv script to the copy form
Modefy_insert_CSV ()

Insert_pai_database ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.