简体   繁体   中英

Insertion of data in mysql table

I need to insert these values in my table but i am getting error of "Operand should contain 1 column(s)"

Here is my code:

sample_text='E://ahmedrazatg.txt'
def stemming_text_1():

conn=pymysql.connect("localhost","root","root","wordnet",use_unicode=True, charset="utf8")
cursor=conn.cursor()
file_array=[]
file = open(sample_text,'r')
arr=[]
surah=[]
verse=[]
var1="ahmedraza"
for line in file.readlines():
        words=re.split(' |/|:|;|,|-RRB-|-LRB-|!|\*|\*\*|``',line)
        words=[line.replace(".","") for line in words]
        words=[line.replace("' '",'')for line in words]
        surah.append(words[0])
        verse.append(words[2])
        j=4

    while j<len(words):
        arr.append(words[j])
        sql="insert into cmp_translation1(surah_no,verse_no,translation,translator_id)values(%s,%s,%s,%s)"
        data=surah,verse,arr,var1
        r=cursor.execute(sql,data)
        j+=2  
 conn.commit()
 cursor.close()
 conn.close() 
stemming_text_1()

My input file is like this:

1|6|Guide us the straight way

2|63|And when We made a covenant with you

2|18|Deaf dumb blind and they cannot turn

It has almost 400 lines. this file is placed in words array.All the words at location words[0] which is 1,2,2 are stored in surah whereas all the words at location words[2] which is 6,63,18 are stored in verse.

I want my output like this:

translation_id|surah_no |verse_no |translation               |translator_id

1             |       1 |     6   |Guide us the straight way |ahmedraza

2             |       2 |    63   |We made covenant with you |ahmedraza

3             |       2 |      18 |Deaf dumb blind cannot tu |ahmedraza

I think it is this:

data=surah,verse,arr,var1

where it should be:

data = [surah, verse, arr, var1]

or this:

cursor.execute(sql,data)

where it should be:

sql = sql % data
cursor.execute(sql)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM