You are not logged in.

#1 2010-07-13 19:57:56

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

[Updated topic] Need help writing a script

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

#2 2010-07-13 20:09:30

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: [Updated topic] Need help writing a script

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

#3 2010-07-14 07:51:19

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Re: [Updated topic] Need help writing a script

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 smile

Last edited by Afnaf (2010-07-14 12:00:30)

Offline

#4 2010-07-14 12:15:43

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [Updated topic] Need help writing a script

Why are you splitting on _?

Offline

#5 2010-07-14 13:04:47

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Re: [Updated topic] Need help writing a script

It was a script I found in a HowTO so i dunno smile Should i change anything?

Last edited by Afnaf (2010-07-14 13:06:50)

Offline

#6 2010-07-14 13:13:33

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: [Updated topic] Need help writing a script

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

#7 2010-07-14 14:45:46

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Re: [Updated topic] Need help writing a script

Didn't work to good sad

Last edited by Afnaf (2010-07-14 15:19:35)

Offline

Board footer

Powered by FluxBB