I have a xx.db file which I access with the keys, printing the keys to the screen for checking. There are many levels of keys, I'm showing the top level here.
for filename in filenames:
with contextlib.closing(shelve.open(filename, flag = "w")) as data:
prdata = data['all'].keys()
print prdata I have a table created like this in Python:
tbl= """CREATE TABLE IF NOT EXISTS table2 (
`class` varchar(30) default NULL,
`name` varchar(120) default NULL,
)"""
cursor.execute(tbl)
I would like to load the output of the first collection of data (from .db file) into the created MySQLdb table. How should I proceed? Do I first need to create a text file from the db file? I have read that LOAD DATA INFILE is a fast way to load data into the database from a text file. Since there is a lot of data in the db, it would be faster if I can skip making a text file first.
edit: Have tried this, provides syntax error"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prdata INTO TABLE table2 \n\t FIELDS TERMINATED BY ' '' . I do not understand the syntax documentation correctly and also how to enclose it in Python.
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
for filename in filenames: with contextlib.closing(shelve.open(filename, flag = "w")) as data:
prdata = data['all'].keys()cursor.execute("""LOAD DATA INFILE prdata INTO TABLE table2 FIELDS TERMINATED BY ' ' """)
Update: Decided to go with INSERT INTO table2 (class,name) VALUE (%s,%s) works fine