0

On one hand,I am writing a script which decompose words in syllabs. The result is a list of variables éléments called cod1. On the other hand, i have a mysql base ('décodeur') and a table 'codon' which contains a colunn of syllab called semes) and another one which contains the supposed significance of the syllab(called sens). I thought a sql query could successicely "read" the élements of the list cod1 and search them in the column 1 in order to give the sense written in the second column. But the query I wrote don't do the job and only catch the last syllab of cod1 and its sense.
Is it possible to make a query iterating on the elements of a list when we don't know in advance the number of the elements of this list? (the same syllab can be appear several times in the same word) Who can help me?

try:
    sql=("""SELECT semes, sens1 FROM codons WHERE semes = %s""")
    k.execute(sql, (cod1,))
    reponse = k.fetchall()

    for x in reponse:
        print(x)

except:
      pass                
if conn:
   conn.close()

I am with python 2.7 and mysql connector Thanks for your answer

Eljoj
  • 3
  • 1
  • using `len(list)` you can get number of elements in list. Python mostly doesn't need number of elements because it uses loop `for item in list:` – furas Jul 05 '19 at 22:17
  • list has no function to use SQL query. You could only convert list to DataFrame with modul pandas and DataFrame has functions to use SQL queries. But I'm not sure if it not use database SQLite for this. – furas Jul 05 '19 at 22:18
  • @furas : I don't know Pandas, i will do researchs on it. – Eljoj Jul 06 '19 at 07:10
  • [pandas](https://pandas.pydata.org/), [pandasql](https://github.com/yhat/pandasql/). And something different: [Pandas - Comparison with SQL](https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_sql.html), [How to rewrite your SQL queries in Pandas, and more](https://medium.com/jbennetcodes/how-to-rewrite-your-sql-queries-in-pandas-and-more-149d341fc53e). And Stackoverflow: [Executing an SQL query over a pandas dataset](https://stackoverflow.com/questions/45865608/executing-an-sql-query-over-a-pandas-dataset/45866311) – furas Jul 06 '19 at 14:34

1 Answers1

0

Python will not automatically spread a list and execute the query repeatedly. You need to write your own loop:

sql=("""SELECT semes, sens1 FROM codons WHERE semes = %s""")
for seme in cod1:
    try:
        k.execute(sql, (seme,))
        reponse = k.fetchall()
        for x in reponse:
            print(x)
    except:
          pass
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your help but i have no idea at all how to write the loop, can you guide me? – Eljoj Jul 06 '19 at 07:08
  • I don't understand the question. I wrote the loop in the answer. What more guidance do you need? – Barmar Jul 06 '19 at 17:05
  • I wrote this message before I unserstood you gave me the loop! And it works fine!!!! thanks for your answer. – Eljoj Jul 07 '19 at 19:44