You are not logged in.
Hey all!
Got a folder with about 100 *.sql files and been trying to use the folowing python script to import them into the database:
#!/usr/bin/env python
import glob, os
patches = glob.glob('*.sql')
patches = sorted(patches)
for x in patches:
db = x.split("_")[2].replace('.sql', '')
os.system("mysql -u mangos -ppassword -v " + db + " < " + x)
But i get an error like this:
File "/opt/mangos/sql/updates/update.py
db = x.split("_")[2].replace('.sql', '')
IndexError: list index out of range
Tried different numbers instead of the [2], any clues?
FYI not very hightech python-guru
Last edited by Afnaf (2010-07-14 11:58:31)
Offline
This isn't python, but here's what I do:
1) I have a bash script called _genspscript.sh which contains the following:
#!/bin/bash
rm _spscript.sql
cat *.sql > _spscript.sql
2) Run that script and it creates a single .sql file with all the SPs in them. Then I just run that one file into MySQL.
(Shameless plug: If you're interested I also have a Python script that will read the Schema of a DB and create the basic CRUD stored routines in individual files for all tables in a MySQL DB.)
Matt
"It is very difficult to educate the educated."
Offline
YES, i have read the above but i would like to learn something else.
I would like some help with writing a script that inserts around 100 .sql files and run them with mysql command one after one. Any ideas or tips? Im really not to good at script or programming but would like to learn
Last edited by Afnaf (2010-07-14 12:00:30)
Offline
Why are you splitting on _?
Offline
It was a script I found in a HowTO so i dunno Should i change anything?
Last edited by Afnaf (2010-07-14 13:06:50)
Offline
Try this instead, for your db variable:
for x in patches:
db = os.path.splitext(x)[0]
os.path documentation http://docs.python.org/library/os.path.html
You need to install an RTFM interface.
Offline
Didn't work to good
Last edited by Afnaf (2010-07-14 15:19:35)
Offline