import MySQLdb import string db MySQLdb connect host 127 user root db

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import MySQLdb
import string
db = MySQLdb.connect(host = "127.0.0.1", user = "root", db = "tests", charset = 'cp1251')
cursor = db.cursor()
f = open("C:\Project\log", "r")
l = f.readline()[:-2]
while l!="":
s = (l[:-2], f.readline()[:-2], f.readline()[:-2], f.readline()[:-2], f.readline()[:-2])
s = str(s)
sql = "INSERT INTO problems ('subject', 'number', 'question', 'answer', 'correct_answer') VALUES" + s
cur.execute(sql)
db.commit()
f.readline()
db.close()
f.close()