vortoj2mysql
author viric <viriketo@gmail.com>
Sun, 13 Feb 2011 08:43:06 +0100
changeset 5 f2a2f64eb682
parent 0 6371497b4e53
permissions -rwxr-xr-x
Events don't work as I expect. tab makes save sentence, while enter should be
doing that.
#!/usr/bin/python
# -*- coding: utf-8 -*-

import shelve,string,os,MySQLdb,re

database = 'lingvigilo'
dbuser = 'lingvigilouser'
dbpass = 'carrinclo'
dbtable = 'vortoj_ru_ca'

db = MySQLdb.connect(host='vicerveza',user=dbuser,passwd=dbpass,db=database)

cursor = db.cursor()

sourcefile = 'vortoj-ru-ca.txt'

# Connexió base de dades


# Read txt
file = open(sourcefile, 'r')

cursor.execute('DELETE FROM ' + dbtable);

for line in file:
	# Traiem l'INTRO final
	line = line.rstrip('\n')

	# Arreglem cometes simples per a sql
	line = re.sub(r"'",r"\'", line)

	# Separem per \t
	words = string.split(line, '\t')

	print "Aldonante:", words[0], '=>', words[2]

	if words[1] == "verb":
		#print ('INSERT INTO ' + dbtable +
		#	' (vorto,tipo,traduko,param1,param2) '+
		#	'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
		#	(words[0], words[1], words[2], words[3], words[4]))
		if (len(words) == 5):
			cursor.execute('INSERT INTO ' + dbtable +
				' (vorto,tipo,traduko,param1,param2) '+
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\');' %
				(words[0], words[1], words[2], words[3],
				words[4]))
		else:
			cursor.execute('INSERT INTO ' + dbtable +
				' (vorto,tipo,traduko,param1) '+
				'VALUES(\'%s\',\'%s\',\'%s\',\'%s\');' %
				(words[0], words[1], words[2], words[3]))
	else:
		#print ('INSERT INTO ' + dbtable +
		#	' (vorto,tipo,traduko) '+
		#	'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
		#	words[2]))
		cursor.execute('INSERT INTO ' + dbtable +
			' (vorto,tipo,traduko) '+
			'VALUES(\'%s\',\'%s\',\'%s\');' % (words[0], words[1],
			words[2]))

db.close()

file.close()