2017-08-13 13 views
0

我試圖在名爲'factura'的MySQL表中使用'INSERT ... SELECT'(英文發票)插入多個記錄'在Python中。在哪裏設置INSERT ... SELECT用於計費系統Python和MySQL的FOR BUcle

#QUERY OF THE CONSULTA DEL VADOR DEL IVA JURIDICO 
cursor.execute("SELECT ivajuridico FROM configuracion;") 
dato1 = cursor.fetchall() 
#CONSULTA DE LA INFORMACION DE LOS ARRENDATARIOS 
cursor.execute("SELECT c_cod, now(), relacionip.i_cod, i_vlrenta, a_tpersona FROM contratos INNER JOIN relacionip ON contratos.r_id = relacionip.r_id INNER JOIN inmuebles ON relacionip.i_cod = inmuebles.i_cod INNER JOIN arrendatarios ON contratos.a_cc = arrendatarios.a_cc;") 
dato2 = cursor.fetchall() 
connect.commit() 
for a in dato1: 
    ivaj = a[0] #16.0 (float) 
    print ivaj 
    for i in dato2: 
     if i[4] == 2: 
      #showinfo("","Es Jurídico") 
      iva = i[3]*ivaj/100 #vl arriendo*iva/100 
      total = i[3]+iva 
     else: 
      #showinfo("","Es Natural") 
      iva = 0 
      total = i[3]+iva 
    try: 
     cursor.execute('''INSERT INTO factura_arre(c_cod, fa_fecha, fa_iva, fa_total) SELECT c_cod, now(), '%f', '%f' FROM contratos INNER JOIN relacionip ON contratos.r_id = relacionip.r_id INNER JOIN inmuebles ON relacionip.i_cod = inmuebles.i_cod INNER JOIN arrendatarios ON contratos.a_cc = arrendatarios.a_cc;''' % (iva, total)) 
     connect.commit() 
    except: 
     pass 
    showinfo('Operación', "Grabado!") 

第一查詢的搜索在一個名爲「configuracion」表稱爲「IVAJurídico」稅,只包含一個浮點值(16.0)的值,而這一次被應用到那些法律租戶。

MariaDB> SELECT ivajuridico FROM configuracion; 
+-------------+ 
| ivajuridico | 
+-------------+ 
|   16 | 
+-------------+ 
1 row in set (0.00 sec) 

然後,第二個查詢搜索在一些相關的表中的結果是8條租戶和屬性的信息需要選用,因爲這些都是總在數據庫中的租戶。其中之一,第一個是類型合法的。

MariaDB> SELECT c_cod, now(), relacionip.i_cod, i_vlrenta, a_tpersona FROM contratos INNER JOIN relacionip ON contratos.r_id = relacionip.r_id INNER JOIN inmuebles ON relacionip.i_cod = inmuebles.i_cod INNER JOIN arrendatarios ON contratos.a_cc = arrendatarios.a_cc; 
+-------+---------------------+-------+-----------+------------+ 
| c_cod | now()    | i_cod | i_vlrenta | a_tpersona | 
+-------+---------------------+-------+-----------+------------+ 
| 1509 | 2017-07-03 23:55:08 | 1140 | 5284240 |   2 | 
| 1526 | 2017-07-03 23:55:08 | 170 | 687500 |   1 | 
| 1528 | 2017-07-03 23:55:08 | 88 | 432000 |   1 | 
| 22736 | 2017-07-03 23:55:08 | 386 | 1338000 |   1 | 
| 22754 | 2017-07-03 23:55:08 | 192 | 720000 |   1 | 
| 22789 | 2017-07-03 23:55:08 | 1144 | 645000 |   1 | 
| 22898 | 2017-07-03 23:55:08 | 448 | 3700000 |   1 | 
| 22900 | 2017-07-03 23:55:08 | 449 | 1100000 |   1 | 
+-------+---------------------+-------+-----------+------------+ 
8 rows in set (0.00 sec) 

下一個代碼,我認爲,這是可以理解的,因爲它定義,如果一個租客的類型是合法的,那麼稅收法律人的價值必須得到應用,否則爲0

然而,其餘的代碼,其中try:是和INSERT ... SELECT必須執行,我遇到了問題,因爲它給了我8個記錄與所有租戶和稅0相同的租金價值,忽略其中一個是合法的。但是,如果我設置try及以下else它的內容我有64個recodrs而不是8

mysql> select * from factura_Arre; 
+--------+-------+---------------------+--------+----------+-----------+----------+ 
| fa_num | c_cod | fa_fecha   | fa_iva | fa_total | fa_estado | fa_notas | 
+--------+-------+---------------------+--------+----------+-----------+----------+ 
|  40 | 1509 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  41 | 1526 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  42 | 1528 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  43 | 22736 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  44 | 22754 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  45 | 22789 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  46 | 22898 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
|  47 | 22900 | 2017-06-08 01:16:01 |  0 | 1100000 |   0 | NULL  | 
+--------+-------+---------------------+--------+----------+-----------+----------+ 
8 rows in set (0.00 sec) 

事情就是try:應位於或者什麼其他的代碼,我需要做工作?

+0

從插入運行select語句..本身沒有%f列選擇。你會得到8條記錄。您將使用最後一個值iva(總數)插入這8條記錄。將打印語句放在iva上,並將總數放在if塊或其他塊中,然後在插入之前...選擇並且您將看到爲什麼會出現此行爲 – LAS

回答

0

我縮進了try塊,並從插入中刪除了select。它重複了被迭代的dato2選擇。我使用dat02的c_cod作插入。

#QUERY OF THE CONSULTA DEL VADOR DEL IVA JURIDICO 
cursor.execute("SELECT ivajuridico FROM configuracion;") 
dato1 = cursor.fetchall() 
#CONSULTA DE LA INFORMACION DE LOS ARRENDATARIOS 
cursor.execute("SELECT c_cod, now(), relacionip.i_cod, i_vlrenta, a_tpersona  FROM contratos INNER JOIN relacionip ON contratos.r_id = relacionip.r_id INNER JOIN inmuebles ON relacionip.i_cod = inmuebles.i_cod INNER  JOIN arrendatarios ON contratos.a_cc = arrendatarios.a_cc;") 
dato2 = cursor.fetchall() 
connect.commit() 
for a in dato1: 
    ivaj = a[0] #16.0 (float) 
    print ivaj 
    for i in dato2: 
     if i[4] == 2: 
      #showinfo("","Es Jurídico") 
      iva = i[3]*ivaj/100 #vl arriendo*iva/100 
      total = i[3]+iva 
     else: 
      #showinfo("","Es Natural") 
      iva = 0 
      total = i[3]+iva 
     try: 
     cursor.execute("INSERT INTO factura_arre(c_cod, fa_fecha, fa_iva, fa_total) values ('%s', now(), '%f', '%f')" % (i[0], iva, total)) # i[0] is c_cod for the row just calculated. 
     connect.commit() 
     except: 
      pass 
    showinfo('Operación', "Grabado!") 
+0

噢!而已!我認爲這更簡單。謝謝!!! – Einnerlink